【问题标题】:UI Test to check if an item exists is not working in Swift用于检查项目是否存在的 UI 测试在 Swift 中不起作用
【发布时间】:2020-01-29 12:00:54
【问题描述】:

我浏览了十几个不同的教程、文章等,但似乎没有什么对我有用。我在 Swift 中附加了一个简单的 UI 测试的代码,以查看是否存在抽屉的一部分。任何想法为什么我的测试失败(不是因为我发现了一个错误,而是因为我的 UI 测试编写不正确)?谢谢!

import XCTest
var app: XCUIApplication!


class MyAppUITests: XCTestCase {

    override func setUp() {
        super.setUp()
        continueAfterFailure = false
        app = XCUIApplication()
        app.launchArguments.append("--uitesting")
        app.launch()
    }

    func testDrawerDisplaysPrivacyAgreement() {   app.navigationBars["MyApp.MainView"].buttons["burgerButton"].tap()

        XCTAssertTrue(app.tables.staticTexts["Privacy Policy"].exists)


    }

    override func tearDown() {
        super.tearDown()
    }
}

【问题讨论】:

    标签: swift xcode unit-testing ui-testing


    【解决方案1】:

    我解决了我的问题。

    我做了什么:

    • 我删除了旧版本的 MyAppUITests。
    • 从现在开始,我每次创建测试时都添加let app = XCUIApplication(); app.launch()

    下面的测试成功通过了!这对我来说非常有效。 :)

    import XCTest
    var app: XCUIApplication!
    
    
    class MyAppUITests: XCTestCase {
    
        override func setUp() {
            super.setUp()
            continueAfterFailure = false
        // Delete these. Doesn't work if put here and not in each test
        //    app = XCUIApplication()
        //    app.launchArguments.append("--uitesting")
        //    app.launch()
        }
    
        func testDrawerDisplaysPrivacyAgreement() {   
    //  ** These two should be written in each test 
    // **
            let app = XCUIApplication()
            app.launch()
    // **
    
           let burgerButtonMenu =         app.navigationBars["MyApp.MainView"].buttons["burgerButton"]
            burgerButtonMenu.tap()
            let privacy = app.tables.staticTexts["Privacy Policy"]
    
            if  burgerButtonMenu.isEnabled && burgerButtonMenu.isSelected {
                XCTAssertTrue(privacy.exists)
    
    
        }
    
        override func tearDown() {
            super.tearDown()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-02-15
      • 2020-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-27
      • 2018-10-28
      • 1970-01-01
      相关资源
      最近更新 更多