3

Is there a way to check directly what screen an XCUIApplication is currently running / is visible on screen?

I want to be able to XCTAssert that the application is currently displaying screen 'X'. I was thinking I might just create a hidden UIElement or button specific to each screen and then assert that that element exists on the screen. Does anyone have a more elegant or direct way of doing this, though?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
atgrubb
  • 1,145
  • 7
  • 11

3 Answers3

1
  1. Create a UI label to be able to reference the current screen.
  2. Set the accessibilityLabel of that UI Label so you can find it via XCUIElementQuery.
  3. Set the accessibilityValue of that UI label so that you can determine the state / screen that the application is in / on.

In the application code... UILabel *currentScreen;

self.currentScreen.accessibilityLabel = @"Current Screen";

<insert_code_to_change_state/screen_in_app>

self.currentScreen.accessibilityValue = @"<insert_current_state/screen_of_app";

In the UI Test code...

XCUIElement *currentScreen = app.staticTexts[@"Current Screen"];

<insert_code_to_navigate_to_state/screen_in_app>

XCTAssert([currentScreen.value isEqualToString: @"<insert_current_state/screen_of_app"]);
atgrubb
  • 1,145
  • 7
  • 11
0

I am assuming each screen will have some title. In that case you can use following approach.

//Get the top most navigation bar element
XCUIElement *currentNavigationBar = [app.navigationBars elementBoundByIndex:0];
XCTAssertEqualObjects(currentNavigationBar.identifier, @"Screen1 Name");

//Perform UI operations
XCTAssertEqualObjects(currentNavigationBar.identifier, @"Screen2 Name");

//Performa more UI operation
XCTAssertEqualObjects(currentNavigationBar.identifier, @"Screen3 Name");

So here when ever you access currentNavigationBar it queries the latest one.

Sandy
  • 3,021
  • 1
  • 21
  • 29
  • I don't think this will work if there are no navigationBars present. – Bay Phillips Oct 01 '15 at 20:10
  • Could I create some hidden static text with an accessibilityLabel = @"Current Screen" and then just change the accessibilityValue of that static text on each screen? – atgrubb Oct 01 '15 at 20:16
  • @atgrubb How are you changing screens in app, aren't using any navigation controller (push or present) to do that? – Sandy Oct 01 '15 at 20:28
  • I probably should have clarified more, but a 'Change of screens' isn't always a typical screen change via tapping a button on a navigation or tab bar. What I *should have* asked was how can I check the state of a screen. I ended up doing what I proposed - UILabel gets created when the screen is in a particular state (that may appear graphically like the screen has changed to an end-user), and then checking if that UILabel has a certain accessibilityValue. – atgrubb Oct 01 '15 at 21:13
0

You can specify title for each viewController like this ( if it's a navigation Controller),

self.navigationItem.title = @"Your title";

then you create a user defined method in AppDelegate to find the visibleViewController as explained here, Get the top ViewController in iOS Swift and read title of the view controller with something like,

NSString currentController = self.navigationController.visibleViewController.title;

You should be able to call this method on appDelegate's object from the UITest Target and write your test based on title value,

if currentController == "myViewControllerTitle" {
}
Community
  • 1
  • 1
Sushant
  • 440
  • 3
  • 8