2012-06-18 49 views
8

我想测试我的应用程序处理方向更改(纵向/横向)的能力。我目前使用KIF,据我所知,它不能这样做。有没有一种方法来模拟iOS模拟器的编程旋转事件?模拟iOS中的方向更改以进行测试

我不在乎它是否是一些未公开的私有API或黑客,因为它只会在测试期间运行,并且不会成为生产版本的一部分。

回答

9

这里是实现这一步骤:

+ (KIFTestStep*) stepToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation { 

    NSString* orientation = UIInterfaceOrientationIsLandscape(toInterfaceOrientation) ? @"Landscape" : @"Portrait"; 
     return [KIFTestStep stepWithDescription: [NSString stringWithFormat: @"Rotate to orientation %@", orientation] 
          executionBlock: ^KIFTestStepResult(KIFTestStep *step, NSError *__autoreleasing *error) { 
           if([UIApplication sharedApplication].statusBarOrientation != toInterfaceOrientation) { 
            UIDevice* device = [UIDevice currentDevice]; 
            SEL message = NSSelectorFromString(@"setOrientation:"); 

            if([device respondsToSelector: message]) { 
             NSMethodSignature* signature = [UIDevice instanceMethodSignatureForSelector: message]; 
             NSInvocation* invocation = [NSInvocation invocationWithMethodSignature: signature]; 
             [invocation setTarget: device]; 
             [invocation setSelector: message]; 
             [invocation setArgument: &toInterfaceOrientation atIndex: 2]; 
             [invocation invoke]; 
            } 
           } 

           return KIFTestStepResultSuccess; 
          }]; 
} 

注意:保持你的设备平放在桌上或加速更新将旋转视图回来。

+0

这似乎不适用于模拟器。我没有设备,所以我没有对它进行测试,但KIF测试将通过VaxSim在模拟器上运行,因此它必须在模拟器上运行。你能确认这是否在模拟器上运行?我正在使用iOS 6.1模拟器。 – applefreak

+0

我的不好!它只在App支持有问题的方向时才有效! – applefreak

0

我不知道'程序化'是什么意思,但如果您使用Apple提供的UIAutomation库以及Instruments应用程序的自动化模板,则可以模拟iPhone支持的不同方向。

+0

没错,我也找到了。我仍然在寻找一种从Objective-C中的应用程序本身来完成它的方法。这就是KIF测试框架的运作方式。 –

-2

为什么要编程?模拟器完全按照你想要的来测试应用程序处理方向变化的能力。

在模拟器中,使用顶部菜单Hardware> Rotate Left/Right或按住Command并使用左右箭头。

+3

我想以编程的方式执行此操作,因为我想要自动化的UI测试运行时不需要我在生成服务器上的干预。 –

4

要模拟UI自动化中的方向更改,可以使用UIATarget的setDeviceOrientation方法。示例:

UIATarget.localTarget().setDeviceOrientation(UIA_DEVICE_ORIENTATION_LANDSCAPELEFT); 

方法需要一个参数'deviceOrientation'常量。 更多信息here

这100%适用于真正的iOS设备。我不确定模拟器。

+0

你的建议是必须从UI自动化工具运行的东西。这不适合我。我需要可以从应用程序中调用的Objective C代码。这就是KIF测试框架的工作原理。对不起,没有足够清楚。 –