JUnit Automation Testing With Selenium

Sadhvi Singh

Posted On: January 4, 2019

view count31751 Views

Read time9 Min Read

This article is a part of our Content Hub. For more in-depth resources, check out our content hub on JUnit Tutorial.

Cross browser testing as we know is a process where you test your website over multiple numbers of browser & browser versions running on different operating systems. This is done so to realize the cross browser compatibility of a website or a web app when accessed across a variety of browsers. If your web app isn’t cross browser compatible then not only would you be missing out on potential leads but your customer retention could also suffer. These browser differences could be anything from cross browser layout issues to cross browser compatible typography and these cross browser differences happen as each browser has a unique rendering engine, which is responsible for rendering web elements.

However, cross browser testing could be highly time-consuming if executed manually. Think about how many browser versions you would need to encounter from legacy to modern and the surplus variety of browsers available in the market. You also would have to consider specific browsers offered by specific mobile vending companies. There are ways you could fast track your manual cross browser testing effort and automation testing sits on top of the list for saving every day time and effort.

This article references automation testing with JUnit and Selenium for a web application through a simple script.

JUnit is an open source 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. So you can even perform automation testing of a web application with JUnit. 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, testing with JUnit and Selenium combined helps to write test cases in a more structured way. We will be walking through the following sections for Selenium automation testing with JUnit framework for automating test script for web application testing over an online Selenium grid:

  • Downloading JUnit Jars.
  • Adding Jars to your Selenium project.
  • Incorporating JUnit annotations and method into your first selenium scripts.
  • Cloud testing with JUnit and Selenium Using LambdaTest.

Step 1. Downloading JUnit Jars

JUnit jar files can be downloaded from https://github.com/JUnit-team/JUnit4/wiki/Download-and-Install. The major jar files included are:

  • junit.jar
  • hamcrest-core.jar

Download and save these files in your system.

Step 2. Adding Jars To Your Selenium Project

In order to add your JUnit external jar files into your project, you need to have an advanced code editor/compiler tool of your preference. I usually work with eclipse so I would be narrating using the same. You also need to have Selenium jar files downloaded. In order to install eclipse, you can refer to its official website. Based on your operating system windows or OS, you can download accordingly. Post Eclipse setup you can download your Selenium jar files from its official website . In order to create your Selenium webdriver scripts, you need to use user language-specific drivers. In our case, we are using Java, though Selenium grid supports multiple languages like C#, Ruby, Python, Javascript along with Java. Download the Selenium jar files and include them into your code editor/compiler workspace, now for automating test script for testing with JUnit and Selenium grid, we need to include our downloaded JUnit Jar files. Follow the below steps to do so:

  • Right click on your created project and select properties:JUnit
  • Click on Java build path from the options:Java-Build-Path
  • Click 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:

  • Assertions
  • Annotations
  • Parameterized
  • Ignore
  • TestListeners etc

For detail list on the class files or source files refer here. Now, let us incorporate JUnit into your Selenium project for proceeding web application testing with JUnit and Selenium.

This JUnit certification establishes testing standards for those who wish to advance their careers in Selenium automation testing with JUnit.

Here’s a short glimpse of the JUnit certification from LambdaTest:

Step 3. Incorporating JUnit To Your Selenium Script

The first block for building collaboration in this article, for testing with JUnit and Selenium for a web application would be to create our first JUnit Selenium simple script on https://www.lambdatest.com/.

Reference Code:

Details:

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 a 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 the 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 carries the process of closing the browser post all action items have been performed.
  • Attaching video of the execution of the above script:

    So far you have seen how to automate your web app testing with JUnit and Selenium using a local webdriver instance. However, there is a downside to that. You can only invoke the automated cross browser testing over the browsers that are installed in your machine. Plus, you need to have your machine every time that you aim for running automation testing with JUnit and Selenium for a web app. It is why testing on cloud is emerging as a dominant preference worldwide. LambdaTest is a cloud based cross browser testing tool that offers supports with Selenium grid, providing a solution to every obstacle you face while performing automation testing using your local machine. LambdaTest offers a Selenium grid consisting 2000+ browsers for you to perform automation testing effortlessly.

    Let me demonstrate how you could easily perform automation testing using LambdaTest.

    To run your test suite on our Selenium grid, you have to configure a couple of capabilities so that your tests execute on a remote browser. Refer to our Capability Generator Tool to help you fetch your desired capabilities at ease.

    Now let’s start with a simple Selenium WebDriver test. Below is a JUnit Selenium script that will open a sample to-do application which will do following task:

    1. Mark first two items as mark done.
    2. Add a new item in the list.
    3. Return the added item.

    You could also test your local files through LambdaTest Selenium grid for performing automation testing of a web application with JUnit and Selenium. You can do with the help of advanced capabilities provided by LambdaTest Capabilities Generator. These advanced capabilities will help you to:

    • Perform test on your locally hosted web pages.
    • Generate browser console logs for every step executed through your test.
    • Configure a custom time zone for executing your test.
    • Generate network logs through your test execution.

    If we want to activate all of the above-mentioned advanced capabilities then you need to add the below desired capabilities:

    Parallel Testing

    LambdaTest also provides the ability to perform parallel test execution. With LambdaTest parallel testing you could run a similar test across multiple browsers, simultaneously. This helps to bring down the time taken on test build activities, significantly. Here is an example of parallel testing performed to automate web application testing with JUnit and Selenium using LambdaTest.

    To run a test on different browsers at the same time, you will need to create a helper class that extends paramerterized class (org.junit.runners.Parameterized) and implements RunnerScheduler class

    (org.junit.runners.model.RunnerScheduler) to support JUnit tests in parallel. See the example below:

    Here is an example of JUnit Test, this would be representing the helper class used above for executing the parallel testing.

    Web Application Testing With JUnit and Selenium On Cloud Is A Must

    Cloud testing means testing cloud-based applications that are hosted on a cloud. Using Selenium along with JUnit to test cloud services for web apps becomes the most obvious choice due to its capabilities to run the script on multiple browsers and platforms with any language support one choose for. The reason makes the incorporation between JUnit and Selenium a lucrative choice for performing cross browser testing. 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. LambdaTest as a cross browser testing tool could help you significantly cut short on the time and effort you put in for executing automation testing with JUnit and Selenium. LambdaTest offers a Selenium grid consisting more than 2000+ browsers and also supports multiple programming languages such as Javascript, Python, Ruby, C# and PHP. 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. We will be demonstrating that in our upcoming articles, so stay tuned and happy testing!

Author Profile Author Profile Author Profile

Author’s Profile

Sadhvi Singh

Sadhvi Singh is a QA Manager. In 7 years of her professional journey, she has worked on multiple domains and testing techniques like Automation testing, Database testing, API testing, Manual testing, and Security testing. With multiple tools exposure added to her experience, she has skilled herself further through her two major accolades at International level through ISTQB with foundation and advanced levels.

Blogs: 12



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free