Reputation: 134
I am new to UI test writing.
I wanted to know if it is possible to know what elements are/exists in view. I want to know how many and how they are called.
I tried something like this to loop through all elements, but it does not work.
for element in app.accessibilityElements! {
print(element)
}
Upvotes: 4
Views: 6221
Reputation: 76
You could always have a break point in the testcase and then make some print calls to the console.
po app.accessibilityElements
po app.accessibilityElements.elementBoundByIndex(0)
po app.buttons["Icon Menu Light"]
etc.
And see what comes back in the output view hierarchy to reference, or just a simple call to po app
will print the hierarchy.
Once you know a particular view exists.. app.buttons["Icon Menu Light"].exists
.. you can then try using something like this to confirm the button/element is visible within the current view by passing it to a helper function like:
func isElementInView(element: XCUIElement) -> Bool {
let window = XCUIApplication().windows.elementBoundByIndex(0)
return CGRectContainsRect(window.frame, element.frame) }
This will tell you whether the element is visible on the screen, because the element.exist
call will return true, even if the element is off screen and hasnt been shown to the user (i.e. if something hides off screen and then transitions into the frame)
Upvotes: 6
Reputation: 10086
You're looking for the debugDescription
method of XCUIElement, in your case to get the entire hierarchy of the current visible window:
app.debugDescription
Quoting header comments:
Provides debugging information about the element. The data in the string will vary based on the time at which it is captured, but it may include any of the following as well as additional data:
• Values for the elements attributes. • The entire tree of descendants rooted at the element. • The element's query.
This data should be used for debugging only - depending on any of the data as part of a test is unsupported.
Upvotes: 8