Moving JUnit Selenium Tests to ReadyAPI

Selenium tests are often written by using Java. Since Groovy has syntax similar to Java, these tests can be moved to ReadyAPI with minor adjustments.

So, to move your JUnit Selenium test to ReadyAPI, you can copy the code and paste it to the Groovy Script test step.

Some Java features are not supported by Groovy, and parts of your code may behave in a different manner after the transition, or your test may fail to start.

Important

If you use Maven dependencies to include JUnit libraries in your project, you need to manually copy them to the <ReadyAPI installation>/bin/ext directory.

You can find the full list of differences between Groovy and Java in the Groovy documentation.

Another thing you may want to do after moving to ReadyAPI is to improve the style. Groovy is very forgiving in terms of style. For example, you do not need to end the line with semicolons.

Example

Suppose, you have the following JUnit test that uses Selenium:

1. Preparation

To use JUnit and Selenium classes in a Groovy script, you need to add their libraries to ReadyAPI.

  1. Copy Selenium libraries as described in Requirements for Running Selenium Tests.

  2. Download the junit.jar file from the following page:

  3. Copy the downloaded file to the <ReadyAPI installation>/bin/ext directory.

2. Copy code to ReadyAPI

  1. Add the Groovy test step to your test and open it.

  2. Paste your Java code to the created test step:

    Integration with Selenium: Copied Java code in Groovy test step

3. Modify your code

Most likely, your code will not work as expected, so you need to modify it.

3.1 Specify the location of the Gecko driver

Selenium searches for the Gecko driver in the webdriver.gecko.driver system property. You need to specify the driver location in one of the following ways:

  • Add the -Dwebdriver.gecko.driver=<path to gecko driver> variable to the list of JVM options. See Modifying JVM Settings to learn how to do this.

  • Set the system property value in the Groovy test step:

3.2 Add JUnit test runner

To make the best use of JUnit tests in ReadyAPI, run them by using the JUnit test runner.

To run our sample test, we need to add the following code to the Groovy test step:

3.3 Log test errors

If we run our test now, it will pass, whatever the JUnit test results are. To fix that, we need to add the assert command that marks the test step as failed and posts an error message.

Below is the complete code of the Groovy Script test step:

See Also

Publication date: