日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

java-appium-527进阶-1 UiAutomator12区别和封装

發布時間:2023/12/10 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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選擇器

更多信息在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;@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区别和封装的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。