Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing nested UIElements in Xcode 7 UI Testing

I am having trouble locating XCUIElements on a screen for the app I am testing. I realize you can access a button for example via something like:

app.buttons[].elementBoundByIndex(0)

But the problem is sometimes, the component is not found. Like in a case where I have a Button in a cell in a UITableView. I try to make an XCUIElementQuery to find the button, and it is not there. I try to look for tables or tableviews or collection views and even though they are in the view controller, they are not found in UI Testing. The count of the returned array will be zero.

I attempted originally to record the test, but clicking the element I am trying to access did not work. Xcode detected it as an "Other Element" and when trying to tap during, playback the application does not advance.

Is there a high level way to access a component like a UIView high in the UI hierarchy to cascade down?

like image 390
ajc6432 Avatar asked Oct 02 '15 17:10

ajc6432


People also ask

How do I test UI in XCode?

How to Run XCUI Tests on XCode. To run the XCUITests on XCode, you can click the highlighted icon below to see your newly created UI Test targets. You can hover on the “testExample()” test case and click the “Play” icon to run that specific test to see if everything was set up properly.

How do I check my SwiftUI UI?

When creating a new SwiftUI application, we can select the Include Tests checkbox and Xcode will create the target for us. Adding a test target. Alternatively, we can add a UI testing target to an existing project by selecting File > New > Target > UI Testing bundle in Xcode's menu.

How do I debug in Xcuitest?

From the debugger command line, you can enter p XCUIApplication(). buttons["show-test-button"] . If this is the first command used, then this text below is printed. To see what happens when you tap on this element you can then enter p $R2.

What is XCUIElement?

In macOS and iPadOS 15 and later, XCUIElement provides a way to test your app with keyboard and mouse interactions, such as typing, clicking, scrolling, and moving and pausing the pointer. In iOS, XCUIElement provides a way to test your app with gestures, such as tapping, swiping, pinching, and rotating.


2 Answers

I didn't know this at the time, but what I was looking for was basically debugDescription:

Set a breakpoint when you hit the area you're trying to debug. Then

po print(app.debugDescription)

in the debug console. You will see the hierarchy then.

like image 85
ajc6432 Avatar answered Sep 23 '22 00:09

ajc6432


Ideally you should set an accessibilityIdentifier on your button and search for it via that. The accessibilityIdentifier should be unique for elements on the screen. You can set an accessibilityIdentifier in the Identity Inspector in Interface Builder (command-option-3) or in code directly. Once you have one, the query looks like:

app.buttons["SomeAccessibilityIdentifier"]
like image 38
Charles A. Avatar answered Sep 24 '22 00:09

Charles A.