我想将 UI 中的文本与使用 KIF V3.0 从 WebService 中获取的内容进行比较。我知道如何在 KIF 1.0 中进行比较,但我不知道如何在 KIF V3.0 中进行比较。
对于V1.0
+(id)stepToverifyOutput:(NSString *)预期标签可访问性:(NSString *)mylabel
{
NSString *description = [NSString stringWithFormat:@"Verify label text for %@",expectedLabel];
return [self stepWithDescription:description executionBlock:^(KIFTestStep *step, NSError **error) {
UIAccessibilityElement *element = [[UIApplication sharedApplication] accessibilityElementWithLabel:mylabel];
BOOL isValid;
UINavigationController *navbar;
UILabel *lblName;
if([element isKindOfClass:[UILabel class]]){
isValid=YES;
lblName = (UILabel *)[UIAccessibilityElement viewContainingAccessibilityElement:element];;
if ([expectedLabel isEqualToString:lblName.text]) {
return KIFTestStepResultSuccess;
}
}else{
isValid=NO;
navbar = (UINavigationController *)[UIAccessibilityElement viewContainingAccessibilityElement:element];;
if ([expectedLabel isEqualToString:navbar.title]) {
return KIFTestStepResultSuccess;
}
}
KIFTestCondition(NO, error, @"Failed to compare the label text: expected '%@', actual '%@'", expectedLabel,(isValid)?lblName.text:navbar.title);
}];
}
请为我做需要的事情。我被困在这个地方。
最佳答案
KIF 的 [tester waitForViewWithAccessibilityLabel:myLabel]
方法返回实际 View 本身,然后您可以使用它来断言。例如:
NSString *accessibilityLabel = @"yourLabel";
NSString *expectedValue = @"whateveryouexpect";
UILabel *label = (UILabel*)[tester waitForViewWithAccessibilityLabel:accessibilityLabel];
XCTAssert([label.text isEqualToString:expectedValue], @"BADONKADONK");
您不需要为此运行整个步骤。这还有一个优点是更具可读性和直接性:获取标签,确保其文本值符合预期。如果没有找到该值,那么测试自然会失败。
关于ios - 如何比较 KIF 3.1.1 中 UI 中的文本和从 Web 服务接收的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27862468/