使用Xcode UI测试测试UIWebView [英] Testing UIWebView with Xcode UI Testing
问题描述
我正在将XCTest Framework
中的新Xcode UI Testing
与Xcode 7 GM
一起使用.我有一个带有简单UIWebView的应用程序(它只是一个导航控制器+具有Web视图和按钮的视图控制器),我想检查以下情形:
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:
- Web视图加载页面
www.example.com
- 用户点击按钮
- Web视图加载带有URL的某些页面:
www.example2.com
- Web View loads page
www.example.com
- User taps on button
- Web View loads some page with URL:
www.example2.com
我想在按下按钮后检查UIWebView中加载了哪个页面.现在可以通过UI测试来实现吗?
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)
推荐答案
您将无法像显示的实际URL那样告诉哪个页面已加载.但是,您可以检查屏幕上是否有断言内容. UI测试为
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.
这篇关于使用Xcode UI测试测试UIWebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!