LambdaTest Community

Find answers, support, and inspiration from other users

Questions
LambdaTest Community
Answered

How can I perform automated testing with JUnit and Selenium for browser compatibility?

Can anyone please tell me how can i perform automated testing with JUnit and Selenium for browser compatibility.

Up Vote Down Vote 0 Votes
Posted 4 months ago

Answers


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 automating test script for web application testing with JUnit and 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/JU.... 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:

  • Click on Java build path from the options:

  • 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.

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/.

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.
Up Vote Down Vote 0 Votes
Posted 3 months ago