Skip to content

Instantly share code, notes, and snippets.

@pilot34
Created April 24, 2018 09:40
Show Gist options
  • Save pilot34/16558f2e1ceb0263434c446ed96fe81f to your computer and use it in GitHub Desktop.
Save pilot34/16558f2e1ceb0263434c446ed96fe81f to your computer and use it in GitHub Desktop.
func typeText(_ text: String, toWebViewField element: XCUIElement) {
// xcode has bug, so we cannot directly access webViews XCUIElements
// as workaround we can check debugDesciption, find frame, tap by coordinate,
// and then paste text there
guard let coordBeforeTap = coordinate(forWebViewElement: element) else {
XCTFail("no element \(element)")
return
}
// "typeText" doesn't work, so we paste text
// first tap to activate field
UIPasteboard.general.string = text
coordBeforeTap.tap()
// wait for keyboard to appear
wait(forWebViewElement: XCUIApplication().keyboards.firstMatch)
// after tap coordinate can change
guard let coordAfterTap = coordinate(forWebViewElement: element) else {
XCTFail("no element \(element)")
return
}
// tap one more time for "paste" menu
coordAfterTap.press(forDuration: 1)
wait(forElement: XCUIApplication().menuItems["Paste"])
if XCUIApplication().menuItems["Select All"].exists {
// if there was a text - remove it, by pressing Select All and Cut
XCUIApplication().menuItems["Select All"].tap()
XCUIApplication().menuItems["Cut"].tap()
// close keyboard
XCUIApplication().toolbars.buttons["Done"].tap()
// call this method one more time
typeText(text, toWebViewField: element)
return
}
XCUIApplication().menuItems["Paste"].tap()
// close keyboard
XCUIApplication().toolbars.buttons["Done"].tap()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment