7

我正在尝试在不同方向使用 XCTestCase 测试我的 iOS 应用程序。我需要一种以编程方式改变方向的方法。我尝试在 2 种方法中执行此操作,但都没有改变方向(方向保持为 UIInterfaceOrientationPortrait)。

尝试 1

[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft;`

尝试 2

[[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft] forKey:@"orientation"];`

当我阅读 [[UIApplication sharedApplication] statusBarOrientation] 是否有另一种方法可以更改方向以进行测试?

4

4 回答 4

9

SWIFT代码:

XCUIDevice.shared.orientation = UIDeviceOrientation.portrait;
于 2018-12-30T09:37:24.923 回答
6

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

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

它为我工作。

于 2016-02-25T18:16:05.757 回答
3

这应该可以,我正在随机更改方向,因为我正在运行几个测试,我希望这很有用

        // 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)))]
于 2017-07-19T14:01:38.440 回答
1

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

  • 在模拟器中,确保设置了自动旋转设备选项。
  • 创建一个 XCTest Host 应用程序并确保它的 info.plist Supported interface orientations部分被完全删除(你不能在你的生产应用程序中这样做,因为 appStore 会拒绝它。)
  • 在宿主应用程序委托中实现-application:supportedInterfaceOrientationsForWindow:并返回UIInterfaceOrientationMaskAll。这将有效地取代以前的info.plist条目。
  • [UIDevice.currentDevice setValue:@(UIDeviceOrientation…) forKey:@"orientation"];然后在您的测试中,在需要时致电私人
  • 这会产生动画,因此请等到所有窗口通过检查它们的框架来改变它们的方向,然后继续您的测试。
  • 为了加快测试中的动画,set window.layer.speed = 100. 这使动画速度提高了 100 倍,希望您的测试也是如此。
于 2019-11-22T21:37:17.517 回答