Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get value of an XCUIElement?

How to get the value of textfield in XCODE7 UITesting?

var b = XCUIApplication().childrenMatchingType(.textField).elementBoundByIndex(0).stringValue
like image 252
Nikhil Kumar Avatar asked Aug 05 '15 19:08

Nikhil Kumar


2 Answers

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.

like image 99
backslash-f Avatar answered Nov 02 '22 05:11

backslash-f


Lets assume I want to get the string that gets printed for the nav bar of my app

enter image description here

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:

print out

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 :)

like image 21
Ray Avatar answered Nov 02 '22 04:11

Ray