Are you tired of writing tests which have a lot of boilerplate code? If so, get started with Spock Framework >>

Transforming Recorded TestProject Tests Into Java Code

The TestProject team published a new feature that allows us to transform our recorded tests into Java code and download the generated code. This blog post explains when this feature is useful to us and describes how we can use this feature.

After we have finished this blog post, we:

  • Know when we should transform recorded tests into Java code.
  • Can download a recorded test as Java code.
  • Are familiar with the contents of the generated Gradle project.
  • Know how we can make the required changes to the generated Gradle project.
  • Can upload our test case to the app.testproject.io website and run the uploaded test.

Let’s begin.

This blog post is the eight part of my TestProject tutorial that is sponsored by TestProject.io. However, the views and opinions expressed in this tutorial are mine.

This blog post assumes that:

By the way, you might want to read the other parts of my TestProject tutorial.

Why Should We Transform Recorded Tests Into Java Code?

It isn’t a secret that code generation is a bit controversial topic. I think that the biggest reason for this situation is that even though some code generators create code that is “good enough”, there are a lot of code generators that create horrible code.

In other words, before we use the code generator provided by the app.testproject.io web application, we have to identify the situations when this code generator is useful to us. These situations are:

  • We want to debug a failing test case on our computer. If a test case fails, most of the time the fastest way to identify the root cause of the test failure is to debug the failing test case on our computer. The benefit of this approach is that we can insert breakpoints to our test code and see the actual user interface when the test execution stops at a breakpoint.
  • We need maximum flexibility. Even though TestProject has a good test recorder, the test recorder is still just a tool that has its limitations. If we cannot change our test by using the test recorder, we don’t have to abandon the test. We can simply download the source code of our test case, make the required changes, and upload the test back to app.testproject.io web site.
  • We have to make changes to a recorded test. I think that the test recorder is a valuable tool if you don’t know how to write code. However, if you can write code, the situation is a bit different because it doesn’t really make sense to use two different tools for the same purpose. I think that we must always choose the tool that allows us to be more productive, and that’s why I argue that developers should favor code over the test recorder.
  • We want to learn to use the TestProject API. Even though I think that the best to way to learn to use an API is to write code that uses the API, sometimes it’s hard to get started. If we end up in this situation, it’s useful to record a few simple tests, download these tests as Java code, and study (and change) the downloaded tests.

Next, we will find out how we can download recorded TestProject tests as Java code.

Downloading the Recorded TestProject Tests as Java Code

We can transform a recorded test into Java code and download the generated code by following these steps:

First, we have to log in to the app.testproject.io website.

Second, we have to navigate to the ‘Tests’ view of the ‘Project’ page. We can do this by opening the ‘Project’ page (The ‘Tests’ view is shown when we open the ‘Project’ page). Also, we can open this view by clicking the ‘Tests & Jobs’ link found from the ‘Project’ page.

After we have opened the ‘Tests’ view, we should see a recorded test called: ‘Exported Blog Search Test’. The following figure displays the content of the ‘Tests’ view:

Third, we have to start the export test wizard by following these steps:

  1. Move the mouse cursor on the top of the HTML element that displays the information of the exported test.
  2. Click the ‘more’ icon (three dots) found from the right side of the HTML element. This opens a drop-down menu which allows us to generate the exported test code.
  3. Select the ‘Generate Code’ menu item. This opens a modal dialog which allows us to configure the options of our export.

The following figure illustrates this step:

Fourth, we have to configure our export by setting the values of these configuration options:

  • The target programming language. At the moment, TestProject can generate only Java code, but it will support C# in the future as well.
  • The web browser that’s used to run the exported test. When TestProject generates the exported code, it will also generate a runner class that can run the exported test. This configuration option specifies the browser that’s started by the generated runner class.
  • The package that contains the generated code.

The following figure illustrates this step:

Fifth, we have to generate the exported code by clicking the ‘Generate’ button. The following figure illustrates this step:

Sixth, we have to close the modal dialog by clicking the ‘OK’ button. The following figure demonstrates this step:

Seventh, we have to log in to our email account and find an email that’s send by TestProject and has the subject: ‘Your source code is ready for download!’. After we have found this email, we have to open it and click the ‘Download Code’ button.

The following figure illustrates this step:

Eighth, we have to download the zip file that contains generated code.

We have now transformed our recorded test into Java code and downloaded the generated code. Let’s move on and find out how we can create a Gradle project that can compile and package the exported code.

Creating Our Gradle Project

When we transform a recorded test into Java code, TestProject generates a Gradle project that that can compile and package the exported code (almost). The directory structure of the generated Gradle project looks as follows:

  • The root directory of the Gradle project contains the build.gradle and settings.gradle files.
  • The lib directory contains the addon proxies (jar files) used by the generated test.
  • The src/main/java directory contains the generated code. To be more specific, the generated code is found from the package which we provided to TestProject when we generated the exported code. This package contains the exported test class, one utility class, and a test runner class that can run the exported test on the development environment.

Even though TestProject does most of the heavy lifting for us, we still have to do two things before we can compile, package, or run the exported test. These things are:

  1. Copy the TestProject SDK jar file (io.testproject.sdk.java.jar) to the lib directory.
  2. Configure the location of the TestProject SDK jar file by changing the value of the TP_SDK variable found from the generated build.gradle file.

After we configured the location of the TestProject SDK, the relevant part of our build.gradle file looks as follows:

// If necessary, update the location of TestProject SDK JAR file
def TP_SDK = 'lib/io.testproject.sdk.java.jar'

We have now made all the required changes to the generated Gradle project. Next, we will find out how we can upload the exported test to the app.testproject.io website and run the uploaded test.

Uploading and Running Our Tests

When we want to upload the exported test to the app.testproject.io website and run it, we have to follow these steps:

  1. Package the exported test to a jar file by running the command: gradle clean build.
  2. Upload the jar file to the app.testproject.io website by following the instructions given on this blog post.

We can now transform a recorded test into Java code, upload the exported test to the app.testproject.io website, and run the uploaded test.

Let’s summarize what we learned from this blog post.

Summary

This blog post has taught us three things:

  • When we download a recorded test as Java code, TestProject generates a Gradle project that can compile and package our test.
  • If the exported test uses addons, TestProject includes the required Addon proxies in the generated Gradle project.
  • We have to copy the TestProject SDK jar file (io.testproject.sdk.java.jar) to the lib directory and configure its location by changing the value of the TP_SDK variable found from the generated build.gradle file.

P.S. You can get the example application of this blog post from Github.

3 comments… add one
  • thank you for sharing

    Reply

Leave a Comment