Add automatic screenshot creation to your iOS project

Project code:

Level:

Easy

Uploading screenshots to the app store is an important part of publishing an iOS app. Many times an update may not be approved if the screenshots do not match the application being submitted for review. So it is important that every time an app update is uploaded, the screenshots are the same as the current version.

If your app supports 10 languages, with 3 iPhone screen types, that’s 30 different screenshots… If this part can be automated, you will have less work to publish a new version of the app.

To automate the creation of screenshots we will use the Xcode UI tests and the fastlane tool.

Create UI Testing Bundle

When creating the new target, xcode creates a directory with some sample UI tests. We are going to create the necessary tests to be able to move through all the screens of the application.

func test_All_Scenes_Screenshots() throws {
    let app = XCUIApplication()
    app.launch()

    if app.buttons["btn_first_next"].exists {
        // Next scene
        app.buttons["btn_first_next"].tap()
            
        if app.buttons["btn_second_next"].exists {
            // Next scene
            app.buttons["btn_second_next"].tap()
                
        }
    }
}

When executing the test we can see how the app moves through all the screens.

Fastlane setup

You can install fastlane by following the instructions on its website.

Once installed, it can be added to the project by running the following command:

fastlane init

This command asks if you want to use fastlane to automate the generation of screenshots, select this option and the main scheme of your application.

Once the configuration is finished you have a new folder called “fastlane” with the necessary files to be able to use the fastlane command.

In order to define the devices and languages in which the screenshots will be generated, we are going to edit the fastlane/Snapfile file to add the iPhone models that we must update screenshots in the app store and the languages of the app:

# A list of devices you want to take the screenshots from
devices([
    "iPhone 8 Plus",
    "iPhone 13 Pro Max",
    "iPhone 14 Pro Max",
])

languages([
    "en-US",
])

Take snapshot

Now we have to define when we want a screenshot to be taken, we are going to edit the UI test and call the snapshot method:

func test_All_Scenes_Screenshots() throws {
    let app = XCUIApplication()
    app.launch()

    if app.buttons["btn_first_next"].exists {
        snapshot("01_first_scene")
        // Next scene
        app.buttons["btn_first_next"].tap()
            
        if app.buttons["btn_second_next"].exists {
            snapshot("02_second_scene")
            // Next scene
            app.buttons["btn_second_next"].tap()
            snapshot("03_last_scene")
        }
    }
}

Run the test

When we have everything ready, we can run the test to generate the screenshots with the following command: fastlane snapshot

When the command finishes, the browser automatically opens with the result.

If you want you can check the project code in the following github repository.

Leave a Reply

Your email address will not be published. Required fields are marked *