在 UITestCase 期间使用辅助功能标识符识别 UISearchBar

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

我的其中一个

UISearchBar
上有一个
UIViewController
。 但是
UISearchBar
默认情况下没有可访问性标识符,因此我尝试使用“用户定义运行时属性”来定义手动可访问性标识符。

但是在

UITestCase
写作过程中,我无法使用“用户定义运行时属性”来访问或识别
UISearchBar
。但我无法使用标识符访问它。我总是必须使用我不想使用的占位符文本,因为它在具有“多语言”支持时会导致问题。

我参考了有关此问题的堆栈溢出答案,但没有一个符合我的要求。

请帮忙提供解决方案。 谢谢你。

ios xcode-ui-testing
2个回答
7
投票

在应用程序中,当我使用用户定义运行时属性将

accessibilityLabel
UISearchBar
设置为
MySearchBar
时,如果您查看应用程序的 UI 层次结构,您可以看到以下内容:

Attributes: Application, 0x604000198050, pid: 18393, {{0.0, 0.0}, {414.0, 736.0}}, label: 'MyApp'
Element subtree:
 →Application, 0x604000198050, pid: 18393, {{0.0, 0.0}, {414.0, 736.0}}, label: 'MyApp'
    Window, 0x6000001993d0, Main Window, {{0.0, 0.0}, {414.0, 736.0}}
      Other, 0x604000199a50, traits: 8589934592, {{0.0, 0.0}, {414.0, 736.0}}
        Other, 0x600000197aa0, traits: 8589934592, {{0.0, 20.0}, {414.0, 56.0}}, identifier: 'MySearchBar'
          Image, 0x604000198390, traits: 8589934596, {{0.0, 0.0}, {414.0, 76.0}}
          SearchField, 0x60000019a4e0, traits: 146031248384, {{8.0, 30.0}, {398.0, 36.0}}

这应该会让您了解如何找到搜索栏。首先搜索具有您设置到搜索栏的标识符的

other
类型元素,然后查找该
searchBar
类型元素的子元素。

let searchField = XCUIApplication().otherElements["MySearchBar"].searchFields.firstMatch
searchField.tap()
searchField.typeText("Hello")

0
投票

我今天遇到了这个或类似的问题。

我曾经有一个UISearchBar,其accessibilityIdentifier设置为“MySearchBar”,而uisearchbar实例又设置为视图控制器的navigationItem.titleView。找到这样的搜索输入字段效果很好

let inputSearch = XCUIApplication().otherElements["MySearchBar"]

当 UISearchBar 像这样稍微移动时:

navigationItem.rightBarButtonItem = UIBarButtonItem(customView: searchBar)
上面查找元素的方法停止工作。

经过多次尝试和错误后,这行之有效:

我像这样设置搜索字段(UISearchBar 实例)的占位符:

searchBar.placeholder = "bla bla"

然后我可以在 UI 测试中找到该元素,如下所示:

let inputSearch = XCUIApplication().searchFields["bla bla"]

因此,在这种情况下,我停止对 UISearchBar 使用accessibilityIdentifier。这可能有国际化问题,但无论如何......

© www.soinside.com 2019 - 2024. All rights reserved.