ios - 如何编写 XCUI 测试是否导航到正确的屏幕?

标签 ios swift testing xcode-ui-testing ui-testing

我正在为以下 UI 编写测试 UI 测试用例 enter image description here

我想测试登录点击是否在仪表板屏幕上正确导航。

有什么方法可以做到这一点吗?

我目前的测试代码是这样的

func testExample() {

        let usernameTextField = app.textFields["Username"]
        usernameTextField.tap()
        usernameTextField.typeText("abc@gmail.com")

        let passwordTextField = app.textFields["Password"]
        passwordTextField.tap()
        passwordTextField.typeText("abc123")

        app.buttons["Login" ].tap()

                //let loginButton = app.staticTexts["Login"]
                //XCTAssertEqual(loginButton.exists, true)

        app.navigationBars["UIView"].buttons["Back"].tap()


    }

最佳答案

当依赖于文本值时,UI 测试会变得非常脆弱。我鼓励您为 ViewController 的 view 设置辅助功能标识符。这样,即使您更改标题或更改整个布局,您仍然可以确保您位于正确的页面/屏幕/ View 中。

class DashVC: UIViewController {
    override func viewDidLoad() {
        view.accessibilityIdentifier = "view_dashboard"
    }
}



    func test_login_withValidInput_goesDashBoard() {
        let app = XCUIApplication()

        //...    
        app.buttons["Login" ].tap()

        let dashBoardView = app.otherElements["view_dashboard"]
        let dashBoardShown = dashBoardView.waitForExistence(timeout: 5)

        XCTAssert(dashBoardShown)

    }

关于ios - 如何编写 XCUI 测试是否导航到正确的屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45657788/

相关文章:

arrays - 当 UIPanGesture 上的状态为 .ending 时循环遍历数组

android - 如何使用 Robolectric 3.0 从服务器测试 ListView 项目

python - 在本地测试 Hive + spark python 程序?

iphone - 没有平滑的图像查看器

ios - 返回值类型为CGFloat时如何使用ReactiveCocoa发送Keypath

ios - 具有多个委托(delegate)的 UITextfield

swift - 无法在模拟器 Xcode 6 中运行应用程序

swift - 由于信号段错误 11,Xcode 8 随机命令失败

testing - 如何在 julia 中为自定义结构创建相等性测试用例

ios - TableView 不显示超过 10 行