使用UIAutomation单击键盘'Next'键

kod*_*die 7 iphone instruments ui-automation ios ios-ui-automation

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

UIATarget.localTarget().frontMostApp().mainWindow().keyboard().keys().firstWithName("next");
Run Code Online (Sandbox Code Playgroud)

此调用失败,因为找不到名为"next"的键.我使用以下方法对我的应用中的所有元素进行了转储:

UIATarget.localTarget().frontMostApp().logElementTree();
Run Code Online (Sandbox Code Playgroud)

这表明键盘上确实有一个名为"next"的键,但不知怎的,我上面显示的检索它的尝试仍然失败.然而,我可以使用此方法检索其他键(如字母'u'的键).这里有一个已知的问题还是我做错了什么?

我尝试了其他变种而没有运气:

UIATarget.localTarget().frontMostApp().mainWindow().keyboard().elements()["next"];
Run Code Online (Sandbox Code Playgroud)

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

返回密钥转储

man*_*man 7

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

XCUIApplication().keyboards.buttons["return"].tap()
Run Code Online (Sandbox Code Playgroud)

您可以在其中创建 XCUIApplication() 作为每个 UI 测试会话的单例。这种方法的问题是您现在可以区分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()
    }
}
Run Code Online (Sandbox Code Playgroud)

你可以像这样使用它:

let usernameTextField = XCUIApplication().textFields["username"]
usernameTextField.typeText("username")
usernameTextField.tap(button: .next)
Run Code Online (Sandbox Code Playgroud)


Jel*_*lle 6

如果您只想单击它,并且您知道键盘将“下一步”作为“返回键”(在您的笔尖中定义),那么您可以使用以下命令:

app.keyboard().typeString("\n");
Run Code Online (Sandbox Code Playgroud)


kod*_*die 2

以下对我有用:

\n\n
UIATarget.localTarget().frontMostApp().mainWindow().keyboard().buttons().firstWi\xe2\x80\x8c\xe2\x80\x8bthPredicate("name contains[c] \'next\'"); \n
Run Code Online (Sandbox Code Playgroud)\n