Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Screenshots #131

Open
robwhitlock666 opened this issue Jul 3, 2020 · 3 comments
Open

Screenshots #131

robwhitlock666 opened this issue Jul 3, 2020 · 3 comments

Comments

@robwhitlock666
Copy link

Do screenshots for failed tests work out of the box? Since we don't really have access to the XCTestCase being generated, we can't do our own screenshot management. Any help appreciated.

@rookiezn
Copy link

@robwhitlock666 Will XCUIScreenshot work for you?

@Mikuu
Copy link

Mikuu commented Aug 14, 2020

@rookiezn I tried XCUIScreenshot in screen definition files, like below

func addScreenshot() {

        let fullScreenshot = XCUIScreen.main.screenshot()
        let screenshot = XCTAttachment(screenshot: fullScreenshot)
        screenshot.lifetime = .keepAlways
        // if we don't set lifetime to .keepAlways, Xcode will delete the image if the test passes.
        
        add(screenshot)
    }

but couldn't find the screenshot/attachment in the xcode test report, no idea why?

@philongho
Copy link

philongho commented Jul 31, 2023

@rookiezn I found this inside the code base

 @param userInfo is a dictionary that currently can have one of two keys kDataTableKey or kDocStringKey. If your step definition is expected to match a data table or a doc string, then you can expect this user info dictionary to contain kDataTableKey or kDocStringKey key respectively. Moreover, it will contain the key kXCTestCaseKey which will hold reference to the XCTestCase that currently executing this step. The value of kXCTestCaseKey will be nil if the step is being executed by a call of "step" or "SStep" C functions and you did not pass a reference to the XCTestCase.

I am able to attach screenshot with this

    When("your step") { args, userInfo in
        let xcTestCaseReference: XCTestCase = userInfo!["XCTestCase"] as! XCTestCase

        let screenshot = app.screenshot() //your XCUIApplication reference
        let attachment = XCTAttachment(screenshot: screenshot)
        attachment.lifetime = .keepAlways
        xcTestCaseReference.add(attachment)
    }

Remember to disable delete attachment on success in Test Scheme -> Options (Xcode 14)

For attach screenshot as embedded data to JSON report, you can add this line
CCIEmbed("image/png", screenshot.pngRepresentation.base64EncodedString())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants