java-appium-527进阶-1 UiAutomator12区别和封装
1.UiAutomator和UiAtumator2的區別:
1.1 UiAutomator1有關于id定位的策略
UiAutomator1 id定位在resourceid匹配失敗時,會匹配contentDesc。
安卓會根據id進行3種情況的判斷:
- 1.resourceId 如user_profile_icon
- 2.accessibility id
- 3.Strings.xml 研發所寫的resource id,包含包名,如com.xueqiu.android:id/user_profile_icon。如果傳入的text是resource Id將只使用resource id選擇器
1.2 UiAutomator2定位策略
UiAutomator2中id定位只匹配resourceId
if (by instanceof ById) {String locator = rewriteIdLocator((ById) by);return CustomUiDevice.getInstance().findObject(android.support.test.uiautomator.By.res(locator));} else if (by instanceof By.ByAccessibilityId) {return CustomUiDevice.getInstance().findObject(android.support.test.uiautomator.By.desc(by.getElementLocator()));} else if (by instanceof ByClass) {return CustomUiDevice.getInstance().findObject(android.support.test.uiautomator.By.clazz(by.getElementLocator()));} else if (by instanceof By.ByXPath) {final NodeInfoList matchedNodes = getXPathNodeMatch(by.getElementLocator(), null);if (matchedNodes.size() == 0) {throw new ElementNotFoundException();}return CustomUiDevice.getInstance().findObject(matchedNodes);} else if (by instanceof By.ByAndroidUiAutomator) {UiSelector selector = toSelector(by.getElementLocator());if (selector == null) {throw new UiSelectorSyntaxException(by.getElementLocator(), "");}return CustomUiDevice.getInstance().findObject(selector); }詳見:https://github.com/appium/appium-uiautomator2-server/blob/master/app/src/main/java/io/appium/uiautomator2/handler/FindElement.java
2. 查找控件方法的簡單封裝
在UiAutomator1中,id定位會去匹配contentDesc,此處只考慮xpath, id, contentDesc。
傳入String,輔以正則表達式,如果匹配Xpath,就用xpath定位;如果匹配id,就用id定位
3. id和xpath定位的區別
使用id,contentDesc,class定位都是使用uiatumator自帶的方法,使用xpath定位需要先獲取節點信息,再對節點信息做檢索
同一個元素:id: "com.xueqiu.android:id/tv_login" xpath: //*[@resource-id,"com.xueqiu.android:id/tv_login" ]
4.總結
1.UiAutomator使用id在匹配resourceId失敗時,會使用ACCESSIBILITY_ID定位
UiAtumator2使用id定位被限制,只能使用resourceId定位
2.使用xpath和id定位,耗時差別可接受
封裝尋找控件方法的測試代碼
test.java
import io.appium.java_client.MobileElement; import io.appium.java_client.android.AndroidDriver; import junit.framework.TestCase; import org.junit.After; import org.junit.Before; import org.junit.Test;import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.concurrent.TimeUnit;import org.junit.experimental.theories.Theories; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.DesiredCapabilities;public class XueqiuDemo {private AndroidDriver driver;@Beforepublic void setUp() throws MalformedURLException {DesiredCapabilities desiredCapabilities = new DesiredCapabilities();desiredCapabilities.setCapability("platformName", "android");desiredCapabilities.setCapability("deviceName", "domo");desiredCapabilities.setCapability("appPackage", "com.xueqiu.android");desiredCapabilities.setCapability("appActivity", ".view.WelcomeActivityAlias");URL remoteUrl = new URL("http://localhost:4723/wd/hub");driver = new AndroidDriver(remoteUrl, desiredCapabilities);driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);}@Testpublic void sampleTest() throws InterruptedException {locate("com.xueqiu.android:id/user_profile_icon").click();Thread.sleep(2000);locate("com.xueqiu.android:id/tv_login").click();Thread.sleep(2000);locate("com.xueqiu.android:id/tv_login_by_phone_or_others").click();Thread.sleep(2000);locate("com.xueqiu.android:id/register_phone_number").sendKeys("123456789");}public WebElement locate(String locate){if (locate.matches("\\/\\/.*")){return driver.findElementByXPath(locate);}else{return driver.findElementById(locate);}}@Afterpublic void tearDown() {driver.quit();} }轉載于:https://www.cnblogs.com/csj2018/p/9741808.html
總結
以上是生活随笔為你收集整理的java-appium-527进阶-1 UiAutomator12区别和封装的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【图像处理】——图像滤波(Python+
- 下一篇: uds 诊断协议的bootloader开