IOS无线客户端自动化测试

做IOS自动化的过程中,会遇到两个问题,需要开发对代码的支持。
1. 用instruments总是会获取不到一些UI元素
2. 客户端中没有固定的ID供测试代码来确定页面元素。然后只能通过target.frontMostApp().mainWindow().scrollViews()[0].tableViews()[0].cells()[0].tap(); 这样的方式来定位元素。

第一个问题产生的原因是在客户端开发的过程,需要自定义大量的UIView来满足需求。这些UI有两种构建方式,
     1. 用原生的UI组件来拼接
     2. 通过一些自己来画
instruments 是通过元素的isAccessibilityElement属性来判定元素是否可见。
解决方案:
1. 原生的UI组件的isAccessibilityElement默认是YES的。自定义的UI组件的isAccessibilityElement属性是NO,当isAccessibilityElement为NO时,instruments将无法捕获。而且苹果的UI还有覆盖性之说,所以这种情况,我们需要将自定义的 UI的子UI的isAccessibilityElement属性置为YES,而不是自定义的UI本身,instruments就能获取到。


@interface SNCommentHeaderView : UIView
{
    UIImageView    *headerBgView;
    UILabel        *titleLabel;

}
- (id)initWithFrame:(CGRect)frame
{
    NSLog(@"SNCommentHeaderView_init");
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        headerBgView = [[UIImageView alloc] initWithImage:[UIImage skinImageNamed:@"comment_sectionheader_title_bg.png"]];
        CGRect bgFrame = headerBgView.frame;
        bgFrame.origin.y = 10;
        headerBgView.frame = bgFrame;
        headerBgView.isAccessibilityElement = YES;
        [self addSubview:headerBgView];
       
        titleLabel = [[UILabel alloc] initWithFrame:UIEdgeInsetsInsetRect(bgFrame, UIEdgeInsetsMake(0, 0, 1.5, 0))];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.font = [UIFont fontWithName:BOLD_FONT_NAME size:12];
        titleLabel.textAlignment = UITextAlignmentCenter;
        titleLabel.textColor = [UIColor skinColorForKey:SNSkinCommentSectionHeaderColor];
        titleLabel.isAccessibilityElement = YES;
        [self addSubview:titleLabel];
    }
    return self;

}


2. 非原生控件成的UI,instruments暂时是不支持获取的。在苹果官方文档中又一套非正式的创建自定义UI的方式。针对这种情况,只能通过去定位他的父节点定位了。如果需要验证的话,可以将自定义UI的内容通过设置accessibilityIdentifier或者accessibilityLabel,然后通过父节点取得值来验证。


做IOS自动化,只能通过target.frontMostApp().mainWindow().scrollViews()[0].tableViews()[0].cells()[0].tap(); 这样来定位坐标是个很原始的方式,而且需要变化比较快,一旦UI方式改变,case会受到很大的影响。所以假使在源码中通过对一些比较固定或case中功能点的元素设置accessibilityIdentifier,然后脚本可以通过这accessibilityIdentifier来定位元素,可以大大减少自动化脚本的维护成本,也可大大提高开发速度。


ps:苹果官方对于可见性的文档 https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/iPhoneAccessibility/Accessibility_on_iPhone/Accessibility_on_iPhone.html#//apple_ref/doc/uid/TP40008785-CH100-SW1

原文地址:https://www.cnblogs.com/dw729/p/3370488.html