NSNerd
NSNerd

Reputation: 141

How to Automate Selecting Date from UIDatePicker using KIF Framework

I'm automating my app using KIF Framework. For one of the Scenarios I need to automate Date selection from UIDatePicker.

Has anyone done this before ? and throw some idea as to how to proceed or share some code.

Any help is greatly appreciated.

Thanks

Upvotes: 2

Views: 1619

Answers (3)

mcatach
mcatach

Reputation: 1596

A more more up-to-date answer.

Use this method from KIF:

- (void) selectDatePickerValue:(NSArray*)datePickerColumnValues;

Available on KIF 3.1.0

Upvotes: 2

iwasrobbed
iwasrobbed

Reputation: 46703

Here's a more up-to-date (and very basic) method you can put in a category on KIFUITestActor

- (void)enterDate:(NSDate *)date intoDatePickerWithAccessibilityLabel:(NSString *)label {
  [tester runBlock:^KIFTestStepResult(NSError **error) {
    UIAccessibilityElement *element;
    [self waitForAccessibilityElement:&element view:nil withLabel:label value:nil traits:UIAccessibilityTraitNone tappable:NO];
    KIFTestCondition(element, error, @"Date picker with label %@ not found", label);
    KIFTestCondition([element isKindOfClass:[UIDatePicker class]], error, @"Specified view is not a picker");
    UIDatePicker *picker = (UIDatePicker *)element;
    [picker setDate:date animated:NO];
    [self waitForTimeInterval:1.f];
    return KIFTestStepResultSuccess;
  }];
}

Then you could use it like this:

[tester enterDate:[NSDate distantPast] intoDatePickerWithAccessibilityLabel:kLocaleDatePicker];

Upvotes: 0

memmons
memmons

Reputation: 40502

Use setAccessibilityLabel on the date picker and then define a method using setDate in KIFTestStep category like this:

+ (id)stepToEnterDate:(NSDate*)date ToDatePickerWithAccessibilityLabel:(NSString*)label
{
    NSString *description=[NSString stringWithFormat:@"Enter date to Date picker with accessibility label '%@'",[date description]];
    return [self stepWithDescription:description executionBlock:^(KIFTestStep *step, NSError **error)
            {
                UIAccessibilityElement *element = [[UIApplication sharedApplication] accessibilityElementWithLabel:label];
                KIFTestCondition(element, error, @"View with label %@ not found", label);
                if(!element)
                {
                    return KIFTestStepResultWait;
                }
                UIDatePicker *picker = (UIDatePicker*)[UIAccessibilityElement viewContainingAccessibilityElement:element];

                KIFTestCondition([picker isKindOfClass:[UIDatePicker class]], error, @"Specified view is not a picker");

                [picker setDate:date animated:YES];
                return KIFTestStepResultSuccess;         
            }];
}

Upvotes: 4

Related Questions