如何使用KIF框架自动从UIDatePicker中选择日期

时间:2013-03-25 19:18:55

标签: ios uidatepicker ios-ui-automation kif

我正在使用KIF Framework自动化我的应用。对于其中一个场景,我需要从UIDatePicker自动选择日期。

以前有人这样做过吗?并提出如何继续或分享一些代码的想法。

非常感谢任何帮助。

由于

3 个答案:

答案 0 :(得分:4)

在日期选择器上使用setAccessibilityLabel,然后在setDate类别中使用KIFTestStep定义方法,如下所示:

+ (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;         
            }];
}

答案 1 :(得分:2)

更新的答案。

从KIF使用此方法:

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

适用于KIF 3.1.0

答案 2 :(得分:0)

以下是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;
  }];
}

然后你可以像这样使用它:

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