![](/img/trans.png)
[英]How to dismiss the UIActivityViewController during a UI test with Xcode 11 & iOS 13
[英]iOS 13 Xcode UI test Automation type mismatch
我的應用程序使用在代碼中設置的WKWebView
(這是因為 iOS 10 中的這個錯誤而完成的):
final class HelpViewController: UIViewController {
// …
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(webView)
let margins = view.layoutMarginsGuide
webView.topAnchor.constraint(equalTo: self.back.bottomAnchor).isActive = true
webView.rightAnchor.constraint(equalTo: margins.rightAnchor).isActive = true
webView.leftAnchor.constraint(equalTo: margins.leftAnchor).isActive = true
webView.bottomAnchor.constraint(equalTo: margins.bottomAnchor).isActive = true
webView.navigationDelegate = self
//…
}
我的 UI 測試如下所示:
func test_helpButtonShowsHelpText() {
//…
let webView = shopEasyApp.webViews.element
let webViewExists = webView.waitForExistence(timeout: kDefaultUITestTimeout)
XCTAssert(webViewExists, "Web view does not exist")
let webViewIsHittable = webView.isHittable
//…
}
此測試運行至 iOS 12 時沒有問題。
使用 iOS 13,它停止在測試webView.isHittable
並出現以下錯誤:
Failed to get matching snapshot: Multiple matching elements found for <XCUIElementQuery: 0x600000bdfbb0>.
日志說:
Assertion Failure: ShopEasyBasicUITests.swift:1100: Failed to get matching snapshot: Multiple matching elements found for <XCUIElementQuery: 0x600003efb4d0>.
Sparse tree of matches:
→Application, pid: 71038, label: 'Shop Easy!'
↳Window, {{0.0, 0.0}, {375.0, 812.0}}
↳Other, {{0.0, 0.0}, {375.0, 812.0}}
↳Other, {{0.0, 0.0}, {375.0, 812.0}}
↳WebView, {{16.0, 74.0}, {343.0, 704.0}}
↳WebView, {{16.0, 74.0}, {343.0, 704.0}}
↳WebView, {{16.0, 74.0}, {343.0, 704.0}}
Possibly caused by runtime issues:
Automation type mismatch: computed WebView from legacy attributes vs ScrollView from modern attribute. Input attributes and values: {
"XC_kAXXCAttributeAutomationType" = 46;
"XC_kAXXCAttributeElementBaseType" = UIScrollView;
"XC_kAXXCAttributeElementType" = WKScrollView;
"XC_kAXXCAttributeTraits" = 8589934592;
}
Automation type mismatch: computed Other from legacy attributes vs WebView from modern attribute. Input attributes and values: {
"XC_kAXXCAttributeAutomationType" = 58;
"XC_kAXXCAttributeElementBaseType" = UIView;
"XC_kAXXCAttributeElementType" = WKWebView;
"XC_kAXXCAttributeTraits" = 146028888064;
}
Automation type mismatch: computed Other from legacy attributes vs WebView from modern attribute. Input attributes and values: {
"XC_kAXXCAttributeAutomationType" = 58;
"XC_kAXXCAttributeElementBaseType" = UIView;
"XC_kAXXCAttributeElementType" = WKContentView;
"XC_kAXXCAttributeTraits" = 146028888064;
}
我的問題是:
我的代碼有什么問題,以及如何正確執行?
您可以看到 XCTest 可以看到的三個 WebKit 視圖:
WKScrollView
WKWebView
WKContentView
看起來他們已經改變了在 iOS 13 上為元素指定類型的方式,這意味着您的視圖層次結構現在包含多個元素,這些元素被歸類為WebView
。 It appears that WKWebView
and WKContentView
now both resolve to being elements of type WebView
, whereas in iOS 12, only the WKScrollView
(which, in iOS 13, no longer resolves to being classed as a WebView
element) was classed as a WebView
.
現在進行查詢:
shopEasyApp.webViews.element
查詢使用element
,僅當您知道查詢將解析為單個元素時才應該使用它。 由於shopEasyApp.webViews
現在有 2 個結果,因此沒有一個元素可供element
返回。 您可以檢查是否存在,因為檢查XCUIElement
的exists
屬性不需要成功解析查詢。 但是,所有其他XCUIElement
屬性確實需要解析查詢,因此當使用isHittable
時,XCTest 引擎會嘗試解析查詢,發現查詢未按預期解析為單個元素,並拋出此錯誤。
Multiple matching elements found for <XCUIElementQuery: 0x600003efb4d0>
為了解決這個問題,我建議您修改查詢以使用第一個匹配索引而不是使用element
:
shopEasyApp.webViews.elementBound(by: 0)
如果屏幕上有多個WebView
元素,此查詢將選擇列表中的第一個元素。
我也遇到過類似的問題——沒有 webview 的后代。 經過調查,事實證明iOS 13.1上一切正常,但iOS 13.3上一切正常。
我認為這是模擬器的錯誤。
對於所有面臨相同問題的人 - 指定早期版本 (13.1) 直到它被修復。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.