how to get value of an XCUIElement?

Nikhil Kumar picture Nikhil Kumar · Aug 5, 2015 · Viewed 14.7k times · Source

How to get the value of textfield in XCODE7 UITesting?

var b = XCUIApplication().childrenMatchingType(.textField).elementBoundByIndex(0).stringValue

Answer

backslash-f picture backslash-f · Aug 28, 2015

Let's suppose you have a Text Field like so:

textField

(I'm hardcoding it's value there ("My Text") for the sake of the example.)

Give it an Accessibility Label. In this case, I'm giving it the "Text Field" label.

Now to access its value, you could do something like:

func testExample() {

    let app = XCUIApplication()        
    let textField = app.textFields["Text Field"]
    XCTAssertTrue(textField.value as! String == "My Text")

    // So basically "textField.value"
}

On a side note: when in doubt on how to access certain properties of a given XCUIElement, I found that its debugDescription helps a lot.

For example, set a breakpoint after the property declaration, execute the test, wait for the app to stop at the breakpoint, go to lldb console, type po propertyName.debugDescription, and check the output:

output

I hope that helps.