使用 Xcode UI 测试测试 UIWebView

2023-10-22移动开发问题
7

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

The End

相关推荐

如何添加“写评论"/“给我们打分"我的应用程序的功能?
How to Add quot;Write a Reviewquot; / quot;Rate Usquot; Feature to My App?(如何添加“写评论/“给我们打分我的应用程序的功能?)...
2024-04-14 移动开发问题
10

iOS UIWebView 应用在 Safari 中打开链接
iOS UIWebView app opens link in Safari(iOS UIWebView 应用在 Safari 中打开链接)...
2023-11-09 移动开发问题
17

使用 javascript:window.close() 关闭 UIWebView;
Close UIWebView using javascript:window.close();(使用 javascript:window.close() 关闭 UIWebView;)...
2023-11-09 移动开发问题
4

NSCachedURLResponse 返回对象,但 UIWebView 不解释内容
NSCachedURLResponse returns object, but UIWebView does not interprets content(NSCachedURLResponse 返回对象,但 UIWebView 不解释内容)...
2023-11-09 移动开发问题
1

无法隐藏 iAd 横幅并在其位置显示 UIWebView
Trouble hiding iAd banner and displaying UIWebView in its place(无法隐藏 iAd 横幅并在其位置显示 UIWebView)...
2023-11-09 移动开发问题
16

知道何时在 UIWebView 中加载了 AJAX
Knowing when AJAX has loaded in UIWebView(知道何时在 UIWebView 中加载了 AJAX)...
2023-11-09 移动开发问题
2