如何更改XCTest for iOS上的测试方向?

如何更改XCTest for iOS上的测试方向?,ios,objective-c,xctest,xctestcase,Ios,Objective C,Xctest,Xctestcase,我正试图在不同的方向上使用XCTestCase测试我的iOS应用程序。我需要一种改变方向的方法。我尝试了两种方法,但都没有改变方向(方向仍然是UIInterfaceOrientationGrait) 尝试1 [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft;` 尝试2 [[UIDevice currentDevice] setValue:[NSNumber n

我正试图在不同的方向上使用XCTestCase测试我的iOS应用程序。我需要一种改变方向的方法。我尝试了两种方法,但都没有改变方向(方向仍然是UIInterfaceOrientationGrait)

尝试1

[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft;`
尝试2

[[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft] forKey:@"orientation"];`
当我阅读[[UIApplication sharedApplication]statusBarOrientation]时
是否有其他方法更改方向以进行测试?

使用下面的解决方案。在设置方法中执行此操作

[[UIDevice currentDevice] setValue:
                      [NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
                            forKey:@"orientation"];

这对我有用。

这应该有用,我正在随机改变方向,因为我正在运行几个测试,我希望这是有用的

        // Set a random device orientation
    let orient = [UIDeviceOrientation.portrait , UIDeviceOrientation.portraitUpsideDown, UIDeviceOrientation.landscapeLeft, UIDeviceOrientation.landscapeRight]
    XCUIDevice.shared().orientation = orient[Int(arc4random_uniform(UInt32(orient.count)))]
Swift代码:

XCUIDevice.shared.orientation = UIDeviceOrientation.portrait;

如果要在
XCTest
环境中运行设备旋转测试,我目前最著名的策略是:

  • 在模拟器中,确保设置了“自动旋转设备”选项
  • 创建XTest主机应用程序,并确保其
    info.plist
    支持的界面方向
    部分已完全删除(您不能在生产应用程序中执行此操作,因为appStore将拒绝它。)
  • 在主机应用程序中,委托实现
    -application:supportedInterfaceOrientionsforWindow:
    并返回
    UIInterfaceOrientationMaskAll
    。这将有效地替换以前的
    info.plist
    条目
  • 然后在测试中,调用私有
    [UIDevice.currentDevice setValue:@(UIDeviceOrientation…)forKey:@“orientation”]在需要时
  • 这将设置动画,因此请等待所有窗口通过检查其帧来更改其方向,然后继续测试
  • 要加快测试中的动画速度,
    设置window.layer.speed=100
    。这将使动画速度提高100倍,希望您的测试也能如此

这在测试函数中对我很有效-我不需要将它添加到
setUp()
。错误:无法分配到属性:“方向”是一个仅获取的属性错误:无法调用非函数类型“XCUIDevice”的值为什么是只读的?以下是文档
方向
UIDevice
上是只读的,但在
xguidevice
上不是。这很好用。与“等待方向更改确认超时”一起崩溃。这个答案对我帮助很大,谢谢!不知道为什么会被否决。这是唯一正确回答这个问题的答案,它是针对单元测试,而不是UI测试。还有一个提示。。。如果要在CI上执行此操作,并且需要始终设置“自动旋转设备”,则可以使用
默认值write com.apple.iphonesimulator rotatewhensignaledbyguest true