iOS UI Automation元素不会找到子元素 [英] iOS UI Automation element finds no sub-elements

查看:92
本文介绍了iOS UI Automation元素不会找到子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用iOS自动化的iOS应用程序,我已经遇到了麻烦。我无法附加屏幕截图,所以我会尽力描述我的场景。

I'm just starting out with UI Automation for my iOS app and am already having trouble. I'm unable to attach screen shots so I'll do my best to describe my scenario.

我正在为iOS 6.0构建并使用故事板。该应用程序启动到带有导航控制器的屏幕。根视图控制器包含一个主视图,该视图具有占据屏幕底部60%的1个UIView子视图和位于该子视图上方的分段控件。我能够为主视图配置可访问性(标签mainview)。然后我能够在我的测试中找到这个元素没问题。但是,我现在无法找到分段控制器。所以我决定从我的mainview元素中注销elements()和segementedControls()的长度,每个数组的长度为0.所以当某个测试运行我的应用程序时,它说没有子 - 我的主视图上的元素。

I'm building for iOS 6.0 and am using a storyboard. The app launches to a screen with a navigation controller. The root view controller contains a main view that has 1 UIView subview that takes up the bottom 60% of the screen and a segmented control that sits above that subview. I was able to configure accessibility for the main view (label "mainview"). I am then able to locate this element in my test no problem. However, I am now having trouble finding the segmented controller. So I decided to log out the length of "elements()" and "segementedControls()" from my "mainview" element and the length of each array is 0. So somehow when the test is running my app it's saying there are no sub-elements on my main view.

另外需要注意的是,我在故事板编辑器的身份检查器中找不到任何可访问性部分用于分段控件。但是我暂时在主视图中添加了一个按钮,并使用辅助功能标签对其进行了配置,以测试在运行我的测试时,elements()或者按钮()调用是否随后会显示主视图的元素,但是这些数组仍然是返回为空,即使按下按钮。

Another thing to note is that I could not find any accessibility section in the identity inspector of the storyboard editor for the segmented control. However I temporarily added a button to my main view and configured that with an accessibility label, just to test if the elements() or buttons() calls would subsequently show an element for the main view when running my test, but these arrays were still returning as empty, even with the button.

这是我的脚本:

var target = UIATarget.localTarget();
var app = target.frontMostApp();

function selectListView() {
var testName = "selectListView";

UIALogger.logStart(testName);

var view = app.mainWindow().elements()["mainview"];
if (!view.isValid()) {
    UIALogger.logFail("Could not locate main view");    
}

UIALogger.logMessage("Number of elements for sub element: " + view.elements().length);

var segmentedControl = view.segmentedControls()[0];
if (!segmentedControl.isValid()) {
    UIALogger.logFail("Could not locate segmented control on physician collection parent view");
}

var listButton = segmentedControl.buttons()[1];
if (!listButton.isValid()) {
    UIALogger.logFail("Could not locate list button on segemented controller on physician collection parent view"); 
}

UIALogger.logMessage("Tapping List button on physician collection view's segmented control");
listButton.tap();

UIALogger.logPass(testName);
}

selectListView();

非常感谢任何帮助。

编辑:我将此添加到我的脚本中以从主窗口搜索整个视图层次结构,在initWithCoder中为我的分段控件设置辅助功能标签值(因为我似乎无法在故事板编辑器中为分段控件设置一个,因为我之前说过)并且仍然找不到元素 - 就好像它不在视图层次结构中,虽然它在屏幕上并且运行正常:

I added this to my script to search the entire view hierarchy from the main window, set an accessibility label value for my segmented control in initWithCoder (since I don't seem able to set one in the storyboard editor for a segmented control, as I stated earlier) and still could not find the element - it's as though it's just not in the view hierarchy, though it's on the screen and functions just fine:

function traverse(root, name) {
if (root.name() == name) {
    return root;
}

var elements = root.elements();

for (var i = 0; i < elements.length; i++) {
    var e = elements[i];

    var result = traverse(e, name);
    if(result != null) {
        return result;
    } 
}

return null;
}

function selectListView() {
var testName = "selectListView";

var segmentedControl = traverse(UIATarget.localTarget().frontMostApp().mainWindow(), "mysegementedcontrol");
if (segmentedControl == null) {
    UIALogger.logMessage("Still could not find it");
}

....
}

编辑:添加了对app.logElementTree()的调用,仍然没有看到分段控件(PhysicianCollectionParentView是我的主视图 - 你可以看到,那里没有子元素):

Added call to app.logElementTree() and still no segmented control in sight ("PhysicianCollectionParentView" is my "mainview" - you can see, no sub-elements there):

编辑:这是一些屏幕截图。第一个显示我的主视图控制器。接下来显示除了分段控件之外,还有一个UIView子视图。第3个显示了我的故事板中应用程序的基本入口点。

Here are some screen shots. The first shows my "master" view controller. The next shows that in addition to the segmented control there is also a UIView subview. The 3rd shows the basic entry point for the app in my storyboard.

这里是我的主视图控制器的类扩展,显示分段控件和其他UIView子视图的出口:

Here is the class extension for my "master" view controller here, showing the outlets for the segmented control and the other UIView subview:

@interface PhysicianCollectionMasterViewController ()
@property (strong, nonatomic) IBOutlet UISegmentedControl *viewSelectionControl;
@property (strong, nonatomic) IBOutlet UIView *physicianCollectionView;
@end

编辑:这里有一些非常有趣的东西 - 我决定全新的在乐器中创建的脚本并利用记录功能。当我点击我的分段控件时,这里是它创建的JavaScript,告诉我它是如何访问我的分段控件上的一个按钮的:

Here's something very interesting - I decided to go with a brand new script created within instruments and take advantage of the record feature. When I clicked on my segmented control, here's the JavaScript it created to show me how it had accessed one of the buttons on my segmented control:

var target = UIATarget.localTarget();

target.frontMostApp().mainWindow().elements()["PhysicianCollectionParentView"].tapWithOptions({tapOffset:{x:0.45, y:0.04}});

所以,我猜最糟糕的情况我可以选择这样的东西,但它没有任何意义对我而言,UI Automation并不认为控件存在。这么奇怪。必须有一些我缺少的东西,但我的设置是如此基本,我无法想象它可能是什么。

So, I guess worst-case I could go with something like this, but it just makes no sense to me that UI Automation just does not think that the control exists. So strange. There must be something I'm missing but my setup is so basic I can't imagine what it could be.

推荐答案

为元素设置accessibilityLabel并标记它 isAccessibilityElement = YES; 隐藏该元素的子视图。对于自动化,您应该使用accessibilityIdentifier而不是accessibilityLabel并设置 isAccessibilityElement = NO;

When you set accessibilityLabel for an element and flag it isAccessibilityElement = YES; the subviews of that element are hidden. For automation, you should use accessibilityIdentifier instead of accessibilityLabel and set isAccessibilityElement = NO;

在您的目标C代码之后提交physicianCollectionView,删除标签和辅助功能标志,然后执行此操作:

In your objective C code after physicianCollectionView is rendered, remove the label and accessibility flag and do this instead:

physicianCollectionView.accessibilityIdentifier = @"PhysicianCollectionParentView";
physicianCollectionView.isAccessibilityElement = NO;

对于元素树中没有子视图的最后元素,设置 isAccessibilityElement = YES;

For last elements in element tree, which do not have sub views, set isAccessibilityElement = YES;

这篇关于iOS UI Automation元素不会找到子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆