Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode UI Testing: Automatically taking snapshot when predicates fail?

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?

like image 745
Apophenia Overload Avatar asked Mar 29 '17 00:03

Apophenia Overload


People also ask

What is XCTest XCUITest?

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.

How to record UI test in 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!

How to UITest swift?

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.


1 Answers

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.

like image 152
Daniel Carlos Avatar answered Sep 27 '22 20:09

Daniel Carlos