how to get value of an XCUIElement?
Asked Answered
H

3

11

How to get the value of textfield in XCODE7 UITesting?

var b = XCUIApplication().childrenMatchingType(.textField).elementBoundByIndex(0).stringValue
Humberto answered 5/8, 2015 at 19:58 Comment(0)
J
16

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.

Jamnis answered 28/8, 2015 at 2:27 Comment(0)
D
4

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

Derwent answered 18/11, 2016 at 0:48 Comment(2)
I cannot upvote this enough! The "po..." in lldb is a life saver!Testudinal
Thanks so much! On swift, it was - myElement.value(forKey: "identifier")Zacharie
E
0

app.staticTexts.element(boundBy: 1).label

Eastwood answered 5/5, 2020 at 23:55 Comment(1)
Welcome to SO, please refer to this for better presenting your answer: meta.https://mcmap.net/q/77258/-passing-a-class-object-to-a-function-probably-by-pointer-not-reference-cEngels

© 2022 - 2024 — McMap. All rights reserved.