Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
766 views
in Technique[技术] by (71.8m points)

swift - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...