UI 测试 Xcode 7- 无法访问子视图中的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32801267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:56:48  来源:igfitidea点击:

UI Testing Xcode 7- can't access element within subview

iosxcodeswift2xcode7xcode-ui-testing

提问by sebradloff

I'm trying to access an element within a subview and I'm finding it impossible to do so.

我正在尝试访问子视图中的元素,但我发现这是不可能的。

The hierarchy being:

层次结构是:

View Controller:

视图控制器:

  • View
    • tempView
    • userEnterView
      • zipCodeEntered
  • 看法
    • 临时视图
    • 用户输入视图
      • 邮政编码输入

I want to access the zipCodeEntered text field. I have an accessibility label on it named "zipCodeEntered".

我想访问 zipCodeEntered 文本字段。我有一个名为“zipCodeEntered”的可访问性标签。

When I try and record the automation, it only register the superview "userEnterView" and not the actual text field which I can tap into.

当我尝试记录自动化时,它只注册超级视图“userEnterView”,而不是我可以利用的实际文本字段。

I print "app.otherElements[SUPER_VIEW_NAME].debugDescription" to see what elements are in that hierarchy and it prints none.

我打印“app.otherElements[SUPER_VIEW_NAME].debugDescription”以查看该层次结构中的元素,但它不打印任何元素。

Any ideas as to why I can not access these elements/how I can access them? enter image description here

关于为什么我无法访问这些元素/如何访问它们的任何想法? 在此处输入图片说明

回答by quellish

The subviews are not accessible because the parent view is not a container view. From the section Making Your iOS App Accessiblefrom the Accessibility Programming Guide:

子视图不可访问,因为父视图不是容器视图。来自访问性编程指南中的“使您的 iOS 应用程序可访问”部分:

From the perspective of accessibility, a custom view is either an individual view or a container view. An individual view does not contain any other views that need to be accessible.

A container view, on the other hand, contains other elements with which users can interact.

从可访问性的角度来看,自定义视图要么是单个视图,要么是容器视图。单个视图不包含任何其他需要访问的视图。

另一方面,容器视图包含用户可以与之交互的其他元素。

To make the subviews accessible the parent view should tell UIAccessibilitythat it is an accessibility container by returning NO from -isAccessibilityElementand implementing the methods of the UIAccessibilityContainerprotocol.

为了使子视图可访问,父视图应该UIAccessibility通过返回 NO-isAccessibilityElement并实现UIAccessibilityContainer协议的方法来告诉它是一个可访问性容器。

回答by Patrick Ridd

The thing that fixed it for me was to check √ the "Accessibility" box to enabled in Interface Builder.

为我修复它的事情是选中 √ 在 Interface Builder 中启用的“辅助功能”框。

enter image description here

在此处输入图片说明

If not working in IB try setting the the view's isAccessibilityElementproperty to true.

如果不在 IB 中工作,请尝试将视图的isAccessibilityElement属性设置为 true。

Hope that helps!

希望有帮助!

回答by Andres Wang

I also encountered this problem in my UITableView. My cell has a containerView(UIView) which has subviews(TextView, UIButton...) and a collectionView with its cells.

我在 UITableView 中也遇到过这个问题。我的单元格有一个 containerView(UIView),它有 subviews(TextView, UIButton...) 和一个带有单元格的 collectionView。

I tried the accepted answer, but it didn't work.(UI Recorder or manual code didn't recognize my sub-elements). But sometimes I found the UITest suddenly recognized my sub-elements when I triggered something to refresh the table.

我尝试了接受的答案,但没有奏效。(UI Recorder 或手动代码无法识别我的子元素)。但有时我发现当我触发某些东西刷新表格时,UITest 会突然识别出我的子元素。

So right now I know if UITest can't find my subviews, I invoke tableView.reloadDatato refresh the view, and then all the problem is gone.

所以现在我知道如果 UITest 找不到我的子视图,我调用tableView.reloadData刷新视图,然后所有问题都消失了。

Anyway, this is just a workaround, I hope Apple can fix it as soon as possible.

无论如何,这只是一种解决方法,我希望苹果能够尽快修复它。