Search code examples
iosswiftxcodexctestxcode-ui-testing

How to rotate simulator using Swift code in xcode ui test


I have a scenario where I have to rotate my simulator while xcode ui test is going.

To rotate the simulator, I use the below code

UIDevice.currentDevice().orientation
let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")

but it is not working.

Is there any solution to rotate the simulator using Swift code in xcode ui test?


Solution

  • Have you tried this?

    Swift 2.x

    XCUIDevice.sharedDevice().orientation = .LandscapeLeft  
    XCUIDevice.sharedDevice().orientation = .Portrait  
    

    Swift 3.0

    XCUIDevice.shared().orientation = .landscapeLeft
    XCUIDevice.shared().orientation = .portrait
    

    Swift 5.x

    XCUIDevice.shared.orientation = .landscapeLeft
    XCUIDevice.shared.orientation = .portrait