Xcode UI Testing takes automatic screenshots for viewing in the results navigator whenever a test fails, which is greatly helpful. However, that does not include tests that fail because a predicate is failed. Since predicates are often for basic checks (such as if an element exists or not on a current view), that is a huge drawback because a screenshot would be useful in diagnosing what was happening in the app when the test failed.
Does anyone know how to force a screenshot? Does this require integrating the Fastlane Snapshot tool?
Key Concepts of XCUITest XCTest: XCTest is a testing framework that allows you to create and run UI tests, unit tests, and performance tests for your Xcode projects in Swift and Objective-C languages. It is pre-built with Xcode.
Just press record UI testing in Xcode is easy. Click inside the body of an empty UI test, click the Record button, and you're up and running. Let's start off by recording a UI test to add a new to-do. That was easy!
When you're ready to test, go to a test class and place the cursor inside the test method to record the interaction. From the debug bar, click the Record UI Test button. Xcode will launch the app and run it. You can interact with the element on-screen and perform a sequence of interactions for any UI test.
On tearDown
you can check if test failed (that's helpful if you are not discarding screenshots when tests pass.)
if let failureCount = testRun?.failureCount, failureCount > 0 {
takeScreenshot()
}
If you are using Xcode 9 already, the takeScreenshot
function can use the new API (If not, then use the workaround mentioned by the other answer) :
let screenshot = XCUIScreen.main.screenshot()
let attach = XCTAttachment(screenshot: screenshot)
add(attach)
You can also name the attach and change its lifetime ;)
See Apple's documentation for how to use and where to find them (the "Report navigator" in View > Navigators > Reports
) in more detail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With