【Appium】使用uiautomator定位元素

介紹:appium可以使用uiautomator 來定位元素,或者滾動頁面。使用uiautomator 只適用於Android。

下面以appium官方自帶的調試App來演示。
調試App下載地址:https://github.com/appium/appium/blob/master/sample-code/apps/ApiDemos-debug.apk

通過className定位

通過classname獲取第一個實例

driver.findElementByAndroidUIAutomator("new UiSelector().className(\"android.widget.TextView\").instance(1)");

通過text定位

在這裏插入圖片描述
如上圖,現在定位Views這個元素,它的text屬性值也爲“Views”,那就可以使用如下定位:

driver.findElementByAndroidUIAutomator("new UiSelector().text(\"Views\")");

滑動頁面直到找到對應的元素

在這裏插入圖片描述
如上圖,Popup Menu元素需要往下滑動頁面才能看到。uiautomator提供滾動屏幕,直到找到目標元素的方法。有以下兩種方式:
第一種:

driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0))" +
                ".getChildByText(new UiSelector().className(\"android.widget.TextView\"), \"Popup Menu\")")

第二種:

driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0))." +
                "scrollIntoView(new UiSelector().text(\"Popup Menu\").instance(0));")

完整代碼:

package test.java.cases;

import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import test.java.common.InitDriver;
import test.java.common.OperateElement;

import java.net.URL;
import java.util.concurrent.TimeUnit;

/**
 * Author: 靈樞
 * Date: 2019/11/22
 * Time: 9:34
 * Description:
 */
public class UIAutomatorTest {
    private AndroidDriver driver;

    @BeforeClass
    public void setUp() throws Exception{
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("deviceName","Android Emulator");
        capabilities.setCapability("noReset",true);
        capabilities.setCapability("unicodeKeyboard",true);
        capabilities.setCapability("autoGrantPermissions",true);
        capabilities.setCapability("appPackage","io.appium.android.apis");
        capabilities.setCapability("appActivity",".ApiDemos");
        //初始化
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        // 設置隱式等待時間
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    @AfterClass()
    public void tearDown() {
        driver.quit();
    }

    @Test
    public void testUiSelector(){
        // 通過text查找
        driver.findElementByAndroidUIAutomator("new UiSelector().text(\"Views\")").click();

        // 滑動屏幕查找,第一種方式
        /* String orderNo = driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0))" +
                ".getChildByText(new UiSelector().className(\"android.widget.TextView\"), \"Popup Menu\")").getText();
        System.out.println(orderNo);*/

        // 滑動屏幕查找,第二種方式
        driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0))." +
                "scrollIntoView(new UiSelector().text(\"Popup Menu\").instance(0));").click();
        OperateElement.threadSleep(3000);
    }
}

代碼運行如下:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章