Shot


Source link: https://github.com/karumi/shot

Shot

Shot is a Gradle plugin that simplifies the execution of screenshot tests using Screenshot Tests For Android by Facebook.

What is this?

Shot is a Gradle plugin thought to run screenshot tests for Android using the screenshot testing Facebook SDK.

Since Shot 0.3.0 a simple but powerful HTML report is generated after every verification or screenshots recording execution. Here you have an example of the recording and verification report generated.

Record your screenshots executing ./gradlew executeScreenshotTests -Precord

And verify your tests executing ./gradlew executeScreenshotTests

If Shot finds any error in your tests execution the Gradle plugin will show a report as follows:

You can find the complete Facebook SDK documentation here.

Getting started

Setup the Gradle plugin:

  buildscript {

  // ...
  dependencies {

 // ...

 classpath 'com.karumi:shot:0.3.0'
  
}

}

apply plugin: 'shot'

shot {

  appId = 'YOUR_APPLICATION_ID'

}

This plugin sets up a few convenience commands you can list executing ./gradlew tasks and reviewing the Shot associated tasks:

If you are using flavors update your shot configuration inside the build.gradle file as follows:

  shot {

  appId = 'YOUR_APPLICATION_ID'
  instrumentationTestTask = 'connected<FlavorName><BuildTypeName>AndroidTest'
  packageTestApkTask = 'package<FlavorName><BuildTypeName>AndroidTest'

}

The flavor used is the one selected to execute your screenshot tests.

An example could be:

  shot {

  appId = 'com.my.app'
  instrumentationTestTask = 'connectedFreeAppDebugAndroidTest'
  packageTestApkTask = 'packageFreeAppAndroidTest'

}

The screenshots library needs the WRITE_EXTERNAL_STORAGE permission. When testing a library, add this permission to the manifest of the instrumentation apk. If you are testing an application, add this permission to the app under test. To grant this permission you can create an AndroidManifest.xml file inside the androidTest folder. Here is an example:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="<YOUR_APP_ID>.tests"
  android:sharedUserId="<YOUR_APP_ID>.uid">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  </manifest> 

Remember to configure the instrumentation test runner in your build.gradle as follows:

android {

  ...
  defaultConfig {

...

testInstrumentationRunner "com.myapp.ScreenshotTestRunner"
  
}
  

In order to do this, you'll have to create a class named ScreenshotTestRunner, like the following one, inside your instrumentation tests source folder:

public class ScreenshotTestRunner extends AndroidJUnitRunner {

@Override
  public void onCreate(Bundle args) {

super.onCreate(args);

ScreenshotRunner.onCreate(this, args);

  
}

@Override
  public void finish(int resultCode, Bundle results) {

ScreenshotRunner.onDestroy();

super.finish(resultCode, results);

  
}
 
}

Now you are ready to use the Screenshot API from your tests:

@Test public void theActivityIsShownProperly() {

Activity mainActivity = startMainActivity();

  /* 

* Take the actual screenshot. At the end of this call, the screenshot 

* is stored on the device and the gradle plugin takes care of 

* pulling it and displaying it to you in nice ways. 

*/

Screenshot.snapActivity(activity).record();
 
}
 

You can find a complete example in this repository under the folder named shot-consumer or review this kata.

The official documentation.

Now you are ready to record and verify your screenshot tests!

Recording tests

You can record your screenshot tests executing this command:

./gradlew executeScreenshotTests -Precord 

This will execute all your integration tests and it will pull all the generated screenshots into your repository so you can easily add them to the version control system.

Executing tests

Once you have a bunch of screenshot tests recorded you can easily verify if the behaviour of your app is the correct one executing this command:

./gradlew executeScreenshotTests 

After executing your screenshot tests using the Gradle task executeScreenshotTests a report with all your screenshots will be generated.

Custom dependencies

If you have included in your project a dependency to related to the dexmaker and you are facing this exception: com.android.dx.util.DexException: Multiple dex files define, you can customize how the facebook SDK is added to your project and exclude the dexmaker library as follows:

  androidTestCompile ('com.facebook.testing.screenshot:core:0.4.2') {

  exclude group: 'com.crittercism.dexmaker', module: 'dexmaker'
  exclude group: 'com.crittercism.dexmaker', module: 'dexmaker-dx'

}
 

The Shot plugin automatically detects if you are including the screenshot facebook library in your project and, if it's present, it will not include it again.

Resources

A simple Android library that allows an ad slideshow to be added into any view in your app. A request is made to your server that hosts a XML file that lists urls for the location of ads to be displayed in your app. The images are then cached on disk for a simple slideshow to run through each image.

A full example of custom fonts in XML using data binding and including font caching.

Swipe right to display drawer with flowing effects.

Customizable Snackbar with material design.

A smooth download button with progress.

RxFlux is small light-weight framework that makes it easy to follow the Flux pattern with RxJava functionalities.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes