HJ3 10.7 高级定位技巧

xpath 定位进阶

uiautomator 定位表达式

  • https://developer.android.com/reference/android/support/test/uiautomator/UiSelecotr.html

  • 优点:

    • xpath 定位速度慢
    • uiautomator 是 android 的工作引擎,速度快
  • 缺点:

    • 表达式书写复杂,容易写错 IDE 没有提示
  • 通过 resourceid 定位

    • new UiSelector().resourceId("id")
  • 通过 classname 定位

    • new UiSelector().className("className")
  • 通过 conteng-desc 定位

    • new UiSelector().description("content-des 属性")
  • 用法:

    • ((AndroidDriver<MobileElement>)driver).findElementByAndroidUIAutomator(表达式).click()
  • 通过文本定位

  • 通过 text 文本定位语法

    • new UiSelector().text("text 文本")
  • 如果文本比较长,可以用 textContains 模糊匹配

    • new UiSelector().textContains("包含 text 文本")
  • 同样可以用 textStartsWith 是以某个文本开头来匹配

    • new UiSelector().textStartsWith("以 text 文本开头")
  • 也可以用正则表达式 textMatches 匹配

    • new UiSelector().textMatches("正则表达式")
  • 组合定位

    • 例1:id 与 text 属性组合
      String id_text = 'resourceId("com.xueqiu.android:id/tab_name").text("交易")'
      driver.findElementByAndroidUIAutomator(id_text).click()
      
    • 例2:class 与 text 属性结合
      String class_text = 'className("android.widget.TextView").text("行情")'
      driver.findElementByAndroidUIAutomator(class_text).click()
      
  • 父子关系定位 childSelector

    • 有时候不能直接定位某个元素,但是它的父元素很好定位,这时候就先定位父元素,通过父元素找儿子
    • son = 'resourceId("com.xueqiu.android:id/scroll_view").childSelector(text("热门"))'
  • 兄弟定位 fromParent

    • 有时候父元素不好定位,但是跟他相邻的兄弟元素很好定位,这时候就可以通过兄弟元素,找到同一父级元素下的子元素
    • brother = 'resourceId("com.xueqiu.android:id/tab_name").fromParent(text("我的"))'

滑动定位

  • 实现滚动查找元素
    • 'new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("查找的文本").instance(0));'
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容