Xcode user interface testing: automatically take snapshots when predicate failures?

Xcode UI Testing takes automatic screenshots to view the results in the navigator whenever the test fails, which is very useful. However, this does not include tests that fail because the predicate failed. Since predicates are often used for basic checks (for example, if an element exists or not in the current view), this is a huge drawback, because a screenshot would be useful in diagnosing what happens in the application when the test fails.

Does anyone know how to get a screenshot? Is integration with Fastlane Snapshot tools required?

+4
source share
4 answers

As tearDownyou can check, whether the test is not passed (this is useful if you do not drop the screenshots at the test.)

if let failureCount = testRun?.failureCount, failureCount > 0 {
  takeScreenshot()
}

If you are already using XCode9, the function takeScreenshotcan use the new API (if not, then use the workaround mentioned by another answer):

let screenshot = XCUIScreen.main.screenshot()
let attach = XCTAttachment(screenshot: screenshot)
add(attach)

You can also name the application and change its lifetime;)

+7
source

You do not need to integrate the Fastlane snapshot for this. Snapshot does the only trick to make the screenshot run this code:

XCUIDevice.shared().orientation = .unknown

This will not change the interface as described in the Snaphot documentation.

, , , waitForExpectations(timeout:handler:), .

, XCTestObservation :

class MockObserver: NSObject, XCTestObservation {
    func testCase(_ testCase: XCTestCase, didFailWithDescription description: String, inFile filePath: String?, atLine lineNumber: UInt) {
        XCUIDevice.shared().orientation = .unknown
    }
}

XCTestObservationCenter.shared().addTestObserver(MockObserver())

setUp() test....

, " Unknown" , :

Test Log

+6

recordFailure, .

override func recordFailure(withDescription description: String, inFile filePath: String, atLine lineNumber: Int, expected: Bool) {
        add(XCTAttachment(screenshot: XCUIScreen.main.screenshot()))
        super.recordFailure(withDescription: description, inFile: filePath, atLine: lineNumber, expected: expected)
}
0

How can we show this screen shot in a Jenkins task when the test fails?

0
source

Source: https://habr.com/ru/post/1673453/


All Articles