Fastlane for Android — Automate Everything (Part 2)

Chan Bo
Open Knowledge
Published in
3 min readMar 15, 2019
Photo by SpaceX on Unsplash

In this part, I will explain and implement Automate Screenshots with Fastlane. If you don’t know how to set up and install fastlane, you can go to read my previous blog post here.

So now let’s get start with fastlane Automate Screenshosts.

Installing Screengrab

You can install screengrab via gem:

$ sudo gem install screengrab

Add Gradle Dependencies

Add the code below to your build.gradle app module file.

androidTestImplementation "tools.fastlane:screengrab:x.x.x"

The latest version of screengrab can be found here.

Configuring Manifest Permission

Ensure that the following permissions exist in your src/debug/AndroidManifest.xml. We create new AndroidManifest.xml in debug folder because some permission we want to use only for capture screen

Configuring UI Tests

create a new class inside src/androidTest/yourpackagename/AutomateScreenshotsTest or you can use existing class ExampleInstrumentedTest to run screengrab .

Let’s take a look on code above:

This method will run before @Test run and inside that method:

ActivityScenario.launch(MainActivity::class.java)

It mean we want to test MainActivity::class.java

Screengrab.setDefaultScreenshotStrategy(UiAutomatorScreenshotStrategy())

We use this one line of code when your device API Level ≥ 18 because it is not yet the default strategy.

Now we are going to take a look at @Test method captureScreen :

To capture screenshots, add the following to your tests Screengrab.screenshot("name_of_screenshot_here") on the appropriate screens.

So in captureScreen method will capture the screen after app launch and store as main_screen file name.

Generating Screenshots with Screengrab

To run screengrab it require 2 apk -debug.apk and -debug-AndroidTest.apk , we get these apks via ./gradlew assembleDebug assembleAndroidTest . After build success, you will see both apks inside /app.build/outputs/apk/ .

Since we are using fastlane so we can let it build for us. So now let’s create lane name capture_screen . This lane is going to build -debug.apk and -debug-AndroidTest.apk then take a screenshot.

If you are using androidx you have to create a file name Screengrabfile inside fastlane folder and add the following code:

test_instrumentation_runner 'androidx.test.runner.AndroidJUnitRunner'

Now we can run that lane via $ bundle exec fastlane capture_screen

When executing capture_screen lane success, it will redirect to your browser and show all your screenshots or you can find them in /fastlane/metadata/android/en-US/images/phoneScreenshots/

Congratulation you have done your Automate Screenshots.

Advance Screengrab Configuration

Every time you run capture_screen lane, it will always ask for app_apk_path and tests_apk_path so to not asked about apk path, you can specific apk path in Screengrabfile :

Fore more about Automate Screenshots you can look at Fastlane Screenshots.

That’s all about Fastlane Automate Screenshots. In the next blog I will show you how to implement Beta Deployment in Fastlane. If you have any problem, please let me know! 😃 Thanks. 💯.

References

--

--