Skip to main content

First test

Setup

Clone our example repository with git:

cd my-root-folder-of-cool-projects
git clone https://pumpitup@dev.azure.com/pumpitup/pumpo-number-five/_git/pumpo-starter-kit

Open the newly created folder pumpo-starter-kit with your IDE as existing project and make sure it is recognised as a standard Maven project.

In the project navigation, create a new file in src/test/java/cz/pumpitup/pn5/starterkit/SimpleNavigationTest.java. It should look like this:

package dev.pumpo5.starterkit;

import org.junit.jupiter.api.Test;

public class SimpleNavigationTest {

@Test
public void test(Firefox firefox) {
firefox
.waitSecondsOf(10)
.openGoogleSearchPage()
.acceptCookies()
.typeIntoSearchBox("pumpitup sro")
.submit()
.onSearchResultsPage()
.clickThroughFirstResult()
.onPumpITupHomePage()
.assertThatContainsText("Solving tough problems with automation");
}
}

If everything is correct, the IDE should propose a play button next to the line public void test(Firefox firefox). In IntelliJ the play button is visible on the left martin. In Visual Studio you have to move your mouse over the name of the method (test) for a play option to appear.

Run the test

Now make sure that your local test farm is up and running. If you are using some other WebDriver compatible test farm, modify the url in the configuration file at: src/test/resources/config.conf

Run from IDE

Click on the play button next to the test. Your IDE will automatically launch the phase Maven test.

The first run might take some time to initialise because Maven will download some dependencies and build the project. Next runs will be much faster.

If everything went well, your IDE will indicate that the test passed. If not, there will be red exceptions displayed in a run window, and the test will be marked as failed.

Congratulations! You have run your first test.

Run from command line

The test can be run from the commandline from the root of the project:

cd pumpo-starter-kit
mvn -DTest=SimpleNavigationTest test

The logs should indicate many (now useless) things regarding compilation and then progress and results of the test.

Note regarding logs: Maven always publishes tests in a standard format that is usually accepted by any CI server (Jenkins, Gitlab, Azure DevOps, etc...) in a predefined folder. So if you run tests within your CI server, results will be available in a much nicer form. Also, Pumpo#5 allows reporting test results to various test management tools such as JIRA TestFlo including each step.

Following test progress

You can follow the test progress in the user interface of your local test farm. When a test is running the session is visible on the main window. By opening the session you can see both logs from the browser that the test is using, and the screen (it's a VNC session). The user interface of your local test farm is on http://localhost:8080.

There is one issue though: tests are rather fast, and before you have time to open the browser through the user interface it is usually finished. To prevent that you can either:

  • Run the test in debug mode and add a breakpoint on the first step
  • Introduce a short pause at the beginning of the test

Debug mode

The test can be run in debug mode (as any other program in Java) by simply selecting the option Debug instead of Run. In most IDEs this is the second option next to running the test.

Before launching the test in debug mode, introduce a break-point on the first line of the test. This can be done by clicking on the left margin next to the selected line. A break-point will cause the test to be paused once reaching that line of code. Then you have the following options to continue:

  • Resume program - The test will run until the next break-point
  • Step out - This will run one step and consider any call as a step
  • Step in - This will one step and go inside called methods

A typical usage of those features is to set one break-point where the tricky part starts and then execute each step manually using step-out.

Short pause

To introduce a short pause at the beginning of the test, uncomment the line waitSecondsOf(10) as follows:

    @Test
public void test(Firefox firefox) {
firefox
.waitSecondsOf(10)
.openGoogleSearchPage()
.acceptCookies()
.typeIntoSearchBox("pumpitup sro")
.submit()
.onSearchResultsPage()
.clickThroughFirstResult()
.onPumpITupHomePage()
.assertThatContainsText("DevOps");
}

Now run your test again, and you should have a few seconds to open the VNC window after the browser has been created and before the test starts to execute.

The method waitSecondsOf has been predefined in our starter kit. You can have a look on how it is done in the file src/main/java/cz/pumpitup/pn5/starterkit/Firefox.java. Usually this is not the preferred solution because you want your test to be ready for being executed in automated pipelines where watching the test is not expected and delays will not be welcomed .

Troubleshooting

  • The IDE should auto-detect that this is a Maven project and propose to load it. Alternatively load it manually. This should be feasible by right-clicking on the file pom.xml located at the root of the project.
  • In case your IDE displays a lot of red code and says that classes were not found, compile the project a first time. In the IDE this can be done by opening the Maven toolbox and executing the goal compile. Using the command line you can just run mvn compile. This will force to download all dependencies and your IDE will then recognise all classes.
  • The first time you try to run any maven phase, your IDE might say that JDK is not configured. In that case in the project settings configure any JDK with version 11+ and try again.