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

Rei*_*ner 4 xcode-ui-testing ios13 xcode11

我的应用程序使用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
  //…
}  
Run Code Online (Sandbox Code Playgroud)

我的 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  
  //…  
  }  
Run Code Online (Sandbox Code Playgroud)

此测试运行到 iOS 12 都没有问题。

在 iOS 13 中,它在测试时停止webView.isHittable并出现以下错误:

Failed to get matching snapshot: Multiple matching elements found for <XCUIElementQuery: 0x600000bdfbb0>.  
Run Code Online (Sandbox Code Playgroud)

日志说:

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;
}  
Run Code Online (Sandbox Code Playgroud)

视图层次结构如下:
在此处输入图片说明

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

Ole*_*tha 6

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

  • WKScrollView
  • WKWebView
  • WKContentView

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

现在查询:

shopEasyApp.webViews.element
Run Code Online (Sandbox Code Playgroud)

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

Multiple matching elements found for <XCUIElementQuery: 0x600003efb4d0>
Run Code Online (Sandbox Code Playgroud)

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

shopEasyApp.webViews.elementBound(by: 0)
Run Code Online (Sandbox Code Playgroud)

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