java-appium-527进阶-1 UiAutomator1&2区别和封装
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);
}
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" ] ```#shell # 使用该命令可以查看执行时间,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<img src="https://img2018.cnblogs.com/blog/1418970/201810/1418970-20181004151953554-760908803.png" width="550" />
# 4.总结
<font color=#458B00> 1.UiAutomator使用id在匹配resourceId失败时,会使用ACCESSIBILITY_ID定位
UiAtumator2使用id定位被限制,只能使用resourceId定位
2.使用xpath和id定位,耗时差别可接受</font>
# 封装寻找控件方法的测试代码
test.java
```#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();
}
}