IOS自动化过程与给元素定义唯一ID

来源:互联网 发布:淘宝太坑视频妹子下载 编辑:程序博客网 时间:2024/05/21 23:33

IOS自动化过程与给元素定义唯一ID

经尝试,发现IOS常用控件(UIButton、UITextField、UIImage、UISlider、UISwitch、UITableViewCell、UILabel)
如果需要添加ID 只需要一行代码即可,不影响逻辑且不影响显示:实例如下
btn.accessibilityLabel = @"refreshbutton";
ID添加尽量在同一个页面中 重要元素不要有相同即可
这对自动化的精确写作、精确执行帮助较大  
几种常用控件初始化如下:


UIButton *btn;
    btn = [[UIButton alloc]initWithFrame:CGRectMake(20, 335, 80, 20)];
    btn.accessibilityLabel = @"refreshbutton";
    btn.backgroundColor = [UIColor greenColor];
    [btn setTitle:@"按钮" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(refresh) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    UITextField *text;
    text = [[UITextField alloc]initWithFrame:CGRectMake(170, 335, 180, 20)];
    text.placeholder = @"这是一个输入框";
    text.accessibilityLabel = @"inputtext01";
    text.textColor = [UIColor redColor];
    text.secureTextEntry = YES;
    [self.view addSubview:text];
    
    UIImage * image =[UIImage imageNamed:@"icon.png"];
    UIImageView * imageview = [[UIImageView alloc]initWithFrame:CGRectMake(20, 360, 80, 20)];
    imageview.accessibilityLabel = @"imagetype01";
    imageview.backgroundColor = [UIColor blueColor];
    imageview.image = image;
    [self.view addSubview:imageview];
    
    UISlider * slider;
    slider = [[UISlider alloc]initWithFrame:CGRectMake(170, 360, 80, 20)];
    slider.accessibilityLabel = @"slider01";
    [self.view addSubview:slider];
    
    UISwitch * switc;
    switc = [[UISwitch alloc]initWithFrame:CGRectMake(20, 385, 80, 20)];
    switc.accessibilityLabel = @"switc01";
    [self.view addSubview:switc];
    
    UITableViewCell * cell;
    cell = [[UITableViewCell alloc]initWithFrame:CGRectMake(170, 385, 80, 20)];
    cell.accessibilityLabel = @"cell01";
    cell.textLabel.text = @"这是一个cell";
    [self.view addSubview:cell];


    
    UILabel *label;
    label = [[UILabel alloc] initWithFrame:CGRectMake(20, 35, 150, 20)];
    label.text = @"平台型号:";
    label.accessibilityLabel = @"platformshap";
    [self.view addSubview:label];
[label release];


原创粉丝点击