Testing UIWebView with Xcode UI Testing

后端 未结 2 2042
遇见更好的自我
遇见更好的自我 2021-01-02 03:36

I\'m using new Xcode UI Testing from XCTest Framework with the Xcode 7 GM. I\'ve got an app with simple UIWebView (it\'s just a naviga

相关标签:
2条回答
  • 2021-01-02 03:53

    You won't be able to tell which page is loaded, as in the actual URL that is being displayed. You can, however, check assert content is on the screen. UI Testing provides a XCUIElementQuery for links that works great with both UIWebView and WKWebView.

    Keep in mind that a page doesn't load synchronously, so you will have to wait for the actual elements to appear.

    let app = XCUIApplication()
    app.launch()
    
    app.buttons["Go to Google.com"].tap()
    
    let about = self.app.staticTexts["About"]
    let exists = NSPredicate(format: "exists == 1")
    expectationForPredicate(exists, evaluatedWithObject: about, handler: nil)
    
    waitForExpectationsWithTimeout(5, handler: nil)
    XCTAssert(about.exists)
    
    XCTAssert(app.staticTexts["Google Search"].exists)
    app.links["I'm Feeling Lukcy"].tap()
    

    There is also a working test host that goes along with the two links if you want to dig into the code.

    0 讨论(0)
  • 2021-01-02 03:58

    If the title of the pages are different, you can check for the title of the web page.

    let app = XCUIApplication()
    app.launch()
    //Load www.example.com
    
    //Tap on some button    
    app.links["Your button"].tap()
    
    //Wait for www.example2.com to load
    let webPageTitle = app.otherElements["Example2"]
    let exists = NSPredicate(format: "exists == 1")
    expectationForPredicate(exists, evaluatedWithObject: webPageTitle, handler: nil)
    waitForExpectationsWithTimeout(5, handler: nil)
    
    0 讨论(0)
提交回复
热议问题