XCUITest中是否可以直接开屏?

Is it possible to open a screen directly in XCUITest?

我有 3 个屏幕,比方说,

  1. 登录
  2. 忘记密码
  3. 帮助屏幕

默认情况下,登录屏幕会在应用程序启动时打开。单击忘记密码按钮时会显示忘记密码屏幕,单击帮助 link 时会打开帮助屏幕。

我可以直接打开忘记密码屏幕,而无需使用 XCUITest 单击按钮吗?

我建议使用与传递 adb 意图以直接打开视图相同的内容。

据我所知,您无法使用 XCUITest Framework 直接进入第二屏幕。无论如何,documentation 声明:

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.

这意味着,如果您的应用的用户无法直接到达第二个屏幕,为什么您的 UI 可以测试。

我知道当您 运行 测试时等待进入第二个屏幕很耗时,但您可以绕过为每个测试编写它。只写一次,在您的 XCTestCase class 中编写一个函数,您可以在其中实现调用第二个屏幕并在 setUp() 方法中调用该函数。然后,每次 运行 测试时都会调用跳过第一个屏幕的过程,因为 setUp() 方法在每次测试 运行.

之前被调用

编辑

阅读您的评论后,我想到了一个 hacky 解决方案。您可以使用 Launch Environment and/or Launch Arguments 从您的测试中与您的应用进行通信。因此,在您的 XCTestCase class 中,设置参数和环境:

class ForgotPasswordUITest: XCTestCase {
    let app = XCUIApplication()

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

然后,在您的 ViewController 中写入这些计算属性:

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

并且在viewDidLoad()方法中,你可以有这样的东西:

    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)
        }
    }

注意:这是一个非常肮脏的 hack,不推荐编写 UI 测试的方式。