zoukankan      html  css  js  c++  java
  • Appium移动自动化测试-----(八)定位控件

    appium 通过 uiautomatorviewer.bat 工具来查看控件的属性。该工具位于 Android SDK 的 /tools/bin/ 目录下。

    id 定位


    通过uiautomatorviewer.bat 工具可以查看对象的id属性。

    如果目标设备的API Level低于18则UIAutomatorViewer不能获得对应的Resource ID,只有等于大于18的时候才能使用。

    打开uiautomatorviewer.bat工具:

    resource-id 就是我们理解的id属性了。

    使用方法:

    driver.findElement(By.id("com.android.calculator2:id/formula"))
    

    name 定位


    打开uiautomatorviewer.bat工具:

    text就是我们要查找的name了,为什么在命名上毫无关联啊!

    使用方法:

    driver.findElement(By.name("9"))
    

    class name 定位


    计算器界面上的的class属性是:android.widget.Button。 使用方法:

    WebElement button = driver.findElement(By.className("android.widget.Button"));
    

    使用 Class Name 一般获得的 view 都不止一个,所以应该需要遍历一遍得到的 views,然后缩小搜索条件来获得目标控件。

    XPath定位


    在 WebDriver 上 XPath 定位是功能强大的一种定位方式。我个人惯用于此方法来定位Web页面上的元素。下面看看在 Android 上 XPath 定位的用法。

    用class的属性来替代做标签的名字。

    使用方法:

    driver.findElement(By.xpath("//android.view.ViewGroup/android.widget.Button"))  //7
    

    当果如果出现class 相同的情况下可以用控件的属性值进行区分。

    driver.findElement(By.xpath("//android.widget.Button[contains(@text,'7')]")).click(); //7
    driver.findElement(By.xpath("//android.widget.Button[contains(@content-desc,'times')]")).click(); //*
    driver.findElement(By.xpath("//android.widget.Button[contains(@text,'7')]")).click();  //7
    driver.findElement(By.xpath("//android.widget.Button[contains(@content-desc,'equals')]")).click(); //=
    

    XPath 在 Appium 上的用法依然很强大,有时需要写更臭更长的定位语法,因为APP上元素的class命令本来就长,再加上多层级,结果可想而知。

    Accessibility ID定位


    这个方法属于Appium扩展的定位方法。

    其实,我们的核心是要找到元素的contentDescription属性。它就是元素的 content-desc 。

    使用方法:

    driver.findElementByAccessibilityId("plus").click();
    

    android uiautomator定位


    这个方法也属于 Appium(Android)扩展的定位方法。同样使用 UIAutomatorViewer.bat 工具直接查看。

    也就是说一个元素的任意属性都可以通过android uiautomator方法来进行定位,但要保证这种定位方式的唯一性。

    使用方法:

    driver.findElementByAndroidUIAutomator("new UiSelector().text("clr")").click();
    driver.findElementByAndroidUIAutomator("new UiSelector().text("8")").click();
    driver.findElementByAndroidUIAutomator("new UiSelector().description("plus")").click();
    driver.findElementByAndroidUIAutomator("new UiSelector().text("5")").click();
    driver.findElementByAndroidUIAutomator("new UiSelector().description("equals")").click();
    

    需要注意的是 description() 方法用的是content-desc属性。

    转自:http://www.testclass.net/appium/appium-base-find-element/

  • 相关阅读:
    代码规范总结
    git记住提交密码的技巧
    php foreach遍历
    flight学习笔记
    the resource is not on the build path of a php project
    Google安装postman插件
    PHP开发框架CodeIgniter
    eclipse中php项目开发的环境配置说明
    MyBatis入门篇
    mybatis学习(十二)——mybatis逆向工程
  • 原文地址:https://www.cnblogs.com/kaola8023/p/8474348.html
Copyright © 2011-2022 走看看