In Xcode UI Testing is it possible to get the orientation of the view
Asked Answered
S

3

6

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?

Supernumerary answered 9/3, 2016 at 18:24 Comment(2)
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
R
2

You can get the orientation using this line

XCUIDevice.sharedDevice().orientation
Reiners answered 10/3, 2016 at 17:42 Comment(5)
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 changedReiners
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 DeviceRyun
R
1

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))
}
Ryun answered 25/5, 2016 at 13:40 Comment(1)
This doesn't work for me. I still get .LandscapeLeftMonica
C
1

sharedDevice has been renamed. Now you can get and set the value like this.

XCUIDevice.shared.orientation = .portrait
Cruise answered 19/11, 2021 at 11:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.