为什么XCTest在UITableViewCell中公开随机的otherElement? (并表现出其他不一致之处)

问题描述 投票:0回答:1

我创建了一个新的单视图项目,并添加了此代码以创建UITableView。


- (void)viewDidLoad {
    [super viewDidLoad];
    tableView=[[UITableView alloc]init];
    tableView.frame = CGRectMake(10,30,320,400);
    tableView.dataSource=self;
    tableView.delegate=self;
    tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    [tableView reloadData];
    [self.view addSubview:tableView];
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier   forIndexPath:indexPath] ;
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = @"hello";
    cell.textLabel.accessibilityIdentifier = @"world";
    return cell;
}

[我的UI测试仅使用print(app.debugDescription)打印视图层次结构,并且您可以看到主窗口的图形具有2个叶视图:对应于StaticTextcell.textLabel和未识别/随机的Other视图。我已经尝试了多种方法来弄清楚这个Other视图是什么,但是答案一直困扰着我。


Element subtree:
 →Application, 0x600000d98820, pid: 17292, label: 'TestApp'
    Window (Main), 0x600000d989c0, {{0.0, 0.0}, {768.0, 1024.0}}
      Other, 0x600000d98a90, {{0.0, 0.0}, {768.0, 1024.0}}
        Table, 0x600000d98b60, {{10.0, 30.0}, {320.0, 400.0}}
          Cell, 0x600000d98c30, {{10.0, 30.0}, {320.0, 44.0}}
            StaticText, 0x600000d98d00, {{25.0, 30.0}, {290.0, 43.5}}, identifier: 'world', label: 'hello'
            Other, 0x600000d98dd0, {{25.0, 73.5}, {305.0, 0.5}}

也许更多this UITableViewCell odd/unexplained behavior

另一个使我感到奇怪的是,当将包含UIScrollViewUILabel添加到UITableViewCell时,UIScrollView不会出现在层次结构中。所以这个:


- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier   forIndexPath:indexPath] ;
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 5,5)];
    [lb setText:@"hello world"];
    [sv addSubview:lb];
    [[cell contentView] addSubview:sv];
    return cell;
}

产生此结果:

Element subtree:
 →Application, 0x6000014e75a0, pid: 20372, label: 'TestApp'
    Window (Main), 0x6000014e7190, {{0.0, 0.0}, {768.0, 1024.0}}
      Other, 0x6000014e6ff0, {{0.0, 0.0}, {768.0, 1024.0}}
        Table, 0x6000014e6f20, {{10.0, 30.0}, {320.0, 400.0}}
          Cell, 0x6000014e6e50, {{10.0, 30.0}, {320.0, 44.0}}
            StaticText, 0x6000014e6d80, {{10.0, 30.0}, {5.0, 5.0}}, label: 'hello world'
            Other, 0x6000014e6cb0, {{25.0, 73.5}, {305.0, 0.5}}

而此(不涉及表或单元格:

- (void)viewDidLoad {
    [super viewDidLoad];
    UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 5,5)];
    [lb setText:@"hello world"];
    [sv addSubview:lb];
    [self.view addSubview:sv];
}

似乎可以正常工作:


Element subtree:
 →Application, 0x600000278750, pid: 20442, label: 'TestApp'
    Window (Main), 0x6000002788f0, {{0.0, 0.0}, {768.0, 1024.0}}
      Other, 0x600000278820, {{0.0, 0.0}, {768.0, 1024.0}}
        ScrollView, 0x6000002789c0, {{0.0, 0.0}, {10.0, 10.0}}
          StaticText, 0x600000278a90, {{0.0, 0.0}, {5.0, 5.0}}, label: 'hello world'

我的想法是,这全都与isAccessibilityElementaccessibilityTraits的方式有关,并且视图是否是容器来确定暴露了什么,但不一致之处令人困惑,我希望其他人知道秘密公式。

ios objective-c uitableview xctest xcuitest
1个回答
1
投票

另一个元素是单元格之间的分隔线。由于这些行不会影响其他元素,因此它们不应打扰您。

当您使某些元素可访问时,其后代不会出现在UI测试元素树中。

© www.soinside.com 2019 - 2024. All rights reserved.