如何更改XCTest for iOS上的测试方向?
我正试图在不同的方向上使用XCTestCase测试我的iOS应用程序。我需要一种改变方向的方法。我尝试了两种方法,但都没有改变方向(方向仍然是UIInterfaceOrientationGrait) 尝试1如何更改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
[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”]代码>在需要时
- 这将设置动画,因此请等待所有窗口通过检查其帧来更改其方向,然后继续测试
- 要加快测试中的动画速度,
。这将使动画速度提高100倍,希望您的测试也能如此设置window.layer.speed=100
setUp()
。错误:无法分配到属性:“方向”是一个仅获取的属性错误:无法调用非函数类型“XCUIDevice”的值为什么是只读的?以下是文档方向在UIDevice
上是只读的,但在xguidevice
上不是。这很好用。与“等待方向更改确认超时”一起崩溃。这个答案对我帮助很大,谢谢!不知道为什么会被否决。这是唯一正确回答这个问题的答案,它是针对单元测试,而不是UI测试。还有一个提示。。。如果要在CI上执行此操作,并且需要始终设置“自动旋转设备”,则可以使用默认值write com.apple.iphonesimulator rotatewhensignaledbyguest true
。