首页 > 解决方案 > 如何访问嵌套在 UITests 的 MkMapView 中的 UIStackView 中的元素?

问题描述

我想为内部的视图编写 UIUIStackView测试MKMapView。这是 MWE 的一个片段。我创建了一个MKMapView添加了UIStackView两个UIViews 作为子视图的 a。

let map = MKMapView()
let view1 = UIView()
let view2 = UIView()
let stackView = UIStackView()
stackView.addArrangedSubview(view1)
stackView.addArrangedSubview(view2)
map.addSubview(stackView)
view.addSubview(map)

accessibilityIdentifier为每个元素声明了一个并将isAccessibilityElement属性设置为true。

view1.accessibilityIdentifier = "view1"
view2.accessibilityIdentifier = "view2"
stackView.accessibilityIdentifier = "stackView"
view1.isAccessibilityElement = true
view2.isAccessibilityElement = true
stackView.isAccessibilityElement = true

但是,在应用程序元素子树中没有可见的堆栈视图的子视图,也无法访问它们。您可以看到UIStackView带有标识符“stackView”的 ,但看不到UIViews.

Element subtree:
 →Application, 0x6000030080e0, pid: 11856, label: 'UITestWithStackView'
    Window (Main), 0x600003009500, {{0.0, 0.0}, {414.0, 896.0}}
      Other, 0x600003009420, {{0.0, 0.0}, {414.0, 896.0}}
        Other, 0x600003009340, {{0.0, 0.0}, {414.0, 896.0}}
          Other, 0x600003009260, {{0.0, 0.0}, {414.0, 896.0}}
            Other, 0x600003009180, {{0.0, 0.0}, {414.0, 896.0}}
              Other, 0x6000030090a0, {{7.0, 348.0}, {400.0, 200.0}}, identifier: 'stackView'
              Other, 0x600003008fc0, {{0.0, 0.0}, {414.0, 896.0}}
                Map, 0x600003008ee0, {{0.0, 0.0}, {414.0, 896.0}}
                Other, 0x600003008e00, {{0.0, 0.0}, {414.0, 896.0}}
              Link, 0x600003008d20, {{375.4, 841.3}, {28.6, 10.7}}, label: 'Legal'
    Window, 0x600003008c40, {{0.0, 0.0}, {414.0, 896.0}}
      Other, 0x600003008b60, {{0.0, 0.0}, {414.0, 896.0}}
        Other, 0x600003008a80, {{0.0, 0.0}, {414.0, 896.0}}

这种行为是否有任何已知的解决方法?

标签: iosswiftxcodetestinguitest

解决方案


嵌套堆栈视图有同样的问题。在我的情况下,它是一个带有标签的stackView,它嵌套在另外两个堆栈中。我需要访问标签。使用 AccessiblityElements 有助于访问较低堆栈中的元素。

rootStackView.accesibilityElements[nestedElementsThatNeedToMakeAccessible]

如果我正确理解你的情况,试试这个

stackView.accessibilityElements[view1, view2]
map.accessibilityElements[stackView]

推荐阅读