使用Xcode UI测试测试UIWebView

Apa*_*pan 11 uiwebview xctest ios9 xcode-ui-testing

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

  1. Web视图加载页面 www.example.com
  2. 用户点击按钮
  3. Web View使用URL加载一些页面: www.example2.com

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

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

let app:XCUIApplication = XCUIApplication()
let webViewQury:XCUIElementQuery = app.descendantsMatchingType(.WebView)
let webView = webViewQury.elementAtIndex(0)
Run Code Online (Sandbox Code Playgroud)

Joe*_*tti 11

你将不能够告诉加载页面时,在正在显示实际的URL.但是,您可以在屏幕上检查断言内容.UI测试提供了一个XCUIElementQuery那伟大工程具有两个链接UIWebViewWKWebView.

请记住,页面不会同步加载,因此您必须等待显示实际元素.

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()
Run Code Online (Sandbox Code Playgroud)

如果你想深入研究代码,还有一个工作测试主机与两个链接一起使用.