Xcode UITest 有时找不到 XCUIElement 的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48075816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:30:12  来源:igfitidea点击:

Xcode UITest sometimes does not find property of XCUIElement

xcodepropertiesxcode-ui-testingxcuitest

提问by Reinhard M?nner

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

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

Here is the storyboard layout:

这是故事板布局:

enter image description here

在此处输入图片说明

The two UI elements used in the test are Text Fieldand Add Button.

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

Here is the relevant code:

这是相关的代码:

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‘
)}

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

我不明白为什么在第一种情况下可以找到frame应该为 all 定义的属性,而XCUIElement在第二种情况下没有。

EDIT

编辑

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

Oletha指出下面,我常addShoppingItemTextFieldXCUIElementQuery当我尝试读取应当解决frame的财产textField
事实上,当程序在测试错误断点处停止并打印其描述时,我得到

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

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

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

enter image description here

在此处输入图片说明

I also inserted in the app

我也插入了应用程序

print(textField.accessibilityIdentifier!)

in viewDidLoad(), and it printed out TextFieldcorrectly.

in viewDidLoad(),并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):

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

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.

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

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

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

EDIT 2

编辑 2

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.

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

回答by Reinhard M?nner

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

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

enter image description here

在此处输入图片说明

The explanation is found in the docsto isAccessibilityElement:

文档中可以找到解释isAccessibilityElement

The default value for this property is false unless the receiver is a standard UIKit control, in which case the value is true.
Assistive applications can get information only about objects that are represented by accessibility elements. Therefore, if you implement a custom control or view that should be accessible to users with disabilities, set this property to true. The only exception to this practice is a view that merely serves as a container for other items that should be accessible. Such a view should implement the UIAccessibilityContainer protocol and set this property to false.

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

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

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

回答by Oletha

The problem is not that the frameproperty is not found on the element, it's that the element itself could not be found.

问题不是frame在元素上找不到属性,而是元素本身找不到。

Every XCUIElementis derived from an XCUIElementQuery. The first attempt to resolve the query is not, as you might expect, when you assign the value of addShoppingItemTextField, but the first time you access a property (other than exists) on addShoppingItemTextField.

每个XCUIElement都是从一个XCUIElementQuery. 如您所料,第一次尝试解析查询并不是在您分配 的值时addShoppingItemTextField,而是在您第一次访问 上的属性(除了exists)时addShoppingItemTextField

Therefore, when you try to access the frameproperty on the XCUIElementobject, the query for finding that element is resolved, but the element is not found - so you get the error saying 'No matches found...' on the line where you access frame. This can be a bit misleading, but the problem you're encountering is that the element could not be found. Try adjusting your query.

因此,当您尝试访问对象frame上的XCUIElement属性时,查找该元素的查询已解析,但未找到该元素 - 因此您在访问的行上收到错误消息“未找到匹配项...” frame。这可能有点误导,但您遇到的问题是找不到该元素。尝试调整您的查询。

回答by pallavi

In addition with above answers... I would like to add one point This may happen because the XCUIElement you are accessing is not available on screen. Suppose you are executing test case for login screen and simulator is launching with dashboard not with login screen. This happen with my case. I tried to logout and then execute test case. Error disappears

除了上述答案......我想补充一点 这可能是因为您正在访问XCUIElement 在屏幕上不可用。假设您正在为登录屏幕执行测试用例,并且模拟器正在使用仪表板而不是登录屏幕启动。这发生在我的情况下。我尝试注销然后执行测试用例。错误消失