APP自動化測試關鍵環節--元素定位,如下咱們來了解appium提供的元素定位方法!java
1. id定位,id一個控件的惟一標識,由開發人員在項目中指定,若是一個元素有對應的resource-id,咱們就能夠採用這種方式來實現元素定位操做,可是實際開發中,也有可能app項目的開發人員不是很嚴謹,一個頁面有不少個相同的id,獲取到的元素結果是一個集合,因此這種狀況咱們須要用list進行接收android
//找到想要定位的元素並進行點擊 androidDriver.findElement(By.id("id")).click();
2. text 定位,java-client舊版本提供了相應的API支持session
//根據text屬性進行定位 androidDriver.findElement(By.name("登陸"));
注意:java-client新版本運行以後會發現定位失敗,這是由於java-client 5.0 之後新版本不支持這個API:app
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: Locator Strategy 'name' is not supported for this session (WARNING: The server did not provide any stacktrace information)
解決方案:框架
androidDriver.findElementByAndroidUIAutomator("new UiSelector().text(\"登陸\")");
注意提示:ide
由於:測試
解決辦法:Configure Build Path jdk1.8 ui
配置後:this
3. className 定位。根據class類查找元素,通常獲得的是多個元素(除非className惟一),若是獲得多個,咱們須要用一個List集合接收返回值spa
//根據className屬性進行定位 List<WebElement> listElement = androidDriver.findElements(By.className(("className"))); listElement.get(1).click();
4. xpath 定位
//根據xpath屬性進行定位 androidDriver.findElement(By.xpath("//android.widget.Button[@text='登陸']")).click(); androidDriver.findElement(By.xpath("//android.widget.Button[@text=\"登陸\"]")).click();
5. accessibility id 元素定位
//根據AccessibilityId進行定位 androidDriver.findElementByAccessibilityId("登陸").click();
6. 座標定位:經過開發者選項>>指針位置或者是UIAutomatorView獲取到的位置(絕對座標)
//座標定位 //經過TouchAction類完成座標點擊 TouchAction touchAction = new TouchAction<>(androidDriver); //將(x,y)座標封裝成PointOption對象傳入tap方法調用 PointOption pointOption =PointOption.point(500, 500); //經過調用tap方法進行點擊.調用perform()方法執行點擊 touchAction.tap(pointOption).perform();
7. UIAutomator 定位,安卓的UIAutomator是一個強有力的元素定位方式,它是經過Android提供的原生的UIAutomator框架去找元素,且支持元素所有屬性定位,appium提供的定位元素API其實都是UIAutomator提供的API,appium元素定位方式以下:
//經過UIAutomator 的description 方法找到屬性是 content-desc 的元素 androidDriver.findElementByAndroidUIAutomator("new UiSelector().description(\"登陸\")").click(); //經過UIAutomator的text 方法找到屬性是text的元素 androidDriver.findElementByAndroidUIAutomator("new UiSelector().text(\"登陸\")").click(); //經過UIAutomator的 resourceId方法找到屬性是 resourceID的元素 androidDriver.findElementByAndroidUIAutomator("new UiSelectot().resourceId(\"resource-id\")").click();
8.未完待續.......