Class

XCUIScreenshot

A captured image of a screen, app, or UI element state.

Overview

Screenshots capture the current UI state of classes that conform to the XCUIScreenshotProviding protocol, such as XCUIScreen and XCUIElement. Each screenshot contains an image representation of the captured UI at the point the screenshot was taken.

Listing 1

Taking screenshots of a screen and a UI element.

func testTakeScreenshots() {

    // Take a screenshot of the current device's main screen.
    let mainScreenScreenshot = XCUIScreen.main.screenshot()
    
    // Take a screenshot of an app's first window.
    let app = XCUIApplication()
    app.launch()
    let windowScreenshot = app.windows.firstMatch.screenshot()

}

You can attach a screenshot of your app's UI to a test or activity to store it for later analysis. Create an attachment for a screenshot by calling XCTAttachment's init(screenshot:) or init(screenshot:quality:) initializer, then add the attachment to a test or activity via XCTActivity's add(_:) method. For more information, see Adding Attachments to Tests and Activities.

Topics

Screenshot Representations

var image: UIImage

A representation of the screenshot as a platform-native image object.

var pngRepresentation: Data

A representation of the screenshot as PNG image data.

Relationships

Inherits From

Conforms To

See Also

Screenshots

class XCUIScreen

A physical screen attached to a device.

protocol XCUIScreenshotProviding

A type that can provide a screenshot of its current UI state.