I have a UI test for my app which should take a screenshot of all the relevant screens.
It works nicely for iPhone in portrait but on iPad, where I rotate the device to landscape (using XCUIDevice.shared.orientation = .landscapeLeft), the screenshots are only partially visible. The screenshot is taken in portrait even though the device is currently running in landscape, resulting in the lower section being black and the right part being cut off.
The interesting thing I've noticed: all automatic screenshots taken by Xcode while running individual test steps are created correctly but the one I create myself using the snippet below is cut-off.
Am I doing something wrong?
in setUp:
And later while running a test:
It works nicely for iPhone in portrait but on iPad, where I rotate the device to landscape (using XCUIDevice.shared.orientation = .landscapeLeft), the screenshots are only partially visible. The screenshot is taken in portrait even though the device is currently running in landscape, resulting in the lower section being black and the right part being cut off.
The interesting thing I've noticed: all automatic screenshots taken by Xcode while running individual test steps are created correctly but the one I create myself using the snippet below is cut-off.
Am I doing something wrong?
Code Block func takeScreenShot(_ element: XCUIElement, name: String = "Screenshot") { let screenshot = element.screenshot() let attachment = XCTAttachment(screenshot: screenshot) attachment.name = name attachment.lifetime = .keepAlways add(attachment) }
in setUp:
Code Block override func setUp() { let app = XCUIApplication() app.launch() if UIDevice.current.userInterfaceIdiom == .pad { XCUIDevice.shared.orientation = .landscapeLeft } // ... more unrelated stuff }
And later while running a test:
Code Block XCTContext.runActivity(named: "Image of the day is shown") { (_) in let dismissButton = app.buttons["dismissImageOfTheDay"].firstMatch XCTAssert(dismissButton.waitForExistence(timeout: 4.0), "The dismiss button exists") takeScreenShot(app, name: "00_ImageOfTheDay") dismissButton.tap() }
Hmm... even though I had the problem for a long time, I have now found the solution myself:
do not take a screenshot from the app/XCUIApplication but from screen/XCUIScreen.main
So I changed takeScreenshot to be
do not take a screenshot from the app/XCUIApplication but from screen/XCUIScreen.main
So I changed takeScreenshot to be
Code Block func takeScreenshot(name: String = "Screenshot") { let screenshot = XCUIScreen.main.screenshot() let attachment = XCTAttachment(screenshot: screenshot) attachment.name = name attachment.lifetime = .keepAlways add(attachment) }