This is the best way I found to do this so far: Automating App Store localized screenshots with XCTest and Xcode Test Plan
To sum it up (and try to make sure this answer doesn't suffer from link rot in the future), you should create a Test Plan that goes through the screens in your app you want to screenshot and include code similar to the following to take the screenshots:
class AppStoreScreenshotTests: XCTestCase {
var app : XCUIApplication!
override func setUpWithError() throws {
continueAfterFailure = false
self.app = XCUIApplication()
}
override func tearDownWithError() throws {
self.app = nil
}
func testSearchJourney() {
self.app.launch()
// moving to search tab
app.buttons[AccessibilityIdentifiers.searchTab.rawValue].tap()
// wait for screen to be fully displayed within 5sec
XCTAssertTrue(app.buttons[AccessibilityIdentifiers.searchButton.rawValue].waitForExistence(timeout: 5))
// take a screenshot of the search page
attachScreenshot(name: "search-form")
}
private func attachScreenshot(name: String) {
let screenshot = app.windows.firstMatch.screenshot()
let attachment = XCTAttachment(screenshot: screenshot)
attachment.name = name
attachment.lifetime = .keepAlways
add(attachment)
}
Then you can automate creation and extraction of screenshots with a shell script that executes the test with xcodebuild test
and use xcparse
to export them into a separate folder:
xcparse screenshots --os --model --test-plan-config /path/to/Test.xcresult /path/to/outputDirectory
Personally I prefer this approach rather than using Fastlane.