【Appium】使用uiautomator定位元素

野性酷女 2023-06-15 11:23 222阅读 0赞

介绍:appium可以使用uiautomator 来定位元素,或者滚动页面。使用uiautomator 只适用于Android。

下面以appium官方自带的调试App来演示。
调试App下载地址:https://github.com/appium/appium/blob/master/sample-code/apps/ApiDemos-debug.apk

通过className定位

通过classname获取第一个实例

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

通过text定位

在这里插入图片描述
如上图,现在定位Views这个元素,它的text属性值也为“Views”,那就可以使用如下定位:

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

滑动页面直到找到对应的元素

在这里插入图片描述
如上图,Popup Menu元素需要往下滑动页面才能看到。uiautomator提供滚动屏幕,直到找到目标元素的方法。有以下两种方式:
第一种:

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

第二种:

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

完整代码:

  1. package test.java.cases;
  2. import io.appium.java_client.android.AndroidDriver;
  3. import org.openqa.selenium.WebElement;
  4. import org.openqa.selenium.remote.DesiredCapabilities;
  5. import org.testng.annotations.AfterClass;
  6. import org.testng.annotations.BeforeClass;
  7. import org.testng.annotations.Test;
  8. import test.java.common.InitDriver;
  9. import test.java.common.OperateElement;
  10. import java.net.URL;
  11. import java.util.concurrent.TimeUnit;
  12. /** * Author: 灵枢 * Date: 2019/11/22 * Time: 9:34 * Description: */
  13. public class UIAutomatorTest {
  14. private AndroidDriver driver;
  15. @BeforeClass
  16. public void setUp() throws Exception{
  17. DesiredCapabilities capabilities = new DesiredCapabilities();
  18. capabilities.setCapability("platformName", "Android");
  19. capabilities.setCapability("deviceName","Android Emulator");
  20. capabilities.setCapability("noReset",true);
  21. capabilities.setCapability("unicodeKeyboard",true);
  22. capabilities.setCapability("autoGrantPermissions",true);
  23. capabilities.setCapability("appPackage","io.appium.android.apis");
  24. capabilities.setCapability("appActivity",".ApiDemos");
  25. //初始化
  26. driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
  27. // 设置隐式等待时间
  28. driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  29. }
  30. @AfterClass()
  31. public void tearDown() {
  32. driver.quit();
  33. }
  34. @Test
  35. public void testUiSelector(){
  36. // 通过text查找
  37. driver.findElementByAndroidUIAutomator("new UiSelector().text(\"Views\")").click();
  38. // 滑动屏幕查找,第一种方式
  39. /* 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);*/
  40. // 滑动屏幕查找,第二种方式
  41. driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0))." +
  42. "scrollIntoView(new UiSelector().text(\"Popup Menu\").instance(0));").click();
  43. OperateElement.threadSleep(3000);
  44. }
  45. }

代码运行如下:
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,222人围观)

还没有评论,来说两句吧...

相关阅读

    相关 appium元素定位(12)

    自动化测试来说,核心技能就是对象的定位。不管是 web 页面上的一个 按钮或输入框,还是移动 app 上的一个按钮或输框,我们要想对其进行点击或输入操作,前提是要先找到这