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);
}

詳見: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" ] ```#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();
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM