How to get the value of textfield
in XCODE7 UITesting?
var b = XCUIApplication().childrenMatchingType(.textField).elementBoundByIndex(0).stringValue
Let's suppose you have a Text Field like so:
(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:
I hope that helps.
Lets assume I want to get the string that gets printed for the nav bar of my app
What I did was I created a XCUIElement for my nav bar:
XCUIApplication *app = [[XCUIApplication alloc] init];
XCUIElement *navBarTitle = [app.navigationBars elementBoundByIndex:0];
I then put a breakpoint after the creation of the navBarTitle object and used the debug console to print out the details of the navBarTitle object:
You see in the print out of the debug console that there is a key called identifier.
To extract that string from that object, I created an NSString object using the following method:
NSString *nameofuser = [navBarTitle valueForKey:@"identifier"];
I used the XCUIElement navBarTitle and then used the method valueForKey. valueForKey extracts the string value for the key identifier.
You can read up about this method here: NSKeyValueCoding
valueForKey is the KEY to unlocking the answer to this question....pun intended :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With