Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Step for arbitrary logic on element #148

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions Classes/KIFTestStep.h
Expand Up @@ -71,6 +71,15 @@ typedef NSInteger KIFTestStepResult;
*/
typedef KIFTestStepResult (^KIFTestStepExecutionBlock)(KIFTestStep *step, NSError **error);

/*!
@typedef KIFTestStepExecutionBlockWithView
@param step The step object itself. This is passed back to the block to ensure that there is a pointer to the fully initialized step at the time of execution.
@param view The view which the step targets. The block should perform the logic of the step and can assume the view has the properties specified during the step construction.
@param error An error to fill out in the case of a failure or wait condition. Filling out this error is mandatory in these cases to ensure that testing output is useful.
@result A test result code. Returning KIFTestStepResultWait will cause the step to be tried again on the next iteration.
*/
typedef KIFTestStepResult (^KIFTestStepExecutionBlockWithView)(KIFTestStep *step, UIView *view, NSError **error);

/*!
@class KIFTestStep
@abstract A step in a testing sequence.
Expand Down Expand Up @@ -317,6 +326,19 @@ typedef KIFTestStepResult (^KIFTestStepExecutionBlock)(KIFTestStep *step, NSErro
*/
+ (id)stepToTapViewWithAccessibilityLabel:(NSString *)label value:(NSString *)value traits:(UIAccessibilityTraits)traits;

/*!
@method stepForViewWithAccessibilityLabel:description:executionBlock:
@abstract A step to perform arbitrary logic on a view with a certain accessibility label.
@discussion The view or accessibility element with the given label is searched for in the view hierarchy. If the element isn't found, then the step will attempt to wait until it is. Once the view is present, the view is passed to the given execution block which should perform the logic of the step.

This method is here to allow arbitrary operations and verifications on views, which is useful if there are properties of the view to verify other than the accessibility properties, or if the app under test requires custom logic to simulate user interaction.
@param label The accessibility label of the element to tap.
@param description A description of the what the step does. Required.
@param executionBlock A block to execute which performs the step. Required.
@result A configured test step.
*/
+ (id)stepForViewWithAccessibilityLabel:(NSString *)label description:(NSString *)description executionBlock:(KIFTestStepExecutionBlockWithView)executionBlock;

/*!
@method stepToTapScreenAtPoint:
@abstract A step that taps the screen at a particular point.
Expand Down
22 changes: 22 additions & 0 deletions Classes/KIFTestStep.m
Expand Up @@ -322,6 +322,28 @@ + (id)stepToTapViewWithAccessibilityLabel:(NSString *)label value:(NSString *)va
}];
}

+ (id)stepForViewWithAccessibilityLabel:(NSString *)label description:(NSString *)description executionBlock:(KIFTestStepExecutionBlockWithView)executionBlock
{
// viewFinderBlock will first try to find the view with the given |accessibilityLabel| then will
// invoke the |executionBlock|.
id viewFinderBlock = ^(KIFTestStep *step, NSError **error) {
UIApplication *app = [UIApplication sharedApplication];
// Scroll to make sure the view is visible, otherwise we will get an accessibilityElement that maps to the first visible parent view of the view we want.
UIAccessibilityElement *element = [self _accessibilityElementWithLabel:label accessibilityValue:nil tappable:NO traits:UIAccessibilityTraitNone error:error];
KIFTestWaitCondition(element, error, @"Wait for view with accessibility label \"%@\"", label);
// Now that the view is scrolled onto the screen, get the element again.
element = [app accessibilityElementWithLabel:label accessibilityValue:nil traits:UIAccessibilityTraitNone];
UIView *view = [UIAccessibilityElement viewContainingAccessibilityElement:element];
if (!view) {
return KIFTestStepResultFailure;
}
// Execute the block.
return executionBlock(step, view, error);
};

return [KIFTestStep stepWithDescription:description executionBlock:viewFinderBlock];
}

+ (id)stepToTapScreenAtPoint:(CGPoint)screenPoint;
{
NSString *description = [NSString stringWithFormat:@"Tap screen at point \"%@\"", NSStringFromCGPoint(screenPoint)];
Expand Down