【问题标题】:iOS 13 Xcode UI test Automation type mismatchiOS 13 Xcode UI 测试自动化类型不匹配
【发布时间】:2019-10-27 10:04:13
【问题描述】:

我的应用程序使用在代码中设置的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;
}  

视图层次结构如下:

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

【问题讨论】:

    标签: ios13 xcode-ui-testing xcode11


    【解决方案1】:

    您拥有三个 XCTest 可以看到的 WebKit 视图:

    • WKScrollView
    • WKWebView
    • WKContentView

    看起来他们已经改变了在 iOS 13 上为元素指定类型的方式,这意味着您的视图层次结构现在包含多个元素,这些元素被归类为 WebView。看起来WKWebViewWKContentView 现在都解析为WebView 类型的元素,而在iOS 12 中,只有WKScrollView(在iOS 13 中不再解析为WebView element) 被归类为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 元素,此查询将选择列表中的第一个元素。

    【讨论】:

    • 感谢您的回答。我尝试了shopEasyApp.webViews[0],但这给出了错误Expression type 'XCUIElementQuery' is ambiguous without more context. 但是,shopEasyApp.webViews.firstMatch 确实有效。如果需要,也许您可​​以更新您的答案。
    • 糟糕,我实际上建议使用elementBound(by: 0) 而不是firstMatch,因为firstMatch 仅适用于只有一个结果时使用,以加快解析速度。我会更新我的答案:)
    • @Oletha 是elementBound(by: 0) XCTest 中的一个方法,因为我找不到这种类型的任何方法
    • XCUIElementQuery 对象上的一个方法
    【解决方案2】:

    我也遇到过类似的问题 - 没有 webview 的后代。 经过调查,发现在 iOS 13.1 上一切正常,但在 iOS 13.3 上却不行。

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

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

    【讨论】:

      猜你喜欢
      • 2013-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      相关资源
      最近更新 更多