【问题标题】:XCode UITest will not give NSTextField keyboard focusXCode UITest 不会给 NSTextField 键盘焦点
【发布时间】:2018-01-02 06:13:01
【问题描述】:

所以我通过单击菜单项出现了一个窗口。通常第一个文本框会立即获得键盘焦点,但在 UITest 中,窗口中没有任何内容获得键盘焦点(没有焦点环)。这是有问题的,因为它阻止我使用textField.typeText("someText") 输入文本字段

我已经在窗口和文本框上尝试了.click() 以尝试使其获得焦点,但似乎没有任何东西可以使窗口或文本框成为焦点。 任何帮助将不胜感激

例子

MainMenu().menuItemForWindow.click() // Opens window
app.windows["windowTitle"].textFields["textBoxId"].click() // Clicks but field does not gain focus
app.windows["windowTitle"].textFields["textBoxId"].typeText("SomeText") // Is not typed into the textField

作为旁注,我已经验证我查询的所有元素确实存在。

编辑:

我已经通过向typeText() 发送垃圾邮件来让它工作,直到它用类似的东西改变给定文本框的值

if let oldValue = textbox.value as? String {
   var newValue: String? = oldValue
   while newValue == oldValue {
         textbox.typeText("a")
         newValue = textbox.value as? String
   }
   //If we get here then we have edited the text box
   textbox.typeText(XCUIDeleteKey) // Get rid of spam text
   //TYpe what I want
   //...
 }

但是这种方法很老套,除了启发式方法(大约 15 到 30 秒)外,我真的无法暂停,所以我希望有人能帮助解释一种更好的方法来确保专注于文本框,或者至少解释一下我最初做错了什么。任何帮助将不胜感激。

【问题讨论】:

  • 未经测试在模拟器中运行应用程序时是否会自动出现键盘?您是否尝试过在模拟器上切换硬件键盘设置?
  • 这是在 OSX 而不是 iOS 上

标签: swift xcode macos xctest uitest


【解决方案1】:

这里有两个可能的想法给你:

  1. 为您尝试与之交互的对象分配辅助功能 ID,并尝试通过辅助功能 ID 对其进行寻址。来自https://blog.metova.com/guide-xcode-ui-test

For element identification there is additionally elementMatchingPredicate(NSPredicate) and elementMatchingType(XCUIElementType, identifier: String?).

These are separate from containingPredicate(NSPredicate) and containingType(XCUIElementType, identifier: String?) which are checking the element for items inside it whereas the elementMatching... options are checking the values on the element itself. Finding the correct element will often include combinations of several query attributes.

  1. 在系统偏好设置中启用辅助功能 |此处描述的辅助功能:https://support.apple.com/en-us/HT204434,然后发送键盘命令来设置焦点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多