JUnit is an opensource unit testing tool that helps to test units of code. It is mainly used for unit testing Java project, however it can be used with Selenium Webdriver to automate testing of Web applications. To be precise JUnit is a unit testing framework for java that helps to write test cases in a more structured and better format. Selenium and JUnit can be used independently of each other though combining both helps to write test cases in a more structured way. This article references automating application using JUnit and Selenium through a simple script.
We will be walking through the following sections:
·Downloading JUnit Jars
·Adding Jars to your Selenium project
·Incorporating JUnit annotations and method into your first selenium scripts
Right click on your created project and select properties:
Click on Java build path from the options:
oClick on the ‘Add external Jars’ button and add your downloaded JUnit Jar files and click ‘OK’ post that:
This adds the JUnit jar files to your selenium project. The major class file/source files that are commonly used in this JUnit Jar files are:
The above script opens the browser with https://www.lambdatest.com/ and clicks on ‘free sign up’ button to register. Post register, the script will check the URL it is redirected to in order to ensure successful registration. We have used two classes of JUnit one is Annotations class and the other Assertions.
The script consists of three sections:
ü @BeforeClass – This annotation runs the piece of code before starting any annotation in the class. As you can see, here we have opened the chrome browser before performing any action on it. The main actions are performed in the @Test annotation marked method.
ü @Test – This test method carries the functionality where the application is opened and registration process is carried out. To validate the result we have used assertion class where we are validating the success of the registration process using the context of current URL. This test annotation runs the piece of code after the @BeforeClass and @BeforeTest method and before the @AfterTest and @AfterClass method.
ü @AfterClass– This annotation tells JUnit to run the piece of code once all the test have been executed. This annotation method usually carry process of closing the browser post all action items have been performed.
Attaching video of the execution of the above script:
Testing Cloud application via JUnit and Selenium
Cloud testing means testing cloud-based applications that are hosted on cloud. Using selenium along with JUnit to test cloud services for web apps becomes the most obvious choice due to its capabilities to run script on multiple browsers and platforms with any language support one choose for. With the use of JUnit it provides a more structured way of writing your test that leads to better maintenance, quality and effectiveness as compared to without using any frameworks like JUnit . Using Maven and Jenkins with Selenium and JUnit helps make a more powerful, smoother and end to end automation experience for cloud-based application, thereby providing for robustness to the whole flow.
Closing my article with the below quote from Jim Hazen