问题
XCUIApplication *app = [[XCUIApplication alloc] init];
[app.buttons[@"Committee"] tap];
[app.buttons[@"Login"] tap];
[app.buttons[@"Add Presenter"] tap];
XCUIElement *nameTextField = app/*@START_MENU_TOKEN@*/.textFields[@"Name"]/*[[".scrollViews.textFields[@\"Name\"]",".textFields[@\"Name\"]"],[[[-1,1],[-1,0]]],[0]]@END_MENU_TOKEN@*/;
[nameTextField tap];
[nameTextField typeText:@"A"];
XCUIElement *topicTextField = app/*@START_MENU_TOKEN@*/.textFields[@"Topic"]/*[[".scrollViews.textFields[@\"Topic\"]",".textFields[@\"Topic\"]"],[[[-1,1],[-1,0]]],[0]]@END_MENU_TOKEN@*/;
[topicTextField tap];
[topicTextField tap];
my error is when run the UI test I receive UI Testing Failure
Neither element nor any descendant has keyboard focus.
Attributes: TextField however the test above passed I have attached a screenshot of what exactly failing enter image description here
回答1:
I faced this same problem with Xcode 7.0.1 and was able to resolve it by turning off the connected hardware keyboard in the simulator settings. Hardware -> Keyboard -> Connect Hardware Keyboard (uncheck this). link
回答2:
Works for me for native screens:
extension XCUIElement {
func typeTextAlt(_ text: String) {
// Solution for `Neither element nor any descendant has keyboard focus.`
if !(self.value(forKey: "hasKeyboardFocus") as? Bool ?? false) {
XCUIDevice.shared.press(XCUIDevice.Button.home)
XCUIApplication().activate()
}
self.typeText(text)
}
}
来源:https://stackoverflow.com/questions/34115375/xcode-ui-testing-error-keyboard