使用Xcode UI测试测试UIWebView

我正在使用XCTest Framework和 Xcode 7 GM进行新的 Xcode UI测试.我有一个简单的UIWebView应用程序(它只是一个带有Web视图和按钮的导航控制器视图控制器),我想检查以下场景:

> Web View加载页面www.example.com
>用户点击按钮
> Web View使用URL加载一些页面:www.example2.com

我想在按下按钮后检查UIWebView中加载了哪个页面.这是否可以通过UI测试立即实现?

其实我得到这样的网页视图:

let app:XCUIApplication = XCUIApplication()
let webViewQury:XCUIElementQuery = app.descendantsMatchingType(.WebView)
let webView = webViewQury.elementAtIndex(0)

解决方法

您将无法分辨正在加载的页面,就像在显示的实际URL中一样.但是,您可以在屏幕上检查断言内容. UI Testing为 links that works great with both UIWebView and WKWebView提供了XCUIElementQuery.

请记住,页面不会同步加载,因此您必须使用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()

如果你想深入研究代码,还有一个working test host和两个链接.

以上是来客网为你收集整理的使用Xcode UI测试测试UIWebView全部内容,希望文章能够帮你解决使用Xcode UI测试测试UIWebView所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。