ios - 出于测试目的模拟 iOS 中的方向变化

标签 ios testing ui-automation ios-ui-automation kif-framework

我想测试我的应用处理方向变化(纵向/横向)的能力。我目前正在使用 KIF据我所知,它不能这样做。有没有办法以编程方式为 iOS 模拟器模拟旋转事件?

我不关心它是否是一些未记录的私有(private) API 或 hack,因为它只会在测试期间运行,不会成为生产构建的一部分。

最佳答案

这是实现这一目标的一个步骤:

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

注意:请将您的设备平放在 table 上,否则加速度计更新会将 View 旋转回来。

关于ios - 出于测试目的模拟 iOS 中的方向变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11080077/

相关文章:

ios - UIActionSheet - 快照尚未呈现的 View 会导致空快照

ios - 以编程方式处理 iOS 设备旋转

azure - 如何在 azure 上管理夏令时

c++ - 在 Visual Studio 中通过包管理器控制台安装 TestStack.White

ios - Xcode ScrollView 不滚动

ios - UITableView 数据显示在模拟器中,但不显示在实际设备上

ruby-on-rails - 如何测试我是否已在 Rails 中正确设置多对多关系?

unit-testing - IO返回值测试

vb.net - 将事件添加到项目中的所有表单

python - Pywinauto Windows 存在但不可见