Fastlane: Capture screenshots automatically and Upload beta build to TestFlight

Jasmine Elamblakatt
5 min readJan 22, 2020
IMAGE SOURCE: GETTY IMAGES.

Why we should do a manual process when Fastlane can easily automate things for you.

Now a days, we are trying to automate everything from Domotics(Home Automation) to self-driving Cars. Then why being a developer we are manually taking screenshots and making build, let’s automate that and save our time.

Fastlane automates the build and release process and makes developer life a little less messy.
Here I will quickly automate the Screenshot capturing part and TestFlight deployment process for the iOS app.

Install Fastlane

Install Fastlane with below commands

sudo gem install fastlane -NV

While the installation process, also note the path where the Fastlane is saved.

My path was “/usr/local/lib/ruby/gems/2.6.0/gems/fastlane-2.140.0/”

If you have not installed command line tools for Xcode.

Install them using xcode-select --install

To check whether Fastlane is installed in your system, usefastlane -v

If you are getting error like -bash: fastlane: command not found, try to update your bash profile in terminal

open ~/.bash_profile

Then set the Fastlane path in Environmental variable.

My bash profile look as below it contains ruby path and fastlane path

export PATH=”/usr/local/lib/ruby/gems/2.6.0/bin:$PATH”
export PATH=”/usr/local/lib/ruby/gems/2.6.0/bin/fastlane/bin:$PATH”

After updating bash profile restart terminal(optional)

Also check the ruby path, if the ruby path is different(i.e. Two different ruby version installed in system), then switch ruby path before running Fastlane.

To switch ruby before running Fastlane command you can use below command

if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi

For more setup details check this awesome page

Generate Screenshots with Fastlane

  1. Go to your project directory in terminal

2. fastlane init

3. This will create ‘./fastlane’ folder and will ask below

What would you like to use fastlane for?
1. 📸 Automate screenshots
2. 👩‍✈️ Automate beta distribution to TestFlight
3. 🚀 Automate App Store distribution
4. 🛠 Manual setup — manually setup your project to automate your tasks

Type 1
This will create

  • SnapshotHelper.swift (./fastlane/SnapshotHelper.swift)
  • Snapfile (./fastlane/Snapfile)

and will ask you to create UI Test target in your project

4. Minimize terminal. Let's create the UI Test target.

a. In Xcode project, File -> New ->UI Testing Bundle and create new SampleUITestCase

b. Add SnapshotHelper.swift file(created in Fastlane folder of your project) to the newly created UITestCase i.e . SampleUITestCase.

c. Add below code in setup function of SampleUITestcase


let app = XCUIApplication()
setupSnapshot(app)
app.launch()

d. Create new function takeScreenShots() to capture screenshot of your app.

func takeScreenShots() { let app = XCUIApplication()
XCUIDevice.shared.orientation = .portrait
snapshot(“1-Login”) // Takes screenshot of Login screen

let textFieldUserName = app.textFields[“userName”]
textFieldUserName.tap()
sleep(1)
textFieldUserName.typeText(“abc@gmail.com”)

let textFieldPassword = app.secureTextFields[“passwordText”]
textFieldPassword.tap()
sleep(1)
textFieldPassword.typeText(“Abc@123456”)

sleep(1)
app.buttons[“Login”].tap()
sleep(8)
snapshot(“2-CustomerList”)
// Takes screenshot of CustomerList screen.
}

My app contains Login screen and a list screen, so I am log in by providing userName and password and taking a screenshot of the Login screen and List screen. You can replace logic according to your project. snapshot(“screenName”) capture screenshot of the present state of app.

e. Add a new Xcode scheme for the newly created UITest target
Product -> New Scheme -> Select SampleUITestCase from Target and Create.

f. Select the scheme and enable the `Shared` box and enable the ‘Run’ box in Build.

You can run the UI Testcase by Product -> Run and check whether testcase are passed.

5. Go back to the terminal and select the newly created UITestcase. If not able to select, then terminate terminal and again do fastlane init

6. After Successfully generating Fastlane configuration, check `Fastfile` in ‘/FastLane’ folder
There must be a lane called screenshots which does screenshot capturing part

7. To create screenshot run fastlane screenshots
Fastlane will create screenshots if your UI test case runs without error.

default_platform(:ios)platform :ios do
desc “Generate new localized screenshots”
lane :screenshots do
capture_screenshots(workspace: “Sample.xcworkspace”, scheme: “SampleUITests”)
end
end

To add frames to your create screenshots type brew install imagemagick

then use the command fastlane frameit

Open the Snapfile(in Fastlane folder of your project) and you can edit the file based on the requirement, you can specify the device and languages of your choice, just remove # and it will work.

# A list of devices you want to take the screenshots from
# devices([
# “iPhone 8”,
# “iPhone 8 Plus”,
# “iPhone SE”,
# “iPhone X”,
# “iPad Pro (12.9-inch)”,
# “iPad Pro (9.7-inch)”,
# “Apple TV 1080p”
# ])
# languages([
# “en-US”,
# “de-DE”,
# “it-IT”,
# [“pt”, “pt_BR”] # Portuguese with Brazilian locale
# ])
# The name of the scheme which contains the UI Tests
# scheme(“SchemeName”)
# Where should the resulting screenshots be stored?
# output_directory(“./screenshots”)
# remove the ‘#’ to clear all previously generated screenshots before creating new ones
# clear_previous_screenshots(true)
# Arguments to pass to the app on launch. See https://docs.fastlane.tools/actions/snapshot/#launch-arguments
# launch_arguments([“-favColor red”])
# For more information about all available options run
# fastlane action snapshot

Upload beta build to TestFlight

  1. Go to your project directory in terminal

2. ‘ fastlane init’

3. This will create folder ‘./fastlane’ and will ask below

What would you like to use fastlane for?
1. 📸 Automate screenshots
2. 👩‍✈️ Automate beta distribution to TestFlight
3. 🚀 Automate App Store distribution
4. 🛠 Manual setup — manually setup your project to automate your tasks

Type 2

Then accordingly add your Apple ID developer credentials, then it will setup the process for you.

Once done check Appfile in Fastlane folder you can check whether your credentials are correct also check in Fastfile a new lane will be created to push build to TestFlight.

platform :ios do
desc “Push a new beta build to TestFlight”
lane :beta do
increment_build_number(xcodeproj: “Sample.xcodeproj”)
build_app(workspace: “Sample.xcworkspace”, scheme: “Sample”)
upload_to_testflight
end

Then from next time you can just run this lane ‘beta’.

fastlane beta

This will increment your build number and push a new beta build to TestFlight.

If you have Two-factor Authentication enabled for your account then when Fastlane tries to login it will require 6 digit code for further process.

For that we can add a password in environmental variable and fastlane can use it while login,

  1. For that sign in to your Apple ID account page.
  2. Go to Security section -> App specific Passwords then add and click Generate Password below App-Specific Passwords
  3. Go to terminal open ~/.bash_profile and update in the env var
export FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD = “generated_password”

Restart the terminal and run fastlane command fastlane beta, this will upload a beta build to TestFlight

If you have any trouble understanding or implementing it or didn’t like any part of it, then let me know in the comment section below I’ll try to work on it.

To know about importing certificates to Match Git Repo and to configure multiple targets in Fastlane check this post

Thank you for reading.🙏 If this post helped you, do clap for me 😃

HaPpY CoDiNg…

--

--