我用XCTest编写了相当复杂的UI测试,最近改用了EarlGrey,因为它速度快得多,而且更可靠--测试在构建服务器上不是随机失败的,测试套件可能需要半个小时才能运行!
在EarlGrey中我还不能做的一件事,我可以在XCTest中做,那就是随机选择一个元素。
例如,在日历collectionView上,我可以使用NSPredicate查询所有带有“标识符”的collectionViewCell,然后使用[XCUIElementQuery count]随机选择一天来获取索引,然后选择tap。
现在,我将对其进行硬编码,但我希望随机选择日期,这样如果我们更改应用程序代码,我就不必重写测试了。
请让我知道,如果我可以详细,期待解决这一问题!
发布于 2017-01-26 00:29:13
步骤1编写了一个匹配器,该匹配器可以使用GREYElementMatcherBlock计算元素匹配的给定匹配器。
- (NSUInteger)elementCountMatchingMatcher:(id<GREYMatcher>)matcher {
__block NSUInteger count = 0;
GREYElementMatcherBlock *countMatcher = [GREYElementMatcherBlock matcherWithMatchesBlock:^BOOL(id element) {
if ([matcher matches:element]) {
count += 1;
}
return NO; // return NO so EarlGrey continues to search.
} descriptionBlock:^(id<GREYDescription> description) {
// Pass
}];
NSError *unused;
[[EarlGrey selectElementWithMatcher:countMatcher] assertWithMatcher:grey_notNil() error:&unused];
return count;
}步骤2使用%选择随机索引
NSUInteger randomIndex = arc4random() % count;步骤3最后使用atIndex:选择该随机元素并对其执行操作/断言。
// Count all UIView's
NSUInteger count = [self elementCountMatchingMatcher:grey_kindOfClass([UIView class])];
// Find a random index.
NSUInteger randIndex = arc4random() % count;
// Tap the random UIView
[[[EarlGrey selectElementWithMatcher:grey_kindOfClass([UIView class])]
atIndex:randIndex]
performAction:grey_tap()];https://stackoverflow.com/questions/41863692
复制相似问题