ios - UI 测试失败 - 未找到 MenuItems 的匹配项

标签 ios swift ios9 xcode-ui-testing

错误:UI 测试失败 - 找不到 MenuItems 的匹配项

  UIPasteboard.generalPasteboard().string = constant.password

  enterPasswordTextField.doubleTap()

  // Tap the Paste button to input the password
  app.menuItems["Paste"].tap()

我已经在我的测试用例中编写了这段代码,我什至尝试过禁用模拟器的硬件键盘,但 doubletap() 函数仍然无法正常工作。

我正在使用 Xcode 7.3.1。

最佳答案

您需要为 menuItem 的出现添加预期。请注意,doubleTap() 只有在文本字段已获得焦点时才会起作用,因此您应该在双击之前添加一个额外的 tap()

    let app = XCUIApplication()

    UIPasteboard.generalPasteboard().string = "hello"

    let enterPasswordTextField = app.textFields["textField"]
    enterPasswordTextField.tap()

    expectationForPredicate(NSPredicate(format: "count > 0"), evaluatedWithObject: app.menuItems, handler: nil)
    enterPasswordTextField.doubleTap()
    waitForExpectationsWithTimeout(10.0, handler: nil)

    app.menuItems["Paste"].tap()

请记住,通过本地化测试访问菜单项可能不是最好的主意,因为它可能在非英语设备上失败。因此,您应该使用最复杂的逻辑来确定粘贴菜单项的位置。对于初学者,在文本字段为空的过于简单化的假设中,您可能需要替换

app.menuItems["Paste"].tap()

app.menuItems.elementsBoundByIndex(2).tap()

关于ios - UI 测试失败 - 未找到 MenuItems 的匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38310256/

相关文章:

ios - 如何添加圆到矩形? UIBezierPath

ios - 静态 TableViewCell 的 TextView

ios - 无法在 Firebase 中获取节点的值

ios - 调用的 Swift 结果未使用

ios - 如何将推送通知 token 存储到外部数据库中 - iOS 10、Swift 3

ios - 以编程方式创建 iOS UIStackView

ios - 如何在 iOS 9 中打开电话拨号器?

ios - 使用 NSFetchedResultsController 时如何在每个部分获取一条记录

ios - 2048 转换为 BOOL 返回 0

ios - 在 View Controller 中隐藏/显示控件