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

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  
  //…  
  }  

本次测试运行没有问题最多iOS12.

使用 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。看起来 WKWebViewWKContentView 现在都解析为 WebView 类型的元素,而在 iOS 12 中,只有 WKScrollView(在 iOS 13,不再解析为 WebView 元素)被归类为 WebView.

现在查询:

shopEasyApp.webViews.element

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

Multiple matching elements found for <XCUIElementQuery: 0x600003efb4d0>

要解决此问题,我建议您修改查询以使用第一个匹配索引而不是使用 element:

shopEasyApp.webViews.elementBound(by: 0)

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

我遇到过类似的问题 - 没有 webview 的后代。 经过调查发现 iOS 13.1 一切正常,但 iOS 13.3.[=10= 一切正常]

我认为这是模拟器的错误。

对于所有面临相同问题的人 - 指定早期版本 (13.1) 直到它被修复。