[英]How can you change orientation for testing on XCTest for iOS?
I'm trying to test my iOS app using XCTestCase in different orientations. 我正在尝试使用XCTestCase在不同方向上测试我的iOS应用。 I need a way to programmatic way to change the orientation. 我需要一种以编程方式更改方向的方法。 I tried doing this in 2 methods, but both didn't change the orientation (orientation remained as UIInterfaceOrientationPortrait). 我尝试使用2种方法进行此操作,但两种方法均未更改方向(方向仍为UIInterfaceOrientationPortrait)。
Attempt 1 尝试1
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft;`
Attempt 2 尝试2
[[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft] forKey:@"orientation"];`
When I read [[UIApplication sharedApplication] statusBarOrientation] Is there another way to change the orientation for testing purposes? 当我阅读[[UIApplication sharedApplication] statusBarOrientation]时,是否还有另一种方法可以更改方向以进行测试?
SWIFT代码:
XCUIDevice.shared.orientation = UIDeviceOrientation.portrait;
use below solution. 使用以下解决方案。 Do it in setup method. 用设置方法来做。
[[UIDevice currentDevice] setValue:
[NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
forKey:@"orientation"];
its working for me. 它为我工作。
This should work, I am changing the orientation randomly because I am running several tests, I hope this is useful 这应该可行,我正在随机更改方向,因为我正在运行多个测试,希望这会有所帮助
// 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)))]
The solution I found ist the following. 我找到的解决方案如下。 Hence the rotation is being updated in the hack as mentioned before and additionally the window is forced to re-layout according to the new orientation. 因此,如前所述,在黑客中更新旋转,并且另外根据新方向强制窗口重新布局。 Still the simulator stays in portrait mode but the layout within is landscape. 模拟器仍然处于纵向模式,但其中的布局是横向的。 That's enough for a unit test on a Bot and if a test fails, you can debug locally with the device correctly rotated by hand. 这对于Bot上的单元测试来说已经足够了,如果测试失败,您可以手动正确旋转设备进行本地调试。
- (void)forceDeviceOrientationTo:(UIInterfaceOrientation)orientation
{
[UIDevice.currentDevice setValue:@(orientation) forKey:@"orientation"];
SEL selector = NSSelectorFromString(@"_updateToInterfaceOrientation:duration:force:");
NSMethodSignature* methodSignature = [self.projectViewController.view.window methodSignatureForSelector:selector];
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
invocation.target = self.projectViewController.view.window;
invocation.selector = selector;
[invocation setArgument:&orientation atIndex:2];
double duration = 0.01;
[invocation setArgument:&duration atIndex:3];
BOOL force;
[invocation setArgument:&force atIndex:4];
[invocation invoke];
// give enough time to let the rotation happen
[NSRunLoop.currentRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:duration+0.2]];
}
If you want to run device rotating tests within the XCTest
environment, my currently best known strategy is: 如果要在XCTest
环境中运行设备旋转测试,我目前最知名的策略是:
info.plist
Supported interface orientations
section is fully removed (you can't do that in your production app as the appStore will reject it.) 创建一个XCTest Host应用程序,并确保其info.plist
Supported interface orientations
部分已完全删除(您不能在生产应用程序中这样做,因为appStore会拒绝它。) -application:supportedInterfaceOrientationsForWindow:
and return UIInterfaceOrientationMaskAll
. 在主机应用程序委托中,实现-application:supportedInterfaceOrientationsForWindow:
并返回UIInterfaceOrientationMaskAll
。 This will effectively replace the former info.plist
entries. 这将有效替换以前的info.plist
条目。 [UIDevice.currentDevice setValue:@(UIDeviceOrientation…) forKey:@"orientation"];
然后在测试中,调用私有的[UIDevice.currentDevice setValue:@(UIDeviceOrientation…) forKey:@"orientation"];
at need 有需要 set window.layer.speed = 100
. 要加快测试中的动画速度,请set window.layer.speed = 100
。 This makes the animations 100 times faster and hopefully your tests too. 这使动画速度提高了100倍,希望您的测试也能做到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.