Testing UIWebView with Xcode UI Testing(使用 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:
- 网页视图加载页面
www.example.com
- 用户点击按钮
- 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">链接对 UIWebView
和 WKWebView
都有效.
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


- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- URL编码Swift iOS 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- UITextView 内容插图 2022-01-01