Octavio Rojas
Octavio Rojas

Reputation: 187

is there a way to count the characters of an XCUIElement Textfield?

Using the UITextfield class in Swift you can do a function like this:

    private func signIn() {

    if emailTextField.text?.characters.count == 0 {
        invalidEmailLabel.text = NSLocalizedString("Required", comment: "Required text field")
        Constants.FormValidation.showValidationLabel(invalidEmailLabel)
        return
    }

Is there any way to do the same or something similar with XCUIElements?

I would like to do something like this:

func testExample() {

let app = XCUIApplication()
let emailTextField = app.textFields["username"]

if emailTextField.text.characters.count == "0"{
let emailTextField = app.textFields["username"]
    emailTextField.tap()
    emailTextField.typeText("example")

let passwordTextField = app.secureTextFields["password"]
    passwordTextField.tap()
    passwordTextField.typeText("whatever")
    app.buttons["Done"].tap()

}

if emailTextField.text.characters.count != "0"{

 let passwordTextField = app.secureTextFields["password"]
    passwordTextField.tap()
    passwordTextField.typeText("whatever")
    app.buttons["Done"].tap()
}

How could it be done so that it works?

Thanks.

Upvotes: 0

Views: 189

Answers (1)

Oletha
Oletha

Reputation: 7649

You can count the characters of a text field element by doing:

let emailValue = emailTextField.value as! String
if emailValue.characters.count == 0 {
    // do something
}

Upvotes: 0

Related Questions