For my app I am trying to run a basic UI test on whether the view stays fixed in its orientation when the actual device is rotated. I know how to simulate the physical rotation (using UIDeviceOrientation), but is there a way to get the orientation of the actual view?
In Xcode UI Testing is it possible to get the orientation of the view
Asked Answered
Since views don't really have a concept of orientation, it's not at all clear what you're trying to ask. –
Johnsen
I guess that the question could be rephrased. Can you actually test that your App DOES NOT rotate, when the device orientation is changed to .Landscape? If YES, how? BTW I'm looking into it myself. –
Ryun
You can get the orientation using this line
XCUIDevice.sharedDevice().orientation
I think the question is can you get the orientation of the view, this gets the simulated orientation of the device. Ie., what the user is looking to do is make sure that the view orientation (a meaningless concept, but...) stays the same regardless of the device orientation. –
Johnsen
He said that he can simulate the rotation, so he has to test the current orientation, simulate the rotation and test if the orientation changed –
Reiners
Sorry, I'm new to iOS dev. You are correct David, that is what I am asking. Do you think that UI testing allows for this kind of test? –
Supernumerary
this test works, but it you cant see the rotation happening
XCUIDevice.sharedDevice().orientation = .LandscapeLeft XCTAssertTrue(XCUIDevice.sharedDevice().orientation == .LandscapeLeft) XCUIDevice.sharedDevice().orientation = .Portrait XCTAssertTrue(XCUIDevice.sharedDevice().orientation == .Portrait)
–
Reiners The answer is irrelevant and misleading. The question is for the App, View and not the Device –
Ryun
I found a workaround. It's not great but it's doing the job. I'll try to find a more elegant way if there is one.
What you need to do is to find an Interface and check it's orientation. In my case I want to test that the UI remains in .Portrait orientation, when my Device is turned to .Landscape.
func testSPSCLaunchScreenLandscape() {
// Use recording to get started writing UI tests.
// Use XCTAssert and related functions to verify your tests produce the correct results.
XCUIDevice.sharedDevice().orientation = .LandscapeLeft
XCTAssert(UIInterfaceOrientationIsPortrait(UIApplication.sharedApplication().statusBarOrientation))
}
This doesn't work for me. I still get .LandscapeLeft –
Monica
sharedDevice
has been renamed. Now you can get and set the value like this.
XCUIDevice.shared.orientation = .portrait
© 2022 - 2024 — McMap. All rights reserved.