Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing UIWebView with Xcode UI Testing

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 navigation controller + view controller with web view and button) and I want to check following scenario:

  1. Web View loads page www.example.com
  2. User taps on button
  3. Web View loads some page with URL: www.example2.com

I want to check which page is loaded in UIWebView after pressing button. Is this possible with UI Testing right now?

Actually I'm getting web view like this:

let app:XCUIApplication = XCUIApplication()
let webViewQury:XCUIElementQuery = app.descendantsMatchingType(.WebView)
let webView = webViewQury.elementAtIndex(0)
like image 498
Apan Avatar asked Sep 17 '15 06:09

Apan


People also ask

How do I test UI in XCode?

How to Run XCUI Tests on XCode. To run the XCUITests on XCode, you can click the highlighted icon below to see your newly created UI Test targets. You can hover on the “testExample()” test case and click the “Play” icon to run that specific test to see if everything was set up properly.

What is UI testing in Swift?

UI tests interact with your app, similar to how your user does. In this article, we'll look at how you can test these types of scenarios. We'll write tests for navigating through an app and interacting with elements on the screen.


2 Answers

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.

like image 115
Joe Masilotti Avatar answered Jan 01 '23 21:01

Joe Masilotti


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)
like image 20
Sandy Avatar answered Jan 01 '23 23:01

Sandy