Xcode UI Testing - typing text with typeText() method and autocorrection
Asked Answered
A

5

22

I've got a test like below:

let navnTextField = app.textFields["First Name"]
let name = "Henrik"
navnTextField.tap()
navnTextField.typeText("Henrik")
XCTAssertEqual(navnTextField.value as? String, name)

Problem is that by default my iPhone Simulator has got Polish keyboard because of the system settings and "Henrik" is automatically changed into "ha" by autocorrect.

Simple solution is to remove Polish keyboard from the iOS Settings. This solution however is not solving the problem because iPhone Simulator can be reset and then test will fail again.

Is there any way to setup autocorrect before test case or other way to input text to text field.

Amatol answered 22/9, 2015 at 8:26 Comment(0)
A
17

There is a workaround to use UIPasteboard to provide input text:

let navnTextField = app.textFields["First name"]
navnTextField.tap()
UIPasteboard.generalPasteboard().string = "Henrik"
navnTextField.doubleTap()
app.menuItems.elementBoundByIndex(0).tap()
XCTAssertEqual(navnTextField.value as? String, name)

You can check link with description as a workaround for secure input in GM

Edit

For better readability instead app.menuItems.elementBoundByIndex(0).tap() you can do app.menuItems["Paste"].tap().

Amatol answered 22/9, 2015 at 8:42 Comment(0)
C
29

Here's a small extension on XCUIElement to accomplish this

extension XCUIElement {
    // The following is a workaround for inputting text in the 
    //simulator when the keyboard is hidden
    func setText(text: String, application: XCUIApplication) {
        UIPasteboard.generalPasteboard().string = text
        doubleTap()
        application.menuItems["Paste"].tap()
    }
}

It can be used like this

let app = XCUIApplication()
let enterNameTextField =  app.otherElements.textFields["Enter Name"]
enterNameTextField.tap()
enterNameTextField.setText("John Doe", app)
  • Credit goes to @Apan for the implementation
Checkpoint answered 7/1, 2016 at 20:25 Comment(2)
doubleTap() could be a problem if text is already in this textfield, cause ti will select part of textUigur
This works for me only if I insert arbitrary delays between the tap, doubleTap, and paste. I guess it might be a performance thing? My simulator is running too slowly to keep up with all of the taps?Serialize
F
19

Currently using Swift 4 on Xcode 10 you can now use typeText(String) like this let app = XCUIApplication() let usernameTextField = app.textFields["Username"] usernameTextField.typeText("Caseyp")

Ford answered 15/11, 2018 at 19:34 Comment(2)
This is a more appropriate answer considering the state of the IDE as of Xcode 11.....Prickly
for Xcode 13.3.1, we have to focus on the field, before we execute typeTest. Thus add usernameTextField.tap() before the last line.Incitement
A
17

There is a workaround to use UIPasteboard to provide input text:

let navnTextField = app.textFields["First name"]
navnTextField.tap()
UIPasteboard.generalPasteboard().string = "Henrik"
navnTextField.doubleTap()
app.menuItems.elementBoundByIndex(0).tap()
XCTAssertEqual(navnTextField.value as? String, name)

You can check link with description as a workaround for secure input in GM

Edit

For better readability instead app.menuItems.elementBoundByIndex(0).tap() you can do app.menuItems["Paste"].tap().

Amatol answered 22/9, 2015 at 8:42 Comment(0)
D
3

For swift v3 need use new sintax (answer by @mike):

extension XCUIElement {
    func setText(text: String?, application: XCUIApplication) {
        tap()
        UIPasteboard.general.string = text
        doubleTap()
        application.menuItems.element(boundBy: 0).tap()
    }
}

and use it:

let app = XCUIApplication()
let enterNameTextField =  app.otherElements.textFields["Enter Name"]
enterNameTextField.tap()
enterNameTextField.setText(text: "John Doe", application: app)
Duna answered 19/6, 2018 at 10:48 Comment(0)
H
2

Tweaked:

  1. so extension is on application which makes a bit more sense to me
  2. the existing field contents are emptied

code:

extension XCUIApplication {
      // The following is a workaround for inputting text in the
      //simulator when the keyboard is hidden
      func setText(_ text: String, on element: XCUIElement?) {
        if let element = element {
        UIPasteboard.general.string = text
        element.doubleTap()
        self.menuItems["Select All"].tap()
        self.menuItems["Paste"].tap()
        }
      }
    }

Run with:

self.app?.setText("Lo", on: self.app?.textFields.firstMatch)
Horrify answered 17/10, 2018 at 8:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.