Class

XCUIScreenshot

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

Declaration

@interface XCUIScreenshot : NSObject

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 attachmentWithScreenshot: or attachmentWithScreenshot:quality: initializer, then add the attachment to a test or activity via XCTActivity's addAttachment: method. For more information, see Adding Attachments to Tests and Activities.

Topics

Screenshot Representations

image

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

PNGRepresentation

A representation of the screenshot as PNG image data.

Relationships

Inherits From

See Also

Screenshots

XCUIScreen

A physical screen attached to a device.

XCUIScreenshotProviding

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