【问题标题】:How to access elements nested in UIStackView within MkMapView for UITests?如何访问嵌套在 UITests 的 MkMapView 中的 UIStackView 中的元素?
【发布时间】:2020-04-03 08:53:24
【问题描述】:

我想为 UIStackView 内的视图编写 UI 测试,即 MKMapView 内的视图。 这是MWE的sn-p。我创建了一个 MKMapView 添加了一个 UIStackView 和两个 UIViews 作为子视图。

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

但是,在应用程序元素子树中没有可见的堆栈视图的子视图,也无法访问它们。 您可以看到带有标识符“stackView”的UIStackView,但看不到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}}

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

【问题讨论】:

  • 我也遇到了这个奇怪的问题。非常令人沮丧。

标签: ios swift xcode testing uitest


【解决方案1】:

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

rootStackView.accesibilityElements[nestedElementsThatNeedToMakeAccessible]

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-25
    • 2013-01-12
    • 2019-02-08
    • 2017-07-19
    • 2017-03-27
    • 1970-01-01
    • 2016-07-16
    • 1970-01-01
    相关资源
    最近更新 更多