【问题标题】:Clicking keyboard 'Next' key using UIAutomation使用 UIAutomation 单击键盘“下一步”键
【发布时间】:2012-02-03 16:14:14
【问题描述】:

我的应用中有一个搜索字段,我已将此字段的键盘返回键类型设置为UIReturnKeyNext。我正在尝试编写一个 UIAutomation 测试,使用以下行单击键盘上的 Next 按钮:

UIATarget.localTarget().frontMostApp().mainWindow().keyboard().keys().firstWithName("next");

此调用失败,因为未找到名为“next”的键。我已经使用以下方法转储了我的应用程序中的所有元素:

UIATarget.localTarget().frontMostApp().logElementTree();

这表明键盘中确实有一个名为“next”的键,但不知何故,我尝试如上所示检索它仍然失败。但是,我可以使用此方法检索其他键(例如字母“u”的键)。这里有已知问题还是我做错了什么?

我尝试了其他变体但没有运气:

UIATarget.localTarget().frontMostApp().mainWindow().keyboard().elements()["next"];

这是我的 UIAKeyboard 中元素的屏幕截图:

【问题讨论】:

    标签: iphone ios instruments ui-automation ios-ui-automation


    【解决方案1】:

    Jelle 的方法对我有用。但如果有人需要,我也找到了另一种方法。

    XCUIApplication().keyboards.buttons["return"].tap()
    

    您可以在每个 UI 测试会话中将 XCUIApplication() 创建为单例。这种方法的关键在于您现在可以区分 returndone 以及其他变体,甚至可以检查它们是否存在。

    你可以做一些额外的事情,如下所示:

    extension UIReturnKeyType {
        public var title: String {
            switch self {
            case .next:
                return "Next"
            case .default:
                return "return"
            case .continue:
                return "Continue"
            case .done:
                return "Done"
            case .emergencyCall:
                return "Emergency call"
            case .go:
                return "Go"
            case .join:
                return "Join"
            case .route:
                return "Route"
            case .yahoo, .google, .search:
                return "Search"
            case .send:
                return "Send"
            }
        }
    }
    
    extension XCUIElement {
        func tap(button: UIReturnKeyType) {
            XCUIApplication().keyboards.buttons[button.title].tap()
        }
    }
    

    你可以像这样使用它:

    let usernameTextField = XCUIApplication().textFields["username"]
    usernameTextField.typeText("username")
    usernameTextField.tap(button: .next)
    

    【讨论】:

      【解决方案2】:

      对我来说,键盘不属于视图层次结构中的 mainWindow()。当您从顶层 logElementTree() 时,它与 mainWindow() 处于同一级别。所以,你想做的是:

      UIATarget.localTarget().frontMostApp().keyboard().buttons()["next"];
      

      当我尝试按下键盘上的“搜索”按钮时,这对我有用。

      【讨论】:

        【解决方案3】:

        如果你只是想点击它,并且你知道键盘有“下一个”作为“返回键”(在你的笔尖中定义),那么你可以使用这个:

        app.keyboard().typeString("\n");
        

        【讨论】:

          【解决方案4】:

          以下对我有用:

          UIATarget.localTarget().frontMostApp().mainWindow().keyboard().buttons().firstWi‌​thPredicate("name contains[c] 'next'"); 
          

          【讨论】:

            【解决方案5】:

            我没有要测试的示例,但由于“下一步”按钮是 UIAButton,而不是 UIAKey,您可以尝试:

            UIATarget.localTarget().frontMostApp().mainWindow().keyboard().buttons()["next"];
            

            如果不行,你也可以试试

            UIATarget.localTarget().frontMostApp().mainWindow().keyboard().buttons()[4];
            

            【讨论】:

            • 我都试过了,但都没有运气。不过感谢您的建议。
            • 朱利安,你的回答促使我再次尝试这个,并且使用按钮()函数确实导致了一个有效的解决方案。这对我有用: UIATarget.localTarget().frontMostApp().mainWindow().keyboard().buttons().firstWithPredicate("name contains[c] 'next'");在我看来,buttons()["next"] 应该有效,但我不知道为什么它没有,但上面的方法确实有效。感谢您的建议。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-11-25
            • 2014-08-30
            • 2023-04-04
            • 1970-01-01
            • 2012-07-18
            相关资源
            最近更新 更多