Cucumberish
Cucumberish copied to clipboard
Screenshots
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.
@robwhitlock666 Will XCUIScreenshot work for you?
@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?
@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())