UI Testing - Finding the 'xpath' of an element

Hello,


I have an application where I need to read the string of a staticText and do an assertion on it. However, the value of the text may change.


How can I find the 'xpath' of the staticText so I can run checks on the title of it?


So far I'm able to print out the value of the app.windows.staticTexts, and see the text in the console, but the console does not tell me the 'xpath' of the staticText itself.

Accepted Reply

Yes, you can do that. Once you set a variable's value you can reuse that value without knowing what it's value is in advance. For example: Let's say you have a list of names, but this list changes frequently and you might just want to do some sort of checks for the name on the top of the list.


In code you could something like the following:

//Let's test that "MyName" is the top of the list.

let nameInTopOfList = app.tables.cells.staticTexts.elementBoundByIndex(1).label // Find name in the top of the List
XCTAssertEqual(nameInTopOfList, "MyName") // Checks to see whatever it found in the top of the list is Equal to "MyName"


I think that's what you're trying to do.

Replies

The UI Testing tools have their own element query resolution pathing that is completely unrelated to XPath. XPath selectors will not work in the new UI Testing Tool. If you're able to find the text's value then you're already successfully querying and finding the element's value.


If you want to find out exactly how the query element logic is working and see a "tree-like" structure to how it does the "finding" of an element, I'd suggest doing something like the following:


p print(app.windows.staticTexts.debugDescription)


It should print out all the available options of what's on the screen for app.windows.staticTexts and show you how it goes through it's querying chain to find elements.

Thanks Denzik, yeah I can print out the debug description for elements like that.


However, this is one of the texts returned in the console output:


StaticText 0x7ff3a3891210: traits: 64, {{128.0, 44.5}, {119.5, 16.0}}, label: '13 FOUND'

Sometimes, that label may say '10 FOUND' or '2 FOUND'. How can I assign that label text to a variable, and then run checks off that variable? Is that possible? For instance:

let foundLabel = staticTexts["NAME"]

Something along those lines

In case the last post didnt make sense:


I cant make a variable like: let foundLabel = app.staticTexts["13 FOUND"]


This is because sometimes that label is going to display a different number, so instead I'd like to assign that labels text to a variable, so I can check it's value no matter how many things are found

Yes, you can do that. Once you set a variable's value you can reuse that value without knowing what it's value is in advance. For example: Let's say you have a list of names, but this list changes frequently and you might just want to do some sort of checks for the name on the top of the list.


In code you could something like the following:

//Let's test that "MyName" is the top of the list.

let nameInTopOfList = app.tables.cells.staticTexts.elementBoundByIndex(1).label // Find name in the top of the List
XCTAssertEqual(nameInTopOfList, "MyName") // Checks to see whatever it found in the top of the list is Equal to "MyName"


I think that's what you're trying to do.