【问题标题】:Unable to pick image from gallery XCUITest无法从图库 XCUITest 中选择图像
【发布时间】:2020-09-30 09:22:54
【问题描述】:

我使用 XCUITest 测试一个允许用户通过从图库中选择照片来选择头像的应用。当我点击打开画廊窗口的按钮时,我可以在 debugDescription 中看到元素。有一个表格,其中包含带有照片的文件夹。问题是当我第一次点击任何单元格时,测试失败并出现错误:

Assertion Failure: UserProfileAndSettingsTests.swift:434: Failed to get matching snapshot: No matches found for Element at index 2 from input {(
    Table
)}".  

如果我在那里设置断点,那么当我第二次点击任何单元格时,它就会起作用。

命令如下:

XCUIApplication().tables.element(boundBy: 2).cells.element(boundBy: 1).tap()

如果在命令之前我输入了这一行:XCUIApplication().tables.element(boundBy: 2).cells.element(boundBy: 1),它不会失败。尝试tap() 时失败。

【问题讨论】:

    标签: ios swift xcode xcuitest


    【解决方案1】:

    看起来像是时间问题。熟悉XCUIElement 类,特别是这个:

    /** Waits the specified amount of time for the element's exist property to be true and returns false if the timeout expires without the element coming into existence. */
        open func waitForExistence(timeout: TimeInterval) -> Bool
    

    你应该可以做这样的事情:

    let element = XCUIApplication().tables.element(boundBy: 2).cells.element(boundBy: 1)
    if element.waitForExistence(timeout: 2) {
       element.tap()
    }
    

    我建议与这种方法以及其他类似的方法和期望交朋友,以便能够做这样方便的事情(self 在这种情况下是一个 XCTestCase):

    func waitUntilTappable(_ element:XCUIElement, timeout: TimeInterval = 2) {
       let tappableExpectation = self.expectation(for: NSPredicate(format: "isHittable == true"),
                                                            evaluatedWith: element)
       self.wait(for: [tappableExpectation], timeout: timeout.rawValue)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-31
      • 2012-08-01
      • 1970-01-01
      相关资源
      最近更新 更多