UITest录制

来源:互联网 发布:数据口径是什么 编辑:程序博客网 时间:2024/06/01 12:09

使用UITest进行录制的时候,会发现,系统的录制工具超级不好用。录制的大部分代码是无法直接运行的。因此,我们需要手动修改。
本文主要说明一些常用的写法。至于API,请参考XCUIElementQuery API

1、模拟UItableViewCell的点击

[[[app.tables childrenMatchingType:XCUIElementTypeCell] elementBoundByIndex:3] tap];

2、获取当前UIViewController的title

NSString *title = app.navigationBars.element.identifier;

3、点击navigationBar上自定义的返回键

XCUIApplication *app = [[XCUIApplication alloc] init];[[[[app.navigationBars[app.navigationBars.element.identifier] childrenMatchingType:XCUIElementTypeButton] matchingIdentifier:@"返回"] elementBoundByIndex:1] tap];

4、在UIImageView上添加Tap手势,模拟点击事件

对ImgaeView添加两个属性:imageView.isAccessibilityElement = YES;imageView.accessibilityIdentifier = @"imageView";if ([app.images[@"imageView"] exists]){    [app.images[@"imageView"] tap];}

5、UICollectionView上视图的模拟点击事件
例如:这里写图片描述
第一种:通过文字:
if ([tablesQuery.staticTexts[@"我的主页"] exists]) {
[tablesQuery.staticTexts[@"我的主页"] tap];
}

第二种:通过图片:

XCUIElementQuery *tablesQuery = app.tables;if ([tablesQuery.images[@"myTotalImage"] exists]) {      [tablesQuery.images[@"myTotalImage"] tap];    }

6、模拟Slider的滑动

if (app.sliders.element.normalizedSliderPosition == 0.5 || app.sliders.element.normalizedSliderPosition == 1.0) {        [[app.sliders elementBoundByIndex:0] adjustToNormalizedSliderPosition:0.0];        sleep(1);    }    [[app.sliders elementBoundByIndex:0] adjustToNormalizedSliderPosition:0.5];    sleep(1);    [[app.sliders elementBoundByIndex:0] adjustToNormalizedSliderPosition:1.0];    sleep(1);    [[app.sliders elementBoundByIndex:0] adjustToNormalizedSliderPosition:0.5];    sleep(1);    [[app.sliders elementBoundByIndex:0] adjustToNormalizedSliderPosition:0.0];    sleep(1);

暂时写到这里,后续有新的发现会持续更新。

0 0