Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you change orientation for testing on XCTest for iOS?

I'm trying to test my iOS app using XCTestCase in different orientations. 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).

Attempt 1

[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft;`

Attempt 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?

like image 872
Meyyappan Nachiappan Avatar asked Feb 25 '16 17:02

Meyyappan Nachiappan


People also ask

What is XCTest framework in iOS?

Use the XCTest framework to write unit tests for your Xcode projects that integrate seamlessly with Xcode's testing workflow. Tests assert that certain conditions are satisfied during code execution, and record test failures (with optional messages) if those conditions aren't satisfied.

How do you do iOS UI testing?

Recording a UI TestFrom the debug bar, click the Record UI Test button. Xcode will launch the app and run it. You can interact with the element on-screen and perform a sequence of interactions for any UI test. Whenever you interact with an element, Xcode writes the corresponding code for it into your method.

Is XCTest a unit testing framework?

XCTest framework is one of those frameworks that enables its users to write basic unit, performance and some level of UI tests for iOS apps.

How do I run XCTest in Xcode?

To run your app's XCTests on Test Lab devices, build it for testing on a Generic iOS Device: From the device dropdown at the top of your Xcode workspace window, select Generic iOS Device. In the macOS menu bar, select Product > Build For > Testing.


1 Answers

Swift code:

XCUIDevice.shared.orientation = UIDeviceOrientation.portrait;
like image 70
Dmytro Avatar answered Nov 15 '22 22:11

Dmytro