我正在制作一个“每个步骤之前”的步骤,我想要执行注销步骤。在尝试触摸它之前,我找不到关于检查元素是否存在的任何内容,如果它不存在则执行其他操作。是否可以使用KIF执行此操作而无需引用我想要检查的对象?
类似的东西:
if([tester elementExistsWithAccesibilityLabel:@"backButton"])
{
[tester tapViewWithAccessibilityLabel:@"backButton"];
}
else
{
[tester tapViewwithAccesibilityLabel:@"Logout"];
}
答案 0 :(得分:19)
我建议尝试这种方法:
if([[[UIApplication sharedApplication] keyWindow] accessibilityElementWithLabel:@"backButton"] != nil) {
[tester tapViewWithAccessibilityLabel:@"backButton"];
} else {
[tester tapViewWithAccessibilityLabel:@"Logout"];
}
答案 1 :(得分:5)
这些方法可以解决问题。将它们添加到KIFUITestActor的类别中。
#import "UIApplication-KIFAdditions.h"
#import "UIAccessibilityElement-KIFAdditions.h"
#import "NSError-KIFAdditions.h"
- (BOOL)existsViewWithAccessibilityLabel:(NSString *)label
{
UIView *view = nil;
UIAccessibilityElement *element = nil;
return [self existsAccessibilityElement:&element view:&view withLabel:label value:nil traits:UIAccessibilityTraitNone tappable:YES];
}
- (BOOL)existsAccessibilityElement:(UIAccessibilityElement **)element view:(out UIView **)view withLabel:(NSString *)label value:(NSString *)value traits:(UIAccessibilityTraits)traits tappable:(BOOL)mustBeTappable
{
KIFTestStepResult (^executionBlock)(NSError **) = ^(NSError **error) {
return [UIAccessibilityElement accessibilityElement:element view:view withLabel:label value:value traits:traits tappable:mustBeTappable error:error] ? KIFTestStepResultSuccess : KIFTestStepResultWait;
};
NSDate *startDate = [NSDate date];
KIFTestStepResult result;
NSError *error = nil;
NSTimeInterval timeout = 10.0;
while ((result = executionBlock(&error)) == KIFTestStepResultWait && -[startDate timeIntervalSinceNow] < timeout) {
CFRunLoopRunInMode([[UIApplication sharedApplication] currentRunLoopMode] ?: kCFRunLoopDefaultMode, 0.1, false);
}
if (result == KIFTestStepResultWait) {
error = [NSError KIFErrorWithUnderlyingError:error format:@"The step timed out after %.2f seconds: %@", timeout, error.localizedDescription];
result = KIFTestStepResultFailure;
}
return (result == KIFTestStepResultSuccess) ? YES : NO;
}
它非常适合我。
答案 2 :(得分:3)
如果有人还在寻找答案,那么KIF中有一系列方法就是KIFUITestActor-ConditionalTests.h
:
- (BOOL)tryFindingViewWithAccessibilityLabel:(NSString *)label error:(out NSError **)error;
- (BOOL)tryFindingViewWithAccessibilityLabel:(NSString *)label traits:(UIAccessibilityTraits)traits error:(out NSError **)error;
- (BOOL)tryFindingViewWithAccessibilityLabel:(NSString *)label value:(NSString *)value traits:(UIAccessibilityTraits)traits error:(out NSError **)error;
- (BOOL)tryFindingTappableViewWithAccessibilityLabel:(NSString *)label error:(out NSError **)error;
- (BOOL)tryFindingTappableViewWithAccessibilityLabel:(NSString *)label traits:(UIAccessibilityTraits)traits error:(out NSError **)error;
- (BOOL)tryFindingTappableViewWithAccessibilityLabel:(NSString *)label value:(NSString *)value traits:(UIAccessibilityTraits)traits error:(out NSError **)error;
- (BOOL)tryFindingAccessibilityElement:(out UIAccessibilityElement **)element view:(out UIView **)view withIdentifier:(NSString *)identifier tappable:(BOOL)mustBeTappable error:(out NSError **)error;
- (BOOL)tryFindingAccessibilityElement:(out UIAccessibilityElement **)element view:(out UIView **)view withElementMatchingPredicate:(NSPredicate *)predicate tappable:(BOOL)mustBeTappable error:(out NSError **)error;
如果您正在使用辅助功能标识符添加(pod 'KIF/IdentifierTests'
),那么还有一个非常方便的等效方法:
- (BOOL) tryFindingViewWithAccessibilityIdentifier:(NSString *) accessibilityIdentifier;
答案 3 :(得分:2)
对于swift 3:
a.butter.com
答案 4 :(得分:1)
您可以尝试这样的事情:
@try {
if([tester waitForViewWithAccessibilityLabel:@"backButton"])
{
[tester tapViewWithAccessibilityLabel:@"backButton"];
}
@catch (NSException *exception )
{
[tester tapViewwithAccesibilityLabel:@"Logout"];
}
@finally
{
NSLOG(@"User logged out.");
}
答案 5 :(得分:0)
我做了这个功能似乎在捏了一把,但如果多个集成测试同时依赖于全局默认超时,它会有一些问题。到目前为止,似乎对我有用。
static CGFloat CPKIFDefaultTimeout = 2
- (BOOL)elementExistsWithAccessibilityLabel:(NSString *)accessibilityLabel {
[KIFTestActor setDefaultTimeout:0.0];
BOOL result = [tester waitForViewWithAccessibilityLabel:accessibilityLabel] ? YES : NO;
[KIFTestActor CPKIFDefaultTimeout];
return result;
}
答案 6 :(得分:0)
答案 7 :(得分:0)
如果使用Swift,您可以简单地使用viewTester().tryFindingView()
:
if viewTester().usingLabel("backButton").tryFindingView() {
// back button exists
viewTester().usingLabel("backButton").tap()
} else {
// back button does not exist
viewTester().usingLabel("Logout").tap()
}