Arun Kumar
Arun Kumar

Reputation: 139

How to clear value in textfield using XCode UI tests

Performing automation using XCode UI tests. I have a scenario where I need to clear the value in the textfield and enter any new text. I am able to access the text but couldn't able to clear it.

app.alerts["abc"].collectionViews.children(matching: .other).element.children(matching: .other).element.children(matching: .other).element.children(matching: .other).element.children(matching: .other).element(boundBy: 1).children(matching: .textField).element.value

Above part of the code gives me the value in the textfield. Is there any way I can clear the value in the textfield using Swift 3

Upvotes: 1

Views: 3916

Answers (4)

Tony Thomas
Tony Thomas

Reputation: 1005

    let textField = app.textFields.element(boundBy: 0)
    textField.tap()
    //Select all text
    textField.tap(withNumberOfTaps: 3, numberOfTouches: 1)
    textField.typeText("2139218938")
    app.buttons["Login"].tap()

Upvotes: 0

divya d
divya d

Reputation: 165

Try this,

let app = XCUIApplication()
let userName = app.textFields["UserName - Login"] //Accessible label identifier
userName.tap()
userName.typeText("hello")

Upvotes: 0

Aaron Sofaer
Aaron Sofaer

Reputation: 726

This is the bit of code that I use for clearing fields before entering information in them in my UI Tests. If there's a Clear Text button it uses that; otherwise, it enters a string of backspaces sufficient to clear the contents of the field.

    if app.buttons["Clear text"].exists {
        app.buttons["Clear text"].tap()
    } else if let fieldValue = field.value as? String {
        // If there's any content in the field, delete it
        var deleteString: String = ""
        for _ in fieldValue.characters {
            deleteString += "\u{8}"
        }
            field.typeText(deleteString)
    }

Upvotes: 3

Alexey Matjuk
Alexey Matjuk

Reputation: 4301

value of XCUIElement is readonly property and there is no way to change it programmatically (only manually using UI interaction). Try

textFieldElement.buttons["Clear text"].tap()

Upvotes: 0

Related Questions