Testing UIWebView with Xcode UI Testing

I am using the new Xcode UI Testing from the XCTest Framework using Xcode 7 GM . I have an application with a simple UIWebView (it's just a navigation controller + a view controller with a web view and button), and I want to check the following scenario:

  • Web browsing loads page www.example.com
  • User mute button
  • Web browsing loads the page with the URL: www.example2.com

I want to check which page is loaded in UIWebView after clicking a button. Is this possible with user interface testing right now?

In fact, I get the web view as follows:

 let app:XCUIApplication = XCUIApplication() let webViewQury:XCUIElementQuery = app.descendantsMatchingType(.WebView) let webView = webViewQury.elementAtIndex(0) 
+7
ios9 xcode-ui-testing uiwebview xctest
source share
2 answers

You will not be able to indicate which page is loaded, as in the actual URL that is displayed. However, you can verify that the contents of the assert are on the screen. UI Testing provides XCUIElementQuery links for that work great with UIWebView and WKWebView .

Keep in mind that the page does not load synchronously, so you need 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 node that comes with two links if you want to embed code.

+6
source share

If the page name is different, you can check the name 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) 
+3
source share

All Articles