Logo for tanaschita.com

How to automate taking screenshots with fastlane for iOS

Learn how to setup fastlane's snapshot action.

24 Jul 2023 · 2 min read

As mentioned in this fastlane guide for iOS, fastlane provides a snapshot action to automatically generate screenshots for an iOS application.

Automatically taking screenshots of a mobile application can serve us developers in different ways, for example as part of publishing the app to the App Store, as part of a QA process or as part of UI testing to identify changes to previous versions.

Let's look at how we can use fastlane's snapshot action to do that.

Sponsorship logo
Preparing for a technical iOS job interview
Check out my new book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, Authentication, Core Data, Concurrency with async/await, Security, Automated Testing and more.
LEARN MORE

The snapshot action uses the capabilities of UI tests to create the desired application state. If you never worked with UI tests before, check out this article on getting started with UI tests to learn more.

To setup snapshot, the following steps are required:

  1. Creating a new UI test target
  2. Running fastlane snapshot init in the project folder which will create a Snapfile and ./SnapshotHelper.swift
  3. Open the created Snapfile file to configure languages, devices and more, for example:
devices([
"iPhone 14"
])
output_directory("./screenshots")
clear_previous_screenshots(true)
override_status_bar(true)
  1. Add the created ./SnapshotHelper.swift file to the UI Test target
  2. Add a new Xcode scheme for the created UI Test target, open its Build section and enable the Run and Shared checkbox.
  3. Add the following code in the setUp() method:
let app = XCUIApplication()
setupSnapshot(app)
app.launch()
  1. Add interactions to the UI test class that lead to desired screens and call snapshot("NameOfTheScreen") to take a screenshot where needed.
  2. Run fastlane snapshot in the terminal. It will create the screenshots inside the fastlane/screenshots directory.

To add device frames around created screenshots, we can use the fastlane frameit action. We also have the possibility to add a background and titles by setting up a Framefile.json.

Sponsorship logo
Preparing for a technical iOS job interview
Check out my new book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, Authentication, Core Data, Concurrency with async/await, Security, Automated Testing and more.
LEARN MORE

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

continuous integration

ios

Continuous integration and delivery for iOS

Learn CI/CD concepts and possible workflows for iOS applications.

29 May 2023 · 5 min read

Latest articles and tips

© 2023 tanaschita.com

Privacy policy

Impressum