2

我正在使用 KIF 框架自动化我的应用程序。对于其中一种情况,我需要从 UIDatePicker 自动选择日期。

有没有人这样做过?并就如何继续或共享一些代码提出一些想法。

任何帮助是极大的赞赏。

谢谢

4

3 回答 3

4

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

+ (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;         
            }];
}
于 2013-03-25T19:38:17.667 回答
2

一个更最新的答案。

使用 KIF 中的此方法:

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

在 KIF 3.1.0 上可用

于 2015-01-07T18:06:14.783 回答
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];
于 2014-03-29T01:11:37.723 回答