This is my case:
let passwordSecureTextField = app.secureTextFields[\"password\"]
passwordSecureTextField.tap()
passwordSecureTextField.typeText(\"wrong_pass
I ran into this issue and was able to fix it in my scenario by taking the solution posted by @AlexDenisov and adding it to my pre-actions for run & test.
I have written a small extension (Swift) which works perfect for me. Here is the code:
extension XCTestCase {
func tapElementAndWaitForKeyboardToAppear(element: XCUIElement) {
let keyboard = XCUIApplication().keyboards.element
while (true) {
element.tap()
if keyboard.exists {
break;
}
NSRunLoop.currentRunLoop().runUntilDate(NSDate(timeIntervalSinceNow: 0.5))
}
}
}
The main idea is to keep tapping an element (text field) before the keyboard is presented.
What fixed this problem for me was adding a 1 second sleep:
let textField = app.textFields["identifier"]
textField.tap()
sleep(1)
textField.typeText(text)
Stanislav has the right idea.
In a team environment, you need something that will automatically work. I've come up with a fix here on my blog.
Basically you just paste:
UIPasteboard.generalPasteboard().string = "Their password"
let passwordSecureTextField = app.secureTextFields["password"]
passwordSecureTextField.pressForDuration(1.1)
app.menuItems["Paste"].tap()
Your first line is just a query definition, which doesn't mean that passwordSecureTextField
would actually exist.
Your second line will dynamically execute the query and try to (re)bind the query to UI element. You should put a breakpoint on it and verify that one and only one element is found. Or just use an assert:
XCTAssertFalse(passwordSecureTextField.exists);
Otherwise it looks ok, tap
should force keyboard visible and then typeText
should just work. Error log should tell you more info.
Another cause of this error is if there is a parent view of the text field in which you are trying to enter text that is set as an accessibility element (view.isAccessibilityElement = true
). In this case, XCTest is not able to get a handle on the subview to enter the text and returns the error.
UI Testing Failure - Neither element nor any descendant has keyboard focus.
It isn't that no element has focus (as you can often see the keyboard up and blinking cursor in the UITextField), it is just that no element it can reach has focus. I ran into this when attempting to enter text in a UISearchBar. The search bar itself is not the text field, when setting it as an accessibility element, access to the underlying UITextField was blocked. To resolve this, searchBar.accessibilityIdentifier = "My Identifier"
was set on the UISearchBar
however the isAccessibilityElement
was not set to true
. After this, test code of the form:
app.otherElements["My Identifier"].tap()
app.otherElements["My Identifier"].typeText("sample text")
Works