22

I am using the UI Test Case class integrated in Xcode and XCTest to test app UI. I want to test something like this:

app = XCUIApplication()
let textField = app.textFields["Apple"]
textField.typeText("text_user_typed_in")
XCTAssertEqual(textField.text, "text_user_typed_in")

I've tried the textField.value as! String method; it does not work. I've also tried using the new async method with expectationForPredicate(), and it will result in a timeout.

Any idea how to do this or validation of this kind is not possible with UI Test and I could only write black-box tests?

Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
leoluo
  • 249
  • 1
  • 2
  • 8
  • Thx @Charles A. for pointing out that the problem is the textField does not exist. I am confused because I call the typeText method and I can see that the text is being input into the textField. – leoluo Nov 18 '15 at 22:58

3 Answers3

43

I use this code and it works fine:

textField.typeText("value")
XCTAssertEqual(textField.value as! String, "value")

If you're doing something similar and it isn't functioning, I would check to make sure that your textField element actually exists:

XCTAssertTrue(textField.exists, "Text field doesn't exist")
textField.typeText("value")
XCTAssertEqual(textField.value as! String, "value", "Text field value is not correct")
Charles A.
  • 10,685
  • 1
  • 42
  • 39
  • Yeah you are right. The textField does not exist. But I don't understand this: I call the typeText on textField and I can see that the text is being input into the textField. – leoluo Nov 18 '15 at 22:56
  • Should I use the async method on this? – leoluo Nov 18 '15 at 22:56
  • Is the initial text "Apple"? The default behavior is probably that the accessibility label of the field is its contents, so the query fails the second time you try and resolve an element. – Charles A. Nov 18 '15 at 22:57
  • You should set an accessibility identifier on your text field, and look for it with that instead of "Apple" so it is consistent. Every time you access an element it attempts to resolve it again. – Charles A. Nov 18 '15 at 22:58
  • 1
    I understand what you are saying, and I find a workaround with elementBoundByIndex(). Thx for the help – leoluo Nov 19 '15 at 00:21
  • @leoluo you should add your comment in answer section. – Urmi Dec 08 '17 at 21:09
  • @leoluo If you know the index of your textField, you can do something like this: `let textField = XCUIApplication().textFields.element(boundBy: index)`. It would be easier to determine the text field index of a more specific element than the root `XCUIApplication` instance, of course. – Charles A. Dec 09 '17 at 01:00
3

Swhift 4.2. You need to clear an existing value in textField and paste new value.

let app = XCUIApplication()
let textField = app.textFields["yourTextFieldValue"]
textField.tap()
textField.clearText(andReplaceWith: "VALUE")
XCTAssertEqual(textField.value as! String, "VALUE", "Text field value is not correct")

where clearText is a method of XCUIElement extension:

extension XCUIElement {
    func clearText(andReplaceWith newText:String? = nil) {
        tap()
        press(forDuration: 1.0)
        var select = XCUIApplication().menuItems["Select All"]

        if !select.exists {
            select = XCUIApplication().menuItems["Select"]
        }
        //For empty fields there will be no "Select All", so we need to check
        if select.waitForExistence(timeout: 0.5), select.exists {
            select.tap()
            typeText(String(XCUIKeyboardKey.delete.rawValue))
        } else {
            tap()
        }
        if let newVal = newText {
            typeText(newVal)
        }
    }
}
Agisight
  • 1,778
  • 1
  • 14
  • 15
2

the following is working in Xcode 10.3 running on macOS 10.14.3, for iOS app running iOS 12.4:

XCTAssert( app.textFields["testTextField"].exists, "test text field doesn't exist" )
let tf = app.textFields["testTextField"]
tf.tap()    // must give text field keyboard focus!
tf.typeText("Hello!")
XCTAssert( tf.exists, "tf exists" )   // text field still exists
XCTAssertEqual( tf.value as! String, "Hello!", "text field has proper value" )
Dad
  • 6,388
  • 2
  • 28
  • 34