如何让父 UiSelector 从其子项开始?

问题描述 投票:0回答:0

注意:使用 Java 11 和 Appium。

我正在尝试使用 Android UiAutomators 为 appium 测试套件获取 MobileElement。具体来说,我需要滚动到一个,所以我正在使用

@AndroidFindBy(uiAutomator = "new UiScrollable(new UiSelector().scrollable(true)).scrollIntoView(new UiSelector()...)"
构造定位器。我的问题是我需要的元素没有唯一标识符,但孩子有。

元素树看起来像这样

<RecyclerView>
   <LinearLayout>
      <TextView text="name">
      <LinearLayout>
         <TextView text="type">
         <TextView text="unique id"> <---I can find this
      <Switch> <--- Element I need
   <LinearLayout>
      <TextView text="name">
      <LinearLayout>
         <TextView text="type">
         <TextView text="another unique id">
      <Switch>

我试过

new UiScrollable(new UiSelector().scrollable(true)).scrollIntoView(new UiSelector().textContains("unique text").fromParent(new UiSelector())
但这只给了我 LinearLayout 下的另一个 TextView。我也尝试过
.childSelector()
和其他路径,但由于 TestView 是唯一的独特元素,我总是以它结束,没有办法回到树上。

有什么建议吗?

RecyclerView 有很多这样的 LinearLayouts,所以我的最终目标是有一个 java 方法可以根据其兄弟的孩子的唯一文本找到开关。名称和类型可以跨元素共享,但 id 始终是唯一的。

private WebElement getSwitch(String name, String uniqueId){
   return driver.findElement(AppiumBy.androidUIAutomator(
      String.format("new UiScrollable(new UiSelector().scrollable(true))"+
      ".scrollIntoView(new UiSelector()...text(\"%s\")...textContains(\"%s\"))", name, uniqueId)));
}
java android automated-tests appium-android android-uiautomator
© www.soinside.com 2019 - 2024. All rights reserved.