Swift2 UI Test - Wait for Element to Appear

前端 未结 6 1127
逝去的感伤
逝去的感伤 2021-02-13 15:40

I want to have a test pause and wait for an element to appear on screen before proceeding.

I don\'t see a good way to create an expectation for this and wait using

6条回答
  •  时光说笑
    2021-02-13 16:11

    Based on onmyway133 code I came up with to extensions (Swift 3.2):

    extension XCTestCase {
      func wait(for element: XCUIElement, timeout: TimeInterval) {
        let p = NSPredicate(format: "exists == true")
        let e = expectation(for: p, evaluatedWith: element, handler: nil)
        wait(for: [e], timeout: timeout)
      }
    }
    
    extension XCUIApplication {
      func getElement(withIdentifier identifier: String) -> XCUIElement {
        return otherElements[identifier]
      }
    }
    

    So on your call site you can use:

    wait(for: app.getElement(withIdentifier: "ViewController"), timeout: 10)
    

提交回复
热议问题