Polymorphic Step Definitions with Cucumber-jvm

Just a quick snippet that I am using to create Polymorphic Step Definitions with cucumber-jvm.

First you need to import picocontainer library.

When you can use it ? It’s useful if you have for example Integration and UI scenarios in the same project. For example, for mobile tests I am using robotium, but for the server tests I am using HTTPClient Apache.

import cucumber.runtime.java.picocontainer.PicoFactory;

public class TestsFactory extends PicoFactory {
public TestsFactory() {
if ("integration".equals(System.getProperty("com.rsouza.androidTest.integration"))) {
You just need define Cucumber to use cucumber.api.java.ObjectFactory system property.

  • Create a cucumber.properties file on your classpath and if you are using Maven, this should be src/test/resources/cucumber.properties. If you are using gradle this should be src/androidTest/res/cucumber.properties 
  • Add the following line: cucumber.api.java.ObjectFactory = my.features.CustomPicoFactory

Thank you guys ! See you next week 🙂

Resources: https://cucumber.io/blog/2015/07/08/polymorphic-step-definitions

Spoon and Cucumber take screenshot

Hi guys,

Today I will post the code that I’ve improved to make the Cucumber take the screenshots and spoon use them:

Your helper class:

public void takeScreenshot() {
    String[] formattedInfo = treatFeatureScenarioStrings();
    mFeature = formattedInfo[0];
    mScenario = formattedInfo[1];

    if (mScenario == null) {
        throw new ScreenshotException("Error taking screenshot: I'm missing a 
valid test mScenario to attach the screenshot to");
    String tag = Thread.currentThread().getStackTrace()[3].getMethodName();
tag, mFeature, mScenario);


This method is formatting the mScenario info to get the mFeature and 
mScenario namesand format them for the same format which Spoon is 
using. The name mFeature needs to follow this
pattern: Feature Test something and mScenario: Scenario Test something
- First letter of the first word of mScenario in uppercase, the same 
for mFeature
- All the others letters need to be in lowercase
- Before mFeature's name put the word Feature and before mScenario's 
name put the word Scenario
- When Scenario Outline the prefix of the scenario needs to be diferent

private String[] treatFeatureScenarioStrings() {
    String mFeatureCap = mScenarioInfo[0].substring(0, 1).toUpperCase() 
+ mScenarioInfo[0].substring(1);
    String mScenarioCap = mScenarioInfo[1].substring(0, 1).toUpperCase() 
+ mScenarioInfo[1].substring(1);
    mFeature = "Feature " + mFeatureCap.replace("-", " ");
    mScenario = "Scenario " + mScenarioCap.replace("-", " ");

    if (mScenarioInfo.length > 2) {
        mScenario = "Scenario Outline " + mScenarioCap.replace("-", " ");

    return new String[]{mFeature, mScenario};

private static class ScreenshotException extends RuntimeException {
    ScreenshotException(final String message) {


ScreenshotTaker class:

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import timber.log.Timber;
import static org.assertj.core.api.Assertions.assertThat;
public abstract class ScreenshotTaker {
private static Set<String> mClearedOutputDirectories = new HashSet();
 static final String NAME_SEPARATOR = "_";
 static final String SPOON_SCREENSHOTS = "spoon-screenshots";
 public static File screenshot(Activity activity, ArrayList<View> views
, String tag, String testClassName, String testMethodName) {
 return screenshot(activity, getRecentDecorView(views), tag, 
testClassName, testMethodName);
public static File screenshot(Activity activity, View view, String tag, 
String testClassName, String testMethodName) {
 try {
 if (view == null) {
 File viewFile = null;
 Log.e("Spoon", "Unable to take screenshot of this view, because it is 
 return viewFile;
File parentFolder = obtainScreenshotDirectory
(activity.getApplicationContext(), testClassName, testMethodName);
 final String screenshotName = System.currentTimeMillis() + 
NAME_SEPARATOR + tag + ".png";
 File screenshotFile = new File(parentFolder, screenshotName);
getBitmapOfView(view, activity, screenshotFile);
 Log.d("Spoon", "Captured screenshot \'" + tag + "\'.");
 return screenshotFile;
 } catch (Exception var7) {
 return null;
 private static void getBitmapOfView(final View view, Activity activity
, final File screenShotFile) {
 activity.runOnUiThread(new Runnable() {
 public void run() {
 Bitmap orig = view.getDrawingCache();
 android.graphics.Bitmap.Config config = null;
 if (orig == null) {
 Timber.e("Bitmap is null");
 } else {
 config = orig.getConfig();
 if (config == null) {
 config = android.graphics.Bitmap.Config.ARGB_8888;
 Bitmap mBitmap = orig.copy(config, false);
FileOutputStream mFileOutput = null;
 try {
 mFileOutput = new FileOutputStream(screenShotFile);
 if (mFileOutput != null) {
 mBitmap.compress(Bitmap.CompressFormat.PNG, 100, mFileOutput);
 screenShotFile.setReadable(true, false);
 } catch (IOException e) {
private static File obtainScreenshotDirectory(Context context, 
String testClassName, String testMethodName) {
 return filesDirectory(context, SPOON_SCREENSHOTS, testClassName, 
private static File filesDirectory(Context context, String directoryType,
 String testClassName, String testMethodName) {
 File directory = null;
 if (Build.VERSION.SDK_INT >= 21) {
 directory = new File(Environment.getExternalStorageDirectory(), "app_" + 
 } else {
 directory = context.getDir(directoryType, 1);
if (!mClearedOutputDirectories.contains(directoryType)) {
 deletePath(directory, false);
File dirClass1 = new File(directory, testClassName);
 File dirMethod = new File(dirClass1, testMethodName);
if (!dirMethod.exists()) {
return dirMethod;
private static void createDir(File dir) {
 File parent = dir.getParentFile();
 if (!parent.exists()) {
if (!dir.exists() && !dir.mkdirs()) {
 Timber.e("Unable to create output dir: " + dir.getAbsolutePath());
 } else {
 private static void deletePath(File path, boolean inclusive) {
 if (path.isDirectory()) {
 File[] children = path.listFiles();
 if (children != null) {
 File[] arr$ = children;
 int len$ = children.length;
for (int i$ = 0; i$ < len$; ++i$) {
 File child = arr$[i$];
 deletePath(child, true);
if (inclusive) {
 * Returns the most recent DecorView
 * @param views the views to check
 * @return the most recent DecorView
public static final View getRecentDecorView(ArrayList<View> views) {
 if (views == null) {
 Timber.e("Error in getRecentDecorView: 0 views passed in.");
 return null;
final View[] decorViews = new View[views.size()];
 int i = 0;
 View view;
for (int j = 0; j < views.size(); j++) {
 view = views.get(j);
 if (view != null && view.getClass().getName()
 .equals("com.android.internal.policy.impl.PhoneWindow$DecorView")) {
 decorViews[i] = view;
 return getRecentContainer(decorViews);
 * Returns the most recent view container
 * @param views the views to check
 * @return the most recent view container
private static final View getRecentContainer(View[] views) {
 View container = null;
 long drawingTime = 0;
 View view;
for (int i = 0; i < views.length; i++) {
 view = views[i];
 if (view != null && view.isShown() && view.hasWindowFocus() && 
view.getDrawingTime() > drawingTime) {
 container = view;
 drawingTime = view.getDrawingTime();
 return container;

Please fell free to improve if you have any comment and suggestion it would be great !
Thank you guys, see you next week !

Get json path from an authentication request


This example I am getting json path from an authentication request and use it in another group thread.
Remember install the json plugin for jmeter (If you don’t have), you can do that with homebrew command :

brew install jmeter --with-plugins


  • Create a test plan and set the web server and the port:


Screen Shot 2015-11-11 at 20.08.27


  • Create a Thread Group:


Screen Shot 2015-11-11 at 20.08.44

  • Create HTTP Request, use the variables for the server and the password. Put the path of the authentication page, username and the password.


Screen Shot 2015-11-11 at 20.09.09

  • Create Json Path Extractor and put the path Expression and the variable that you will use. You can test if your path is correct here.


Screen Shot 2015-11-11 at 20.10.14


  • Create a beanshell Assertion or Post processor and set the property:



Screen Shot 2015-11-11 at 20.10.27


  • Create a new Thread Group and a HTTP Header Manager and use the same variable you used before:



Screen Shot 2015-11-11 at 20.42.41


  • Create the Listener > View Result Tree and it’s done, you can run the jmeter and see if it’s getting the token and using in the next thread group


See you guys 🙂

Java Multithreading – Free course

Hi guys, today I will post the link of a free course that I’ve been doing these last days. So, if you are interested to know a bit more of java multithreading, here is the link.




It’s teaching how to manage threads, if you need lock them, synchronize… The instructor knows how to explain things in a clear way which sometimes is really hard to find. So, I hope you like it and be useful as it’s being for me.

Thank you guys ! See you next week !