gpt4 book ai didi

ios13 - iOS 13 Xcode UI 测试自动化类型不匹配

转载 作者:行者123 更新时间:2023-12-01 11:12:21 25 4
gpt4 key购买 nike

我的应用程序使用 WKWebView这是在代码中设置的(这是因为 iOS 10 中的 this bug):

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;
}

View 层次结构如下:
enter image description here

我的问题是:
我的代码有什么问题,如何正确执行?

最佳答案

XCTest 可以看到三个正在运行的 WebKit View :

  • WKScrollView
  • WKWebView
  • WKContentView

  • 看起来他们已经改变了元素在 iOS 13 上被赋予类型的方式,这意味着你的 View 层次结构现在包含多个元素,这些元素被归类为 WebView .看来 WKWebViewWKContentView现在都解析为 WebView 类型的元素,而在 iOS 12 中,只有 WKScrollView (在 iOS 13 中,不再解析为被归类为 WebView 元素)被归类为 WebView .

    现在查询:
    shopEasyApp.webViews.element

    查询使用 element ,只有在您知道查询将解析为单个元素时才应该使用。由于 shopEasyApp.webViews 现在有 2 个结果, element 没有一个元素返回。您可以检查是否存在,因为检查 XCUIElementexists属性不需要成功解析查询。但是,所有其他 XCUIElement属性确实需要解析查询,所以当 isHittable使用时,XCTest 引擎尝试解析查询,发现查询未按预期解析为单个元素,并抛出此错误。
    Multiple matching elements found for <XCUIElementQuery: 0x600003efb4d0>

    为了解决这个问题,我建议你修改查询以使用第一个匹配的索引而不是使用 element :
    shopEasyApp.webViews.elementBound(by: 0)

    如果有多个 WebView元素在屏幕上,此查询将选择列表中的第一个元素。

    关于ios13 - iOS 13 Xcode UI 测试自动化类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58578338/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com