How to Run a Selenium Test with JUnit
This blog post will take you through a step-by-step guide demonstrating how to run a Selenium test with JUnit. JUnit has proven itself as a light-weight and “easy to start” testing framework that helps you to prepare, execute and verify your tests in fast and reliable manner.
When used with Selenium, JUnit provides tools to start and manage Selenium WebDriver. WebDriver is an interface that enables interacting with a web browser in a user-friendly manner, like clicking on buttons, entering text into fields, etc. To learn more about WebDriver you can refer to the official page. To learn how to integrate WebDriver into your Continuous Testing process check out this blog post “How to Automate Testing Using Selenium WebDriver, Jenkins and Allure“.
With JUnit, you can manage user sessions and assert expectations and conditions, which together allow you to run comprehensive UI tests with Selenium.
To create a test with the JUnit framework we will need the following:
- Maven as a build tool. To download, follow this link. Maven is a project management tool that helps you build your project and manage documentation and resources. In terms of this blog we will use Maven to compile the Java code, download libraries like JUnit and Selenium and run tests.
- Java version 1.8 or above. You can download it from the official site.
To make development easier, code will be developed with the usage of IntelliJ IDEA IDE. You can download it from the website. However, you can use any other IDE of your choice. Code snippets can be easily migrated to any platform.
Creating a Project in Intellij IDEA
To create a project in Intellij IDEA select: File -> New -> Project. Select maven. Click on Next.
In the next step, specify a unique group id (consider a group as a tree of folders for your project), an artifactId (project name) and version.
Now we have a created project with the following structure:
In the src/test/java/environment package there are 2 classes: the EnvironmentManager class is designed to initialize and shut down WebDriver, and the purpose of the RunEnvironment class is to keep the WebDriver instance and control access to it.
The package test/java/tests includes a single class DemoTest that includes a test method to verify the homepage of blazemeter.com.
pom.xml is the configuration file of the project and includes sections for declaring project dependencies. Selenium dependency:
Also, we need to add a JUnit dependency. However there is one additional point here: there are two major versions of JUnit - 3 or 4 (similar versions), and 5. Depending on the version, the way dependencies are loaded and tests are executed differs. Later on in the blog we will come back to this point but for now let’s take a closer look at how to initialize and shut down a web driver.
In this blog post we are going to run our demo test in the Chrome browser. In order to run a test in Chrome browser you need to download ChromeDriver executable from the official website.
Once downloaded, the ChromeDriver location (absolute path) should be added to your PATH environment variable. This can be done either programmatically or via OS tools. We will do it programmatically as shown in the code snippet below from test/java/environment/EnvironmentManager.
Note: if you are using a Unix-based OS system, the path to chromedriver will be different!
And to shut down a webdriver:
Once we are done with developing a code for WebDriver instance manipulation, we are ready to create our first test in JUnit.
To identify that a method is a test, the annotation @Test is used. To start WebDriver with Chrome before the tests start, the method startBrowser() was marked with the @Before annotation, which ensures this method is executed before each test. The method tearDown() annotated with @After will be executed after every test. The test will throw an AssertionError if the link to the homepage doesn’t equal to https://www.blazemeter.com/.
Now it’s time to define the correct dependencies and the way tests are executed for the different versions of JUnit.
For JUnit 4
You can find the complete code repository here.
Declare the dependency in pom.xml under <dependencies> tag:
To run a single test, execute a command via the command line:
‘mvn -Dtest=DemoTest test’.
To run all the tests in a batch, execute ‘mvn test’ in the command prompt. Please note that all the test classes you would like to run should be prefixed with *Test in this case, like the DemoTest class (if the class name had the name Demo, it would not be executed by maven).
You can see the results of the execution as a console output:
JUnit doesn’t have built-in reporting tool. If you want to view results of tests in a html report you need to add reporting section to your pom.xml
This will produce an xml report in your build output directory in the surefire-reports directory. The XML format is not user-friendly so instead you can execute your tests with the ‘mvn surefire-report:report’ command. This will run your tests and generate an HTML report in the site directory of your build output directory.
You can find the complete code repository here.
Instead of the JUnit 4 dependency, declare the following dependency in pom.xml under the <dependencies> tag:
To execute a JUnit5 test, the maven-surefire-plugin needs to be configured with the dependency to junit-jupiter-engine:
Also a few modifications need to be made to the test class:
To generate a html report as we did for Junit4, we need to add additional dependency to maven-surefire-plugin:
So, the complete plugin configuration will look like this:
Following this guide, you can create and execute tests in the JUnit framework with Selenium and maven. JUnit also is a good choice if you need to run parameterized tests, dynamic tests (whose behavior differs on different conditions) and make assumptions on conditions.
You can also skip all these steps and upload your functional test script (in java and yaml) to BlazeMeter, and easily run it.