Xcode UITest有时找不到XCUIElement的属性

Rei*_*ner 5 xcode properties xcode-ui-testing xcuitest

在我的UI测试中,XCUIElement找到了一些框架属性,但没有找到其他框架属性.
下面使用的可访问性标识符在storyboard中设置,并appsetUp()as中初始化XCUIApplication().

这是故事板布局:

在此输入图像描述

测试中使用的两个UI元素是Text FieldAdd Button.

这是相关代码:

func test() {
    // given
    let mainViewNavigationBar = app.navigationBars[„NavBar“]
    let navBarHeight = mainViewNavigationBar.frame.size.height
    print("navBarHeight: \(navBarHeight)") // is printed out correctly

    let addShoppingItemTextField = app.textFields["TextField"]
    let textFieldHeight = addShoppingItemTextField.frame.size.height // error breakpoint here
    print("textFieldHeight: \(textFieldHeight)")
}
Run Code Online (Sandbox Code Playgroud)

测试在第二行的错误断点处停止,并显示以下消息:

No matches found for Find: Descendants matching type TextField from input {(
    Application, 0x60000019f070, pid: 13114, label: ‚xxx‘
)}
Run Code Online (Sandbox Code Playgroud)

我不明白为什么frame应该为所有人定义的属性XCUIElement在第一种情况下找到,而在第二种情况下找不到.

编辑

Oletha在下面指出,我的常数addShoppingItemTextField是一个XCUIElementQuery应该在我尝试阅读该frame属性时解决的常量textField.
实际上,当程序在测试错误断点处停止并且我打印其描述时,我得到了

Printing description of addShoppingItemTextField:
Query chain:
 ?Find: Target Application 0x6080000a6ea0
  ??Find: Descendants matching type TextField
    ??Find: Elements matching predicate '"TextField" IN identifiers'  
Run Code Online (Sandbox Code Playgroud)

但是,虽然启用了"辅助功能",但"查找"失败,并且"辅助功能标识符"设置为TextField:

在此输入图像描述

我也插入了应用程序

print(textField.accessibilityIdentifier!)
Run Code Online (Sandbox Code Playgroud)

viewDidLoad(),它打印出来TextField正确.

作为一种解决方法,我将测试设置为录制,然后点击textField.这创建了用于访问的代码textField.然后我替换了let addShoppingItemTextField = app.textFields["TextField"]by(右侧是由录音生成的):

let addShoppingItemTextField = app.otherElements.containing(.navigationBar, identifier:"WatchNotOK")
.children(matching: .other).element.children(matching: .other).element
.children(matching: .other).element
Run Code Online (Sandbox Code Playgroud)

现在代码可以正常运行.

因此在我看来,对a的可访问性标识符的查询textField无法正常工作.

编辑2

我放弃了:在没有改变故事板中的任何内容的情况下,测试现在停止了相同的测试错误(找不到与匹配谓词的元素匹配谓词'"WatchNotOK"IN标识符')let navBarHeight = mainViewNavigationBar.frame.size.height.这确实有效.
这向我表明Xcode UI测试已被破坏.

Rei*_*ner 17

我联系了Apple,他们发现了我的错误:
我的主视图控制器的视图已将其accessibility属性设置为true.这是错的; 它必须设置为false:

在此输入图像描述

【解说】在发现文档isAccessibilityElement:

除非接收方是标准的UIKit控件,否则此属性的默认值为false,在这种情况下值为true.
辅助应用程序只能获取有关由辅助功能元素表示的对象的信息.因此,如果您实现了残障用户可以访问的自定义控件或视图,请将此属性设置为true.这种做法的唯一例外是一个视图,它只是作为应该可访问的其他项目的容器.这样的视图应该实现UIAccessibilityContainer协议并将此属性设置为false.

只要我将主视图的可访问性设置为false,UI测试就会成功.