2017-07-26 185 views
0

所以我有一个窗口通过点击一个菜单项出现。通常情况下,第一个文本框会获得即时的键盘焦点,但在UITest中,窗口中没有任何东西获得键盘焦点(没有聚焦环)。这是问题,因为它阻止我输入到文本字段与textField.typeText("someText")XCode UITest不会给NSTextField键盘焦点

我试图.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-30s)之外,我真的想把时间放在一边,所以我希望有人能够帮助解释一种更好的方式来确保对文本框的关注,或者至少解释我最初做错了什么。任何帮助将不胜感激。

+0

当您在没有测试的模拟器中运行应用程序时,键盘是否自动出现?您是否尝试过在模拟器上切换硬件键盘设置? – Oletha

+0

这是在OSX而不是iOS上 –

回答

0

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

  1. 指定的无障碍标识您试图与之交互的对象,并尝试通过辅助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.

  • 在系统预置使能可访问性|这里描述的可访问性:https://support.apple.com/en-us/HT204434然后发送键盘命令来设置焦点。