iOS 9 UI Testing - Test Fails Because Target Control Isn't Available (yet)

前端 未结 1 755
陌清茗
陌清茗 2021-02-10 03:15

I am testing the new UI testing functionality of Xcode 7 (introduced in the WWDC 2015 video \"UI Testing in Xcode\").

On launch, my app animates a \"login panel\" into v

1条回答
  •  长情又很酷
    2021-02-10 03:34

    I would recommend against ticking the run loop and instead use the built in XCTest asynchronous API. For example:

    let app = XCUIApplication()
    app.launch()
    
    let textField = app.textField["user name"]
    let existsPredicate = NSPredicate(format: "exists == 1")
    
    expectationForPredicate(existsPredicate, evaluatedWithObject: textField, handler: nil)
    waitForExpectationsWithTimeout(5, handler: nil)
    
    textField.tap()
    textField.type("joemasilotti")
    

    This will wait until the text field with accessibility label/identifier "username" actually exists on the screen. Then it will tap on the text field, making it active, and type my username. If five seconds pass before this becomes true the test will fail.

    I have also written a post on how to use UI Testing on my blog and put together a UI Testing Cheat Sheet for scenarios like this.

    0 讨论(0)
提交回复
热议问题