Xcode UI测试 - UI测试失败 - 点击搜索字段"取消"按钮时无法滚动显示(通过AX操作)

Vin*_*pai 70 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
Run Code Online (Sandbox Code Playgroud)

日志:

    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
Run Code Online (Sandbox Code Playgroud)

San*_*ndy 118

我想在这里"取消"按钮返回falsehittable属性,那就是防止它拍打.

如果你tap()在文档中看到它说

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

似乎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()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在你可以打电话给

button.forceTapElement()
Run Code Online (Sandbox Code Playgroud)

更新 - 对于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()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 在我的情况下,当水龙头位于视图的中心时,它可以工作.将偏移更改为(0.5,0.5).`let coordinate:XCUICoordinate = self.coordinateWithNormalizedOffset(CGVectorMake(0.5,0.5))` (6认同)

dog*_*god 9

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

  • 已被设置为隐藏(和后面)
  • 已被删除并重新附加

在这两种情况下,isAccessibilityElement财产都是false事后的.将其设置回来true修复它.

  • 这正是我的问题.奇怪的是,它开始在iOS 11上发生(它在iOS 10上运行良好). (3认同)

Jes*_*edc 5

这个问题在围绕“无法滚动到可见(通过 AX 操作)按钮”一词的 Google 查询中排名很好。鉴于问题的年龄,我倾向于认为这不再是 XCUITest 框架的问题,正如公认的答案所暗示的那样。

我发现这个问题是由于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)
Run Code Online (Sandbox Code Playgroud)