单击键盘“下一步”;使用 UIAutomation 的 key

发布于 2024-12-25 15:46:39 字数 727 浏览 4 评论 0原文

我的应用程序中有一个搜索字段,并且我已将此字段的键盘返回键类型设置为 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 中元素的屏幕截图:

return key dump

I have a search field in my app and I have set the return key type of the keyboard for this field to UIReturnKeyNext. I am attempting to write a UIAutomation test that clicks the Next button on the keyboard using the following line:

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

This call is failing because the key with name 'next' is not being found. I have done a dump of all of the elements in my app using:

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

This reveals that there is indeed a key in the keyboard with name 'next', but somehow my attempt to retrieve it as show above still fails. I can however retrieve other keys (like the key for the letter 'u') using this method. Is there a known issue here or am I doing something wrong?

I've tried other variations with no luck:

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

Here is a screen capture of the elements in my UIAKeyboard:

return key dump

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

话少情深 2025-01-01 15:46:39

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

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

您可以在其中将 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()
    }
}

你可以像这样使用它:

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

Jelle's approach worked for me. But I also found an alternative way if anybody needed it.

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

Where you can create XCUIApplication() as a singleton on each UI Test session. The thing about this approach is you can now distinguish between return and done and other variants and even check for their existence.

You can go extra and do something like following:

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()
    }
}

And you can use it like:

let usernameTextField = XCUIApplication().textFields["username"]
usernameTextField.typeText("username")
usernameTextField.tap(button: .next)
我是男神闪亮亮 2025-01-01 15:46:39

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

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

If you just want to click it, and you know the keyboard has "next" as "Return key" (defined in your nib), then you can use this:

app.keyboard().typeString("\n");
写给空气的情书 2025-01-01 15:46:39

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

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

如果它不起作用,您也可以尝试

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

I dont't have an example to test, but as the "Next" button is an UIAButton, and not an UIAKey you could try :

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

If it doesn't work, you can also try

UIATarget.localTarget().frontMostApp().mainWindow().keyboard().buttons()[4];
完美的未来在梦里 2025-01-01 15:46:39

以下对我有用:

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

The following works for me:

UIATarget.localTarget().frontMostApp().mainWindow().keyboard().buttons().firstWi‌​thPredicate("name contains[c] 'next'"); 
久光 2025-01-01 15:46:39

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

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

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

For me, keyboard does not fall under mainWindow() in the View Hierarchy. It is at the same level as mainWindow() when you logElementTree() from top level. So, what you want to do is:

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

This worked for me when I was trying to press the "Search" button on keyboard.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文