Appium中部分api的使用方法

使用的语言是java,appium的版本是1.3.4,java-client的版本是java-client-2.1.0,建议多参考java-client-2.1.0-javadoc。

1.使用AndroidDriver,其已经继承了AppiumDriver

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
     private AndroidDriver driver;
 
@Before
public void setUp() throws Exception {
     DesiredCapabilities capabilities = new DesiredCapabilities();
     capabilities.setCapability( "deviceName" , "Android Emulator" );
     capabilities.setCapability( "platformVersion" , "4.4" );
     capabilities.setCapability( "platformName" , "Android" );
     capabilities.setCapability( "appPackage" , "com.android.settings" );
     capabilities.setCapability( "appActivity" , ".Settings" );
 
     driver = new AndroidDriver( new URL( "http://127.0.0.1:4723/wd/hub" ),
             capabilities);
}
 
@After
public void tearDown() throws Exception {
     driver.quit();
}
2.截屏并保存至本地

?
1
2
3
4
5
6
7
8
         //截屏并保存至本
File screen = driver.getScreenshotAs(OutputType.FILE);
File screenFile = new File( "d:\\screen.png" );
try {
     FileUtils.copyFile(screen, screenFile); //commons-io-2.0.1.jar中的api
} catch (IOException e) {
     e.printStackTrace();
}

3.push文件、pull文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
     File file = new File( "d:\\test.txt" ); //test.txt内容为"test"
String content = null ;
try {
     content = FileUtils.readFileToString(file);
} catch (IOException e) {
     e.printStackTrace();
}
 
byte [] data = Base64.encodeBase64(content.getBytes());
driver.pushFile( "sdcard/test.txt" , data);
 
byte [] resultDate = driver.pullFile( "sdcard/test.txt" );
System.out.println( new String(Base64.decodeBase64(resultDate))); //打印结果为"test"

4.

?
1
2
//获取当前界面的activity,可用于断言是否跳转到预期的activity
driver.currentActivity();

5.
?
1
2
//打开通知栏界面
driver.openNotifications();
6.

?
1
2
3
4
5
6
7
8
//获取网络状态
int status = driver.getNetworkConnection().value;
System.out.println(status);
 
//设置网络状态
driver.setNetworkConnection( new NetworkConnectionSetting(status));
//或者
driver.setNetworkConnection( new NetworkConnectionSetting( false , true , false ));
7.

?
1
2
//启动其他应用,跨APP
driver.startActivity( "com.android.camera" , ".CameraLauncher" );
8.

?
1
2
3
4
//自动滑动列表
driver.scrollTo( "text" );
//或者
driver.scrollToExact( "text" );
9.
?
1
2
3
4
5
         //安装APP
driver.installApp(appPath);
 
//判断应用是否已安装
driver.isAppInstalled( "package name" );
10.

?
1
2
3
//拖动相机图标至日历图标位置
new TouchAction(driver).longPress(driver.findElementByName( "相机" ))
.moveTo(driver.findElementByName( "日历" )).release().perform();
11.

?
1
2
3
4
5
         //锁屏
driver.lockScreen( 2 );
 
//判断是否锁屏
driver.isLocked();
12.

?
1
2
         //发送按键事件
driver.sendKeyEvent(AndroidKeyCode.HOME);

13.

?
1
2
3
4
5
6
7
8
9
<span style= "color:#ff0000;" > </span>
                 //通过uiautomator定位clickable属性为true的元素并点击
         driver.findElementByAndroidUIAutomator( "new UiSelector().clickable(true)" ).click();
         
         //相同属性的元素使用List存放
         List<webelement> elements = driver.findElementsByClassName( "class name" );
         elements.get( 0 ).click(); //点击List中的第一个元素
         //tap,点击元素位置
         driver.tap( 1 , driver.findElementByName( "日期和时间" ), 0 );</webelement>




转载自:http://www.2cto.com/kf/201501/368019.html

Appium 是一个开源的自动化测试工具,用于移动应用的UI测试,它支持多种平台和多种编程语言,包括 Java。在 Appium ,定位元素是测试脚本的核心部分,Java API 提供了多种方法来查找 UI 元素。以下是一些常见的元素定位方法: 1. By.id (根据ID定位):这是最常用的定位方式,通过元素的唯一 ID 来找到元素。 ```java WebElement element = driver.findElement(By.id("your_element_id")); ``` 2. By.name (根据名称定位):如果元素的name属性是唯一的,可以使用方法。 ```java WebElement element = driver.findElement(By.name("element_name")); ``` 3. By.className (根据class名称定位):定位具有特定CSS类名的元素。 ```java WebElement element = driver.findElement(By.className("your_class_name")); ``` 4. By.tagName (根据标签名定位):如定位所有的`<div>`元素。 ```java List<WebElement> elements = driver.findElements(By.tagName("div")); ``` 5. By.xpath (根据XPath表达式定位):提供强大的路径选择能力,适用于复杂结构的页面。 ```java WebElement element = driver.findElement(By.xpath("//element[@attribute='value']")); ``` 6. By.linkText (根据链接文字定位):找到匹配链接文本的元素。 ```java WebElement element = driver.findElement(By.linkText("link_text")); ``` 7. By.partialLinkText (根据链接文本的部分匹配):对链接文本进行模糊匹配。 每个定位器的使用取决于实际的UI元素结构,可能需要尝试多种方法才能找到正确的元素。定位元素后,可以执行点击、输入等操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值