Xcode UITest 有时找不到 XCUIElement 的属性 [英] Xcode UITest sometimes does not find property of XCUIElement

查看:58
本文介绍了Xcode UITest 有时找不到 XCUIElement 的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my UI tests, the frame property of some XCUIElement are found, but not of others.
The accessibility identifiers used below are set in storyboard, and app is initialised in setUp() as XCUIApplication().

这是故事板布局:

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

The two UI elements used in the test are Text Field and Add 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)")
}

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

The test stops at an error breakpoint at the second last line with the following message:

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

我不明白为什么应该为所有 XCUIElement 定义的 frame 属性出现在第一种情况中,而在第二种情况中没有.

I do not understand why the frame property, which should be defined for all XCUIElement, is found in the first case, but not in the second.

编辑

Oletha 在下面指出,我的常量 addShoppingItemTextField 是一个 XCUIElementQuery,当我尝试读取textField.
事实上,当程序在测试错误断点处停止并打印其描述时,我得到

Oletha pointed out below, that my constant addShoppingItemTextField is an XCUIElementQuery that should be resolved when I try to read the frame property of the textField.
Indeed, when the program stops at the test error breakpoint and I print its description, I get

Printing description of addShoppingItemTextField:
Query chain:
 →Find: Target Application 0x6080000a6ea0
  ↪︎Find: Descendants matching type TextField
    ↪︎Find: Elements matching predicate '"TextField" IN identifiers'  

但查找失败,尽管启用了辅助功能,并且辅助功能标识符设置为TextField:

But the find fails, although Accessibility is enabled, and the Accessibility Identifier is set to TextField:

我也插入了应用程序

print(textField.accessibilityIdentifier!)

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

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

As a workaround, I set the test to recording, and tapped the textField. This created code for the access to the textField. I then replaced let addShoppingItemTextField = app.textFields["TextField"] by (the right side was generated by the recording):

let addShoppingItemTextField = app.otherElements.containing(.navigationBar, identifier:"WatchNotOK")
.children(matching: .other).element.children(matching: .other).element
.children(matching: .other).element

现在代码可以正常工作了.

And now the code works without errors.

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

So it seems to me that the query for the accessibility identifier of a textField does not work correctly.

编辑 2

我放弃了:在没有更改故事板中的任何内容的情况下,测试现在停止并出现相同的测试错误(找不到匹配的 Find: Elements matching predicate '"WatchNotOK" IN identifiers')行 let navBarHeight = mainViewNavigationBar.frame.size.height.这确实一直有效.
这向我表明 Xcode UI 测试已损坏.

I give up: Without changing anything in the storyboard, the test now stops with the same test error (No matches found for Find: Elements matching predicate '"WatchNotOK" IN identifiers‘) at the line let navBarHeight = mainViewNavigationBar.frame.size.height. This did work all the time.
This indicates to me that Xcode UI tests are broken.

推荐答案

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

I contacted Apple, and they found my bug:
The view of my main view controller had its accessibility property set to true. This was wrong; it must be set to false:

可在 docsisAccessibilityElement 中找到解释:

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

一旦我将主视图的可访问性设置为 false,UI 测试就成功了.

As soon as I set accessibility of the main view to false, the UI test succeeded.

这篇关于Xcode UITest 有时找不到 XCUIElement 的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆