使用 Xcode UI 测试测试 UIWebView

Testing UIWebView with Xcode UI Testing(使用 Xcode UI 测试测试 UIWebView)

本文介绍了使用 Xcode UI 测试测试 UIWebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用来自 XCTest Framework 的新 Xcode UI 测试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:

  1. 网页视图加载页面www.example.com
  2. 用户点击按钮
  3. Web View 加载一些带有 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 测试为 XCUIElementQuery" rel="noreferrer">链接对 UIWebViewWKWebView 都有效.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 Xcode UI 测试测试 UIWebView

基础教程推荐