Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 888
Apan Avatar asked Sep 22 '15 08:09

Apan


People also ask

What is UI testing in Xcode?

UI testing allows you to automate this procedure, so every time you update your code, Xcode will automatically test whether your app works correctly without you having to manually do it. When is UI testing used?

How to do xcuitests in Xcode?

The easiest option to get XCUITests done for your app is to use Xcode Test Recorder. With this, native UI components can be identified and specific interaction on those is done automatically based on the object’s characteristics (class, ID, attributes etc.).

Is Xcode storyboard good for test automation?

Frankly, Xcode Storyboard has numerous benefits for every app developer, but those also work as a great illustration for those who implement test automation for the app. Furthermore, if you select to start using Xcode, it’s always recommended to get a deep understanding of which pros and cons are provided with it for testing.

What is UI testing for Swift?

In this case, you might want to look at UI testing for Swift. UI testing allows you to automate this procedure, so every time you update your code, Xcode will automatically test whether your app works correctly without you having to manually do it. When is UI testing used?


2 Answers

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
like image 158
mike Avatar answered Sep 22 '22 13:09

mike


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().

like image 41
Apan Avatar answered Sep 19 '22 13:09

Apan