2012-06-18 32 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測試框架的工作原理。對不起,沒有足夠清楚。 –

相關問題