Xcode UI测试 - UI测试失败 - 点击搜索字段“取消”按钮时无法滚动到可见(通过AX操作)

时间:2015-10-29 19:10:24

标签: xcode xcode7 xcode-ui-testing

我正试图通过点按搜索栏中的“取消”按钮来关闭搜索字段。

测试用例未能找到取消按钮。它在Xcode 7.0.1中运行良好

我添加谓词等待按钮出现。当我们点击“取消”按钮时,测试用例失败

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

日志

    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 'vinayak@xmd.net' 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

9 个答案:

答案 0 :(得分:111)

我猜这里"取消"对于false属性,按钮返回hittable,这会阻止其点击。

如果您在文档中看到tap()

/*!
 * Sends a tap event to a hittable point computed for the element.
 */
- (void)tap;

似乎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()
        }
    }
}

现在你可以打电话给

button.forceTapElement()

更新 - 对于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()
        }
    }
}

答案 1 :(得分:8)

对我来说,根本原因是我想点击的对象

  • 已设置为隐藏(并返回)
  • 已被删除并重新附加

在这两种情况下,isAccessibilityElement属性之后都是false。将其重新设置为true修复它。

答案 2 :(得分:2)

我发现此问题是由于XCElement存在,但被隐藏在软件键盘后面。该错误由框架发出,因为它无法将存在于视图中的视图滚动到可点击的视图中。就我而言,该按钮有时在软件键盘有时后面。

我发现iOS模拟器软件键盘在某些情况下(例如在您的计算机上)可能已关闭,而在其他情况下(例如在您的CI上)可能已打开。就我而言,我在一台机器上关闭了软件键盘,而默认情况下却在另一台机器上打开了。

在尝试轻按键盘后面的按钮之前,先将其关闭。

我发现在某个按钮上敲击明确消除了键盘的位置,然后在所有环境下都解决了我的问题。

我添加了一些操作,以使当前响应者重新登录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)
  

对于Google查询,该查询在“无法滚动到可见的(通过AX操作)按钮” 一词中排名很好。鉴于问题的年代久远,我倾向于认为这已不再是XCUITest框架的问题,正如公认的答案所暗示的那样。经过反复试验,我找到了上面的解决方案。

答案 3 :(得分:1)

请检查元素的特性,我遇到了与TableViewSectionHeader相同的问题,我试图点击但是它在每一点都失败

enter image description here

答案 4 :(得分:1)

Sandy的变通办法似乎在一段时间内有所帮助,但此后不再有用-然后我像这样更改它:

func waitAndForceTap(timeout: UInt32 = 5000) {
    XCTAssert(waitForElement(timeout: timeout))
    coordinate(withNormalizedOffset: CGVector(dx:0.5, dy:0.5)).tap()
}

要点是,因为问题在于isHittable检查会引发异常,所以我根本不执行此检查,而是在找到元素后直接查找坐标。

答案 5 :(得分:0)

就我而言,它是通过编程方式添加的UI元素来覆盖按钮。

答案 6 :(得分:0)

如果您使用AppCenter模拟器运行测试,则应确保在与本地模拟器相同的设备版本上运行测试。因此,我失去了3天的工作。

答案 7 :(得分:0)

本着可以覆盖您元素的精神,我将RN调试器部分覆盖在图标上方:

enter image description here

答案 8 :(得分:0)

试试这个:

if !button.isHittable {
     let coordinate: XCUICoordinate = button.coordinate(withNormalizedOffset: CGVector(dx:0.0, dy:0.0))
     coordinate.tap()
}