Skip to content

Commit

Permalink
Improve screengrab
Browse files Browse the repository at this point in the history
  • Loading branch information
penn5 committed Jan 20, 2021
1 parent e2ff499 commit 8381b6a
Show file tree
Hide file tree
Showing 28 changed files with 471 additions and 506 deletions.
51 changes: 25 additions & 26 deletions fastlane/lib/fastlane/actions/docs/capture_android_screenshots.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,28 @@ As of Screengrab version 2.0.0, all Android test dependencies are AndroidX depen
Ensure that the following permissions exist in your **src/debug/AndroidManifest.xml**

```xml
<!-- Allows unlocking your device and activating its screen so UI tests can succeed -->
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<!-- Allows for storing and retrieving screenshots -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- Allows storing screenshots on external storage, where it can be accessed by ADB -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" />

<!-- Allows changing locales -->
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
<!-- Allows changing locales -->
<uses-permission
android:name="android.permission.CHANGE_CONFIGURATION"
tools:ignore="ProtectedPermissions" />

<!-- Allows changing SystemUI demo mode -->
<uses-permission
android:name="android.permission.DUMP"
tools:ignore="ProtectedPermissions" />

</manifest>
```

##### Configuring your <a href="#ui-tests">UI Tests</a> for Screenshots

1. Add `LocaleTestRule` to your tests class to handle automatic switching of locales.
1. Add `LocaleTestRule` to your tests class to handle automatic switching of locales.
If you're using Java use:
```java
@ClassRule
Expand All @@ -63,7 +70,7 @@ Ensure that the following permissions exist in your **src/debug/AndroidManifest.
@Rule @JvmField
val localeTestRule = LocaleTestRule()
```
Important is the `@JvmField` annotation. It won't work like that:
The `@JvmField` annotation is important. It won't work like this:
```kotlin
companion object {
@get:ClassRule
Expand All @@ -79,7 +86,7 @@ Ensure that the following permissions exist in your **src/debug/AndroidManifest.
- You can also create a lane and use `build_android_app`:
```ruby
desc "Build debug and test APK for screenshots"
lane :build_for_screengrab do
lane :build_and_screengrab do
build_android_app(
task: 'assemble',
build_type: 'Debug'
Expand All @@ -88,6 +95,7 @@ Ensure that the following permissions exist in your **src/debug/AndroidManifest.
task: 'assemble',
build_type: 'AndroidTest'
)
screengrab()
end
```
- Once complete run `fastlane screengrab` in your app project directory to generate screenshots
Expand Down Expand Up @@ -163,7 +171,7 @@ public class JUnit4StyleTests {
public static final LocaleTestRule localeTestRule = new LocaleTestRule();

@Rule
public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(MainActivity.class);
public ActivityScenarioRule<MainActivity> activityRule = new ActivityScenarioRule<>(MainActivity.class);

@Test
public void testTakeScreenshot() {
Expand All @@ -181,7 +189,7 @@ Kotlin:
@RunWith(JUnit4.class)
class JUnit4StyleTests {
@get:Rule
var activityRule = ActivityTestRule(MainActivity::class.java)
var activityRule = ActivityScenarioRule(MainActivity::class.java)

@Rule @JvmField
val localeTestRule = LocaleTestRule()
Expand All @@ -208,22 +216,13 @@ When using JUnit 3 you'll need to add a bit more code:
- Use `LocaleUtil.changeDeviceLocaleTo(LocaleUtil.getEndingLocale());` in `tearDown()`
- Use `Screengrab.screenshot("name_of_screenshot_here");` to capture screenshots at the appropriate points in your tests

If you're having trouble getting your device unlocked and the screen activated to run tests, try using `ScreenUtil.activateScreenForTesting(activity);` in your test setup.

## Clean Status Bar

Screengrab can clean your status bar to make your screenshots even more beautiful.
Screengrab can clean your status bar to make your screenshots even more beautiful.
It is simply a wrapper that allows configuring SystemUI DemoMode in your code.
Note: the clean status bar feature is only supported on devices with *API level >= 23*.

To use the clean status bar feature add the following lines to your src/debug/AndroidManifest.xml
```xml
<!-- Indicates the use of the clean status bar feature -->
<uses-feature android:name="tools.fastlane.screengrab.cleanstatusbar"/>
<!-- Allows for changing the status bar -->
<uses-permission android:name="android.permission.DUMP"/>
```

After that you can enable and disable the clean status bar at any moment during your tests.
You can enable and disable the clean status bar at any moment during your tests.
In most cases you probably want to do this in the @BeforeClass and @AfterClass methods.
```java
@BeforeClass
Expand All @@ -237,7 +236,7 @@ public static void afterAll() {
}
```

Have a look at the methods of the `CleanStatusBar` class to customize the status bar even more.
Have a look at the methods of the `CleanStatusBar` class to customize the status bar even more.
You could for example show the Bluetooth icon and the LTE text.
```java
new CleanStatusBar()
Expand Down
6 changes: 3 additions & 3 deletions screengrab/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
gradlePluginPortal()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath 'com.android.tools.build:gradle:4.1.1'
classpath 'com.github.ben-manes:gradle-versions-plugin:0.20.0'
}
}
Expand All @@ -20,7 +20,7 @@ allprojects {
}
}

Properties loadProperties(File propsFile) {
static Properties loadProperties(File propsFile) {
new Properties().with { props ->
propsFile.withInputStream { stream ->
props.load(stream)
Expand All @@ -32,7 +32,7 @@ Properties loadProperties(File propsFile) {
/**
* @return an Array containing the [versionNameString, versionCodeInt]
*/
Object[] screengrabVersion(File propsFile) {
static Object[] screengrabVersion(File propsFile) {
Properties versionProps = loadProperties(propsFile)

int versionMajor = (versionProps['major'] ?: 0) as int
Expand Down
15 changes: 8 additions & 7 deletions screengrab/example/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 29
compileSdkVersion 30

defaultConfig {
applicationId "tools.fastlane.localetester"
minSdkVersion 18
targetSdkVersion 29
targetSdkVersion 30
versionCode 1
versionName "1.0"

Expand All @@ -28,14 +28,15 @@ android {
}

dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'

testImplementation 'junit:junit:4.12'

androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test:rules:1.3.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
androidTestImplementation project(':screengrab-lib')
}
6 changes: 6 additions & 0 deletions screengrab/example/fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ platform(:android) do
gradle(task: "assemble assembleAndroidTest", gradle_path: '../gradlew')
end

desc("Build debug and test APK for screenshots")
lane(:build_and_screengrab) do
gradle(task: "assembleDebug assembleAndroidTest")
capture_android_screenshots
end

# You can define as many lanes as you want

after_all do |lane|
Expand Down
49 changes: 49 additions & 0 deletions screengrab/example/fastlane/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
fastlane documentation
================
# Installation

Make sure you have the latest version of the Xcode command line tools installed:

```
xcode-select --install
```

Install _fastlane_ using
```
[sudo] gem install fastlane -NV
```
or alternatively using `brew install fastlane`

# Available Actions
## Android
### android test
```
fastlane android test
```
Runs all the tests
### android beta
```
fastlane android beta
```
Submit a new Beta Build to Crashlytics Beta
### android deploy
```
fastlane android deploy
```
Deploy a new version to the Google Play
### android assemble
```
fastlane android assemble
```
Assemble the debug app and tests APKs
### android build_and_screengrab
```
fastlane android build_and_screengrab
```
Build debug and test APK for screenshots

----

This README.md is auto-generated and will be re-generated every time [fastlane](https://fastlane.tools) is run.
More information about fastlane can be found on [fastlane.tools](https://fastlane.tools).
The documentation of fastlane can be found on [docs.fastlane.tools](https://docs.fastlane.tools).
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8-all.zip
Empty file modified screengrab/example/gradlew
100644 → 100755
Empty file.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tools.fastlane.localetester;

import androidx.test.rule.ActivityTestRule;
import androidx.test.core.app.ActivityScenario;
import androidx.test.ext.junit.rules.ActivityScenarioRule;

import org.junit.ClassRule;
import org.junit.Rule;
Expand All @@ -26,13 +27,17 @@ public class FalconScreenshots {
public static final LocaleTestRule localeTestRule = new LocaleTestRule();

@Rule
public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(MainActivity.class, false, false);
public ActivityScenarioRule<MainActivity> activityRule = new ActivityScenarioRule<>(MainActivity.class);


@Test
public void testTakeScreenshot() {
activityRule.launchActivity(null);
Screengrab.setDefaultScreenshotStrategy(new FalconScreenshotStrategy(activityRule.getActivity()));
activityRule.getScenario().onActivity(new ActivityScenario.ActivityAction<MainActivity>() {
@Override
public void perform(MainActivity activity) {
Screengrab.setDefaultScreenshotStrategy(new FalconScreenshotStrategy(activity));
}
});

onView(withId(R.id.greeting)).check(matches(isDisplayed()));

Expand All @@ -45,8 +50,12 @@ public void testTakeScreenshot() {

@Test
public void testTakeMoreScreenshots() {
activityRule.launchActivity(null);
Screengrab.setDefaultScreenshotStrategy(new FalconScreenshotStrategy(activityRule.getActivity()));
activityRule.getScenario().onActivity(new ActivityScenario.ActivityAction<MainActivity>() {
@Override
public void perform(MainActivity activity) {
Screengrab.setDefaultScreenshotStrategy(new FalconScreenshotStrategy(activity));
}
});

onView(withId(R.id.nav_button)).perform(click());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tools.fastlane.localetester;

import androidx.test.rule.ActivityTestRule;
import androidx.test.core.app.ActivityScenario;
import androidx.test.ext.junit.rules.ActivityScenarioRule;

import org.junit.AfterClass;
import org.junit.BeforeClass;
Expand All @@ -10,6 +11,8 @@
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.util.concurrent.CountDownLatch;

import tools.fastlane.screengrab.Screengrab;
import tools.fastlane.screengrab.UiAutomatorScreenshotStrategy;
import tools.fastlane.screengrab.cleanstatusbar.BluetoothState;
Expand All @@ -30,14 +33,14 @@ public class JUnit4StyleTests {
public static final LocaleTestRule localeTestRule = new LocaleTestRule();

@Rule
public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(MainActivity.class);
public ActivityScenarioRule<MainActivity> activityRule = new ActivityScenarioRule<>(MainActivity.class);

@BeforeClass
public static void beforeAll() {
Screengrab.setDefaultScreenshotStrategy(new UiAutomatorScreenshotStrategy());

new CleanStatusBar()
.setMobileNetworkDataType(MobileDataType.THREEG)
.setMobileNetworkDataType(MobileDataType.ONEX)
.setBluetoothState(BluetoothState.DISCONNECTED)
.enable();
}
Expand All @@ -60,7 +63,7 @@ public void testTakeScreenshot() {

@Test
public void testTakeMoreScreenshots() {
onView(withId(R.id.nav_button)).perform(click());
onView(withId(R.id.nav_button)).perform(click()); // TODO: does not always finish displaying

Screengrab.screenshot("anotherActivity");

Expand Down
23 changes: 11 additions & 12 deletions screengrab/example/src/debug/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="tools.fastlane.localetester"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-feature android:name="tools.fastlane.screengrab.cleanstatusbar"/>
<!-- Allows changing locales -->
<uses-permission
android:name="android.permission.CHANGE_CONFIGURATION"
tools:ignore="ProtectedPermissions" />

<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.DUMP" tools:ignore="ProtectedPermissions" />
<!-- Allows changing SystemUI demo mode -->
<uses-permission
android:name="android.permission.DUMP"
tools:ignore="ProtectedPermissions" />

</manifest>
</manifest>
4 changes: 2 additions & 2 deletions screengrab/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Aug 22 16:28:53 CDT 2019
#Sun Jan 10 17:30:32 GMT 2021
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8-all.zip

0 comments on commit 8381b6a

Please sign in to comment.