Xcode UI 测试 - UI 测试失败 - 点击搜索字段“取消”按钮时无法滚动到可见(通过 AX 操作)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33422681/
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
Xcode UI test - UI Testing Failure - Failed to scroll to visible (by AX action) when tap on Search field "Cancel' button
提问by Vinpai
I am trying to dismiss the search field by tapping 'Cancel' button in search bar.
我试图通过点击搜索栏中的“取消”按钮来关闭搜索字段。
The test case is failing to find the cancel button. It was working fine in Xcode 7.0.1
测试用例未能找到取消按钮。它在 Xcode 7.0.1 中运行良好
I have added predicate to wait for button to appear. The test case is failing when we tap of "cancel" button
我添加了谓词以等待按钮出现。当我们点击“取消”按钮时,测试用例失败
let button = app.buttons[“Cancel”]
let existsPredicate = NSPredicate(format: "exists == 1")
expectationForPredicate(existsPredicate, evaluatedWithObject: button, handler: nil)
waitForExpectationsWithTimeout(5, handler: nil)
button.tap() // Failing here
logs:
日志:
t = 7.21s Tap SearchField
t = 7.21s Wait for app to idle
t = 7.29s Find the SearchField
t = 7.29s Snapshot accessibility hierarchy for com.test.mail
t = 7.49s Find: Descendants matching type SearchField
t = 7.49s Find: Element at index 0
t = 7.49s Wait for app to idle
t = 7.55s Synthesize event
t = 7.84s Wait for app to idle
t = 8.97s Type '[email protected]' into
t = 8.97s Wait for app to idle
t = 9.03s Find the "Search" SearchField
t = 9.03s Snapshot accessibility hierarchy for com.test.mail
t = 9.35s Find: Descendants matching type SearchField
t = 9.35s Find: Element at index 0
t = 9.36s Wait for app to idle
t = 9.42s Synthesize event
t = 10.37s Wait for app to idle
t = 10.44s Check predicate `exists == 1` against object `"Cancel" Button`
t = 10.44s Snapshot accessibility hierarchy for com.test.mail
t = 10.58s Find: Descendants matching type Button
t = 10.58s Find: Elements matching predicate '"Cancel" IN identifiers'
t = 10.58s Tap "Cancel" Button
t = 10.58s Wait for app to idle
t = 10.64s Find the "Cancel" Button
t = 10.64s Snapshot accessibility hierarchy for com.test.mail
t = 10.78s Find: Descendants matching type Button
t = 10.78s Find: Elements matching predicate '"Cancel" IN identifiers'
t = 10.79s Wait for app to idle
t = 11.08s Synthesize event
t = 11.13s Scroll element to visible
t = 11.14s Assertion Failure: UI Testing Failure - Failed to scroll to visible (by AX action) Button 0x7f7fcaebde40: traits: 8589934593, {{353.0, 26.0}, {53.0, 30.0}}, label: 'Cancel', error: Error -25204 performing AXAction 2003
回答by Sandy
I guess here "Cancel" button returns false
for hittable
property, that is preventing it from tapping.
我想在这里“取消”按钮返回false
的hittable
属性,那就是防止它拍打。
If you see tap()
in documentation it says
如果您tap()
在文档中看到它说
/*!
* Sends a tap event to a hittable point computed for the element.
*/
- (void)tap;
It seems things are broken with XCode 7.1.To keep myself (and u too ;)) unblocked from these issues I wrote a extension on XCUIElement
that allows tap on element even if it is not hittable. Following can help you.
似乎 XCode 7.1 的事情已经被打破了。为了让我自己(和你也是 ;))不受这些问题的影响,我写了一个扩展XCUIElement
,即使它不是可点击的,也允许点击元素。以下可以帮到你。
/*Sends a tap event to a hittable/unhittable element.*/
extension XCUIElement {
func forceTapElement() {
if self.hittable {
self.tap()
}
else {
let coordinate: XCUICoordinate = self.coordinateWithNormalizedOffset(CGVectorMake(0.0, 0.0))
coordinate.tap()
}
}
}
Now you can call as
现在你可以调用
button.forceTapElement()
Update- For swift 3 use following:
更新- 对于 swift 3 使用以下内容:
extension XCUIElement {
func forceTapElement() {
if self.isHittable {
self.tap()
}
else {
let coordinate: XCUICoordinate = self.coordinate(withNormalizedOffset: CGVector(dx:0.0, dy:0.0))
coordinate.tap()
}
}
}
回答by dogsgod
For me, the root cause was that the objects I wanted to tap
对我来说,根本原因是我想点击的对象
- have been set to hidden (and back)
- have been removed and re-attached
- 已设置为隐藏(并返回)
- 已被删除并重新附加
In both cases the isAccessibilityElement
property was false
afterwards. Setting it back to true
fixed it.
在这两种情况下,isAccessibilityElement
财产都是false
后来的。将其重新设置以true
修复它。
回答by Jessedc
This question ranks well for Google queries around the term "Failed to scroll to visible (by AX action) Button". Given the age of the question I was inclined to think this was no longer an issue with the XCUITest framework as the accepted answer suggests.
对于围绕术语“无法滚动到可见(通过 AX 操作)按钮”的Google 查询,此问题的排名很好。鉴于问题的年龄,我倾向于认为这不再是 XCUITest 框架的问题,正如公认的答案所暗示的那样。
I found this issue was due to the XCElement
existing, but being hidden behind the software keyboard. The error is emitted by the framework since it is unable to scroll a view that exists into view to be tappable. In my case the button in question was behind the software keyboard sometimes.
我发现这个问题是由于XCElement
现有的,但隐藏在软件键盘后面。该错误是由框架发出的,因为它无法将存在的视图滚动到可点击的视图中。就我而言,有问题的按钮有时位于软件键盘后面。
I found the iOS Simulator's software keyboard may be toggled off in some cases (eg: on your machine) and toggled on in others (eg: on your CI). In my case I had toggled the software keyboard off on one machine, and by default it was toggled on on others.
我发现 iOS 模拟器的软件键盘在某些情况下可能会关闭(例如:在您的机器上)并在其他情况下(例如:在您的 CI 上)打开。在我的例子中,我在一台机器上关闭了软件键盘,默认情况下它在其他机器上被打开。
Solution: Dismiss the keyboard before attempting to tap buttons that may be behind it.
解决方案:在尝试点击键盘后面的按钮之前关闭键盘。
I found tapping somewhere that explicitly dismissed the keyboard before tapping on the button solved my problem in all environments.
我发现在点击按钮之前点击某个明确关闭键盘的地方解决了我在所有环境中的问题。
I added add some actions to get the current responder to resignFirstResponder. The views behind my text views will force the first responder to resign, so I tap somewhere just underneath the last text area.
我添加了一些操作来让当前响应者 resignFirstResponder。我的文本视图背后的视图将迫使第一响应者辞职,所以我点击最后一个文本区域下方的某个地方。
/// The keyboard may be up, dismiss it by tapping just below the password field
let pointBelowPassword = passwordSecureTextField.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 1))
pointBelowPassword.press(forDuration: 0.1)
回答by Nagaraj
回答by Aist? Stikliūt?
The workaround of Sandy seemed help for a while but then no more - I then changed it like this:
桑迪的解决方法似乎有帮助,但后来没有了 - 然后我像这样改变了它:
func waitAndForceTap(timeout: UInt32 = 5000) {
XCTAssert(waitForElement(timeout: timeout))
coordinate(withNormalizedOffset: CGVector(dx:0.5, dy:0.5)).tap()
}
Main point being that as the issue is that isHittable check throws an exception, I don't do this check at all and go straight for coordinates after the element is found.
主要的一点是,因为问题是 isHittable 检查会引发异常,所以我根本不进行此检查,而是在找到元素后直接查找坐标。
回答by Jeroen Vannevel
回答by PruitIgoe
In my case it was having a programmatically added UI element covering the button.
就我而言,它有一个以编程方式添加的 UI 元素覆盖按钮。
回答by fdelam
If you're using the AppCenter simulator to run the tests, you should make sure that you're running the tests on the same device version than your local simulator. I lost 3 days of work because of this.
如果您使用 AppCenter 模拟器运行测试,则应确保在与本地模拟器相同的设备版本上运行测试。因为这个,我失去了 3 天的工作。