是否可以直接在 XCUITest 中打开屏幕? [英] Is it possible to open a screen directly in XCUITest?

查看:37
本文介绍了是否可以直接在 XCUITest 中打开屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个屏幕,比如说,

I have 3 screens, lets say,

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

默认情况下,登录屏幕会在应用启动时打开.单击忘记密码"按钮时会显示忘记密码"屏幕,单击帮助"链接时会打开帮助"屏幕.

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.

我可以不经过使用XCUITest点击按钮的过程而直接打开忘记密码屏幕吗?

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

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

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

推荐答案

据我所知,使用 XCUITest Framework 无法直接进入第二屏.无论如何,documentation 指出:

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

UI 测试以用户无法访问您应用的内部方法、函数和变量的相同方式来测试您应用的 UI.这使您的测试能够以与用户相同的方式查看应用,从而暴露用户遇到的 UI 问题.

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 为什么要进行测试.

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

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

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.

编辑

在阅读您的评论后,我想到了一个骇人听闻的解决方案.您可以使用 Launch Environment 和/或通过测试与您的应用进行通信启动参数.因此,在您的 XCTestCase 类中,设置参数和环境:

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

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

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

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

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

注意:这是一个非常肮脏的黑客,不推荐使用这种方式编写 UI 测试.

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

这篇关于是否可以直接在 XCUITest 中打开屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆