Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to open a screen directly in XCUITest?

I have 3 screens, lets say,

  1. Login
  2. Forgot Password
  3. Help screen

By default the Login screen open when the app starts. The Forgot Password screen is shown when you click on the Forgot Password button, and Help Screen opens when the Help link is clicked.

Can I somehow open the the Forgot Password Screen directly without going through the procedure of clicking the button using XCUITest?

I am suggesting something in the same lines as passing an adb intent to open a View directly.

like image 207
anotherCoder Avatar asked Feb 28 '18 00:02

anotherCoder


People also ask

What is the difference between XCTest and XCUITest?

Tests are run directly from Xcode and are written with either Swift or Objective C. XCUITest makes use of the application's accessibility functionality, which allows tests to interact with the app as a real user would.

Which test framework does XCUITest use?

XCUITest is an iOS testing framework developed by Apple in 2015 for its native UI suite. It is developed on top of the XCTest framework which is the main test framework that is integrated within Apple's Xcode. The XCUITest tests can be written on XCode, using Swift or Objective-C.

How do I start XCUITest?

Find an image within the UI with an identifier called “XCUITest Tutorial” and then perform a tap action on it. Next, find a button on the UI with the text “Learn More” and perform another tap action on it. Finally, check if the tap action leads to the text “Automation Test iOS” to appear on the UI.

What are the test options in xcuitests?

Options include running tests on simulators or real devices. The recording will show all the actions that were performed on the UI. XCUITests as an iOS testing framework is the iOS counterpart to Android app automation testing.

Why run UI test cases with xcuitest?

Running UI test cases is not just about finding out what succeeded or failed. As UI elements are involved, testers must be able to see what went wrong, when it went wrong and at what point in the code the test case failed. The XCUITest framework provides multiple options to document these scenarios.

What does the recording show in xcuitests?

The recording will show all the actions that were performed on the UI. XCUITests as an iOS testing framework is the iOS counterpart to Android app automation testing. It proves simple interfaces to write test cases that perform any action on the UI and then document the actions as well as results.

What is the role of automation in xcuitest testing?

This article will discuss the role of automation, XCUITest itself, and how to get started with it. UI Automation Testing is a relatively recent practice, and frameworks like XCUITest allow testers to develop clean automation test suites for an app’s UI.


1 Answers

As far as I know, you can't go directly to the second screen using XCUITest Framework. Anyway, documentation states:

UI testing exercises your app's UI in the same way that users do without access to your app's internal methods, functions, and variables. This enables your tests to see the app the same way a user does, exposing UI problems that users encounter.

Which means that if user of your app can't reach the second screen directly, why could your UI tests.

I know it's time consuming to wait to go to the second screen when you run your tests, but you can bypass writing it for every test. To write it only once, in your XCTestCase class write a function where you implement calling a second screen and call that function in setUp() method. Then, the process of skipping the first screen will be called every time you run a test because setUp() method is called before every test run.

EDIT

After reading your comment, I could think of one hacky solution. You can communicate with your app from your tests using Launch Environment and/or Launch Arguments. So, in your XCTestCase class, set up argument and environment:

class ForgotPasswordUITest: XCTestCase {
    let app = XCUIApplication()

    override func setUp() {
        app.launchArguments += ["UI-TESTING"]
        app.launchEnvironment["pageToGo"] = "forgotPassword"
        app.launch()
    }
}

Then, in your ViewController, write these computed properties:

var isUiTestingEnabled: Bool {
    get {
        return ProcessInfo.processInfo.arguments.contains("UI-TESTING")
    }
}

var shouldShowForgotPassword: Bool {
    get {
        return ProcessInfo.processInfo.environment["pageToGo"] == "forgotPassword"
    }
}

var shouldShowHelpScreen: Bool {
    get {
        return ProcessInfo.processInfo.environment["pageToGo"] == "helpScreen"
    }
}

And in viewDidLoad() method, you can have something like this:

    if isUiTestingEnabled {
        if shouldShowForgotPassword {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let secondViewController = storyboard.instantiateViewController(withIdentifier: "ForgotPasswordViewController")
            self.present(secondViewController, animated: true, completion: nil)
        } else if shouldShowHelpScreen {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let secondViewController = storyboard.instantiateViewController(withIdentifier: "HelpScreenViewController")
            self.present(secondViewController, animated: true, completion: nil)
        }
    }

Note: this is a really dirty hack and it is not recommended way of writing UI tests.

like image 55
Mladen Avatar answered Oct 26 '22 05:10

Mladen