atgrubb
atgrubb

Reputation: 1203

How to check what screen the application is currently running?

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?

Upvotes: 4

Views: 2022

Answers (3)

Sushant
Sushant

Reputation: 470

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" {
}

Upvotes: 0

atgrubb
atgrubb

Reputation: 1203

  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"]);

Upvotes: 1

Sandy
Sandy

Reputation: 3131

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.

Upvotes: 0

Related Questions