Take screenshots with XCTest methods

XCTest test methods may create screenshots at key points during the test run. The Gradle plugin retrieves these screenshots and includes them in the execution report. The plugin supports screenshots generated by either:

  • KIF system KIFSystemTestActor class
  • EarlGrey GREYScreenshotUtil class

To learn how to take screenshots using these classes, read the following sections.

KIF screenshots

To take screenshots using the KIFSystemTestActor  captureScreenshotWithDescription() method:

  1. Use the instrumentationArgs configuration parameter to indicate the use of KIF for screenshots.

    "instrumentationArgs" : ["KIF"]

    This will preset the KIF_SCREENSHOTS environment variable for the test run.

  2. Supply a description parameter that includes the test's className and testMethod in the following format:
    "testClass#testMethod" (the full description may include a prefix or suffix to this string). This format allows associating the screenshot with the correct test method.

EarlGrey screenshots

To take screenshots using the EarlGrey GREYScreenshotUtil class:

  1. Use the instrumentationArgs configuration parameter to indicate the use of EarlGrey for screenshots.

    Copy
    "instrumentationArgs" : ["EARLGREY"]
  2. The screenshot should be in a "png" file and the name should include a string that includes the test's className and testMethod in the following format:

    "testClass#testMethod.png" (the full description may include a prefix or suffix to this string). This format allows associating the screenshot with the correct test method.
    The following XCode snippet provides an example of creating the screenshot:

    Copy
    let confpath = GREYConfiguration.sharedInstance().value(forConfigKey: kGREYConfigKeyArtifactsDirLocation) as! String
    var screenshot = GREYScreenshotUtil.takeScreenshotForAppStore()
    var filename = String(describing: EarlGreySimpleIosAppForTestingTests.self) + "#" + #function + ".png"
    GREYScreenshotUtil.saveImage(asPNG: screenshot, toFile: filename, inDirectory: confpath)
    EarlGrey.select(elementWithMatcher: grey_text("EarlGrey Tests")).assert(grey_sufficientlyVisible())