How to Open a Screen Directly in Xcuitest

Is it possible to open a screen directly in XCUITest?

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.

UI testing accessing specific screen directly : iOS

You can set a variable in app.launchEnvironment like this:

        app.launchEnvironment["SKIP_VERIFICATION"] = true

In main target, before going to next screen you can check if SKIP_VERIFICATION is set, skip this screen and move to next screen.

In main target you can get this variable like this:

ProcessInfo.processInfo.environment["SKIP_VERIFICATION"]

XCTest UI Testing - How to close and open an app without relaunch?

As of Xcode 9 and iOS 11, XCUIApplication() has an activate() method that you can use to relaunch the app.

As brandenbyers suggested, you can "press" the home button to background your app, and then activate it again like this to avoid using Siri:

XCUIDevice.shared.press(.home)
XCUIApplication().activate()

Note that this only works with targets built using XCUITest, not XCTest. If you try this within a target built from XCTest, all XCUIApplication operations will crash.

Is it possible to testing uitest (XCUI) when open url external browser?

Absolutely. Safari is an XCUIApplication just like any other with a bundle identifier of com.apple.mobilesafari.

To check the URL you'll tap() the URL field (it's a button when a page loads) and read the value of the URL field (at this point it's a textField). Once you're done asserting that, activate() your app and you'll be back in it (note: my tests finish after asserting so I don't have to do this, but it's the published method - you could always enter debug and find how to tap the button to return to your application in the top-left of the screen if this doesn't work).

I'm happy to provide exact code if you show me you've tried this and can't get it working, but it's pretty straightforward XCUI automation.

XCUITest interact with elements on Home Screen

Yes, the home screen is run under the "Springboard" application, which encompasses a lot of system dialogs and popups as well.

Once on the home screen, you can pull a debug output to see what is available for interaction:

po XCUIApplication(bundleIdentifier: "com.apple.springboard")


Related Topics



Leave a reply



Submit