The JUnit Touch

In this post we will focus on using a unit testing framework called JUnit which helps to organise and execute our test code efficiently.

WHAT IS JUNIT?

JUnit is a Java-based, open-source framework to help you execute unit testing. JUnit is mostly used to test unit or components of your application like classes and methods. JUnit also helps to run automated tests be it API or UI.

This post will help you use JUnit annotations in your Selenium framework. We would be looking at examples of JUnit annotations are that are used for a specific purpose in Selenium.

WHAT ARE JUNIT ANNOTATIONS?

JUnit annotations are meta-tags that provide data about the test methods and test classes outlined in our code structure. In order for our Selenium framework to execute in the most efficient manner we should add JUnit annotations in our script.

Below is the list of every JUnit annotation in Selenium that will be covered in this blog

@BeforeClass

@Before

@Test

@After

@AfterClass

@Ignore

*Note* – For our framework we shall be using JUnit 4.

In order to get started with JUnit 4 we need to add the dependency to our pom.xml as in the example below.

@BEFORECLASS

The @BeforeClass annotation is used to initialise objects that that could be used in our tests. When we use an object in the BeforeClass method, it would be invoked only once. The main usage of @BeforeClass annotation is used to execute code before all the tests listed in the test cases are executed.

In our example we are going to remove our public static void main method for the script. We will wrap the code for setting the property of the Chrome Driver inside of our @BeforeClass annotation. I will also add that we must make the method for using the @BeforeClass annotation static in order to use it. I will also print to the terminal window a simple statement to show the order of the test execution. This is done via the system.out.println() method.

@BEFORE

The @Before annotation is used when we want to initialise an object or perform an action for the duration of the test method . In our framework we are going to duplicate our tests 3 times to demonstrate the usage of @Before. As a result of having 3 test methods the @Before method will be called a total of 3 times, once for each test method.

What we shall do is to move the line of code relating to creating the driver object and maximising the browser window in the @Before annotation as for each test run we would like to ensure the browser is at its maximum screen size.

@TEST

The @Test annotation informs JUnit that the method that is tagged against can be run as a test A single test class may comprise numerous test methods in it tagged with @Test.

In our example we are going to duplicate the test using the nomenclature _capital letter to ensure the tests run in order for the purpose of our demo.

*Note* JUnit has the ability to add test script execution ordering. We shall explore this in later posts.

@AFTERTEST

The @After annotation is used to tear down pervious actions that we would have used in previous test setups i.e. the @Before method .Tear down methods can be used to erase the state of a pervious test environment.

In our example we are going to close down the browser after each test run using the close() method.

@AFTERCLASS

Our @AfterClass annotation method, is executed once. It will be executed after all tests have completed. The @AfterClass tag is used to clean up operations that we would have started in our @BeforeClass method.

In our example we shall use the quit() method of Selenium to shut down all Chrome Driver system processes.

*Note* The @AfterClass annotation should use a public static method in order to be used.

@IGNORE

The @Ignore annotation tells JUnit that the associated method to not be executed. In cases where we do not want to execute our methods we can temporarily place that block of code with the @Ignore annotation to avoid test case execution.

In our example we shall duplicate a 4th test case and tag it with @Ignore.

Now we shall execute the tests . On the BasicTest class file right click and select Run BasicTest.

Note in the terminal window we shall see all of our methods run in the following order.

From the test run we can clearly see the following JUnit execution runs:

@BeforeClass ran once.

@AfterClass ran once.

@Test ran 3 times

@Ignore has ignored the 4th test.

We can also observe how clean and concise our test class now looks with the use of JUnit.

Advertisement

A Basic Test Script In Java

We pick up right where we left off which is now to incorporate our first basic Selenium test script. This script will be a very simple script that will open a webpage. As we get further on into developing our tests, we will adopt best practices such as using an execution framework (J-Unit) assertions libraries (HamCrest) with the addition of using the modelling technique (Page Object Modelling) this will incorporate typically good programming practices such as DRY and SOLID.

In future blog posts we will also start to perform actions on the website, then put together an end to end framework. Let’s open our IDE IntelliJ and click on new project you should see the following window screen displaying below.

Ensure to select Maven as the option and click on the next button. There will be a further screen which will allow us to enter our project name. I’m going to call this project automationodyssey. You are free to call the project a meaningful name.

Once done click on the finish button you will see a project screen that will have the following folder structure .idea, src \main\java, src\main\resources, src\test\java. This is a typical Maven folder structure, this has been created for us by Maven. I will be going into further detail about Maven in a future blog post.

In order for us to start using Selenium we need to add Selenium-Java as a dependency to our project. Adding a dependency allows us to use Selenium-Java library. We do this by adding the Selenium XML to the pom.xml file. The pom.xml file will handle all other project dependencies we may need in future.

Handling our dependencies in this manner allows us to update versions and fixes to dependencies should they require change. When I first started out in test automation I downloaded .jar files and added these to the external libs folder.

This became an inefficient way as you downloaded a new .jar file for every version update, then delete your old .jar file. Rather now the site https://mvnrepository.com/ hosts the files and XML codings, it’s a small matter of making the changes direct to the pom.xml file and updating information such as the version number.

In the pom.xml file place the following line of code under the version tag as show below.

<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
</dependencies>

Now under the src\test\java folder right click under the new option select add Java class and name that class as BasicTest. A Java class is where we write and house our code. Typically in a Java class we house things such as fields, constructors, methods and nested classes. As we progress with our tests we will see these building blocks being housed in our Java classes.

Now in the Java file we are going to create our first field which will be the WebDriver field and assign it to a variable called ‘driver’ as shown below. *Note* if you see the class name WebDriver highlighted in red this is a warning to us to import the WebDriver class into our project file.

You can fix this issue simply by right clicking on the project name then in the Maven option reimporting the project. You will then need to head back over to the Java file and under the field placing the cursor over the declared WebDriver field and selecting the class to import as shown below.

In Java we need explicitly state the entry point for our program.We do this by adding the following line of code to our Java class file, public static void main (String [] args) { } . I won’t go into detail regarding that line of code, we can further explore this in another post. Inside of the curly braces of public static void main (String [] args) { } add the following lines of code as below.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class BasicTest {

   static WebDriver driver;

   public static void main (String[] args){
     
System.setProperty("webdriver.chrome.driver","src/main/resources/chrome-driver");
      driver = new ChromeDriver();
      driver.manage().window().maximize();
      driver.navigate().to("https://the-internet.herokuapp.com/login");
   }


}

Looking at the lines of code what we have done is to set the system property this allows Selenium to use ChromeDriver a standalone server which will let our code run against a Chrome browser. I will also add the ChromeDriver standalone server in the resources folder you will need to add the Chrome.exe file.

This file can be downloaded at the following site https://chromedriver.chromium.org/home. Ensure that you are downloading the version that matches your current Chrome browser version. The version can be located under the Chrome about option.

Now in the test file right click and select Run BasicTest() main to see Selenium in action.

If you have followed all of the steps correctly you would have seen a browser spin up and the following page displayed below. Congratulations you have successfully ran your first Selenium test script!

In the next post will further build on interacting with the page and using the runner framework J-Unit. Unit then take care and thanks for reading this post.