Testing a simple Android app using Espresso
  February 09, 2017

In this blog post, we'll explain step by step how to automate your first test with Hiptest and Espresso.

To follow this tutorial, you'll need a few pre-requisite:

  • an Hiptest account
  • an environment for executing Espresso tests
  • hiptest-publisher installed locally (optional for the last part)

The tested application

Go to the android-testing Github project and  clone the repository to your local machine. For this tutorial, we will test the application located in ui/espresso/BasicSample. This application has a text field and display the content of it above.

espresso basic sample app.png

There is by default two tests for this application: the first one tests the behaviour of the "Change text" button, the second one tests the behavior of the "Open activity and change text" button. Both tests can be seen in the ChangeTextBehaviorTest.java file.

In this tutorial, we will rewrite the first test in Hiptest and then automate it.

Writing the test in Hiptest

Create a new project in Hiptest (called "Espresso Basic Sample" for example) and create a new scenario in it. The scenario should have the following content:

espresso basic sample scenario.png

As you can see, both steps are actionwords (which is a pre-requisite when it comes to automation) and each one has a parameter (in that case set to "Hello world").

If you do not know how to create a parameter in an actionword, that's pretty simple. When you type your first step, use double-quotes around the "Hello world" part and that will become a parameter.

Exporting and automating the Espresso test

Go to the automation tab of your project and click on the Espresso logo. You will get a zip file containing four files:

exported-espresso-tests

Copy those four files to the folder "ui/espresso/BasicSample/app/src/androidTest/java/com/example/android/testing/espresso/BasicSample" of you local copy of the android-testing project.

Now, use your favorite editor to update the content of the Actionwords.java file:

implemented-actionwords

Note that, if you renamed the parameters of the actionwords in Hiptest, the name of the function will differ. You should keep the name of the function given by hiptest-publisher and only edit the content.

The content used for the function can be copy/paster from the file "ChangeTextBehaviorTest.java" (as the API of Android/Espresso might change in the future, it would be safer to have a look to this file first).

Now open the file "ProjectTest" and update the name of the package to "com.example.android.testing.espresso.BasicSample". Now you should be able to run the test, as shown below using Android studio:

running tests with android studio.png

 

Start Hiptest for free banner

 

Going a bit further

For this last part, you will need hiptest-publisher installed on your machine. First, we will fix the issue of the package name. Open the file "hiptest-publisher.conf" and edit the line defining the package so it uses the correct one:

package = 'com.example.android.testing.espresso.BasicSample'

Now update the test file by running this command in a terminal:

cd ui/espresso/BasicSample/app/src/androidTest/java/com/example/android/testing/espresso/BasicSample 

hiptest-publisher -c hiptest-publisher.conf --only=tests

This will regenerate the test file and use the correct package.

Now let's have a look to what happens when we update our scenario, for example changing the value used in the test to "My first Espresso test":

espresso scenario updated.png

Once again, we will use hiptest-publisher and update only the test files:

hiptest-publisher -c hiptest-publisher.conf --only=tests

The content of ProjectTest.java should now look like this and can be ran again, this time using a different value for the test.

espresso-test-updated

And that's it, you now have your first test working with Hiptest and Espresso. Use the same principle to run your real tests now.

Happy mobile testing :)