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选择器
更多信息在appium/appium-android-bootstrap分支,详见https://github.com/appium-boneyard/appium-android-bootstrap/blob/master/bootstrap/src/io/appium/android/bootstrap/handler/Find.java。
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定位public WebElement locate(String locate){ if (locate.matches("\\/\\/.*")){ return driver.findElementByXPath(locate); }else{ return driver.findElementById(locate); } }
3. id和xpath定位的区别
使用id,contentDesc,class定位都是使用uiatumator自带的方法,使用xpath定位需要先获取节点信息,再对节点信息做检索 同一个元素:id: "com.xueqiu.android:id/tv_login" xpath: //*[@resource-id,"com.xueqiu.android:id/tv_login" ]# 使用该命令可以查看执行时间,v代表详细信息,此处只查看结果和执行时间,将--v去除time curl -X POST http://127.0.0.1:4723/wd/hub/session/${SessionId}/elements --data-binary '{"using":"id","value":"com.xueqiu.android:id/tv_login"}' -H "Content-Type: application/json; charset=UTF-8" --v
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; @Before public 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); } @Test public 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); } } @After public void tearDown() { driver.quit(); }}