• Selenium Video Tutorials

Selenium - JUnit Report



Junit can be used to create a detailed automation test report. It is an open source framework which can be integrated with Selenium tests and used for reporting purposes.

Prerequisites to Create JUnit Report

Steps to Create JUnit Report

Step 1 − Create a maven project and add the proper dependencies to the pom.xml file for the below items −

Step 2 − Create a JUnit test class with the implementation of the below example where we will first click on the New User button verify the text Welcome, Login In on the Welcome Page.

Selenium JUnit Report 1

On clicking the New User button, we will be navigating to the Registration page, having the Back to Login button as highlighted in the below image.

Selenium JUnit Report 2

Code Implementation

package Report;

import org.junit.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.TestMethodOrder;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class JunitTest {

   WebDriver driver;

   @Before
   public void setup() throws Exception{

      // Initiate browser driver
      driver = new ChromeDriver();

      // adding implicit wait of 20 secs
      driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

      // Opening the webpage
      driver.get("https://www.tutorialspoint.com/selenium/practice/login.php");
   }

   @Test
   @Order(1)
   public void verifyLoginAndRegisterPage() {
   
      // identify header then get text
      WebElement header = driver.findElement
         (By.xpath("//*[@id='signInForm']/h1"));
      String text = header.getText();

      // assertions to test case to check login page
      assertEquals("Welcome, Login In", text);

      // navigate to register page
      WebElement btn = driver.findElement
         (By.xpath("//*[@id='signInForm']/div[3]/a"));
      btn.click();

      // assertions added to test case to check register page
      WebElement btnchk = driver.findElement
         (By.xpath("//*[@id='signupForm']/div[5]/a"));
      boolean displayed = btnchk.isDisplayed();

      // assertions to test case
      assertEquals(true, displayed);
   }

   @After
   public void teardown() {
   
      // quitting browser
      driver.quit();
   }
}

Dependencies in pom.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   
   <modelVersion>4.0.0</modelVersion>
   <groupId>org.example</groupId>
   <artifactId>SeleniumJava</artifactId>
   <version>1.0-SNAPSHOT</version>

   <properties>
      <maven.compiler.source>16</maven.compiler.source>
      <maven.compiler.target>16</maven.compiler.target>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   
   <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
   <dependencies>
      <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-java</artifactId>
         <version>4.11.0</version>
      </dependency>
      
      <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
      <dependency>
         <groupId>org.junit.jupiter</groupId>
         <artifactId>junit-jupiter-api</artifactId>
         <version>5.10.2</version>
      </dependency>
      
      <!-- https://mvnrepository.com/artifact/junit/junit -->
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.13.2</version>
         <scope>test</scope>
      </dependency>

      <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-site-plugin -->
      <dependency>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-site-plugin</artifactId>
         <version>4.0.0-M13</version>
      </dependency>
   </dependencies>

   <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-report-plugin -->
   <reporting>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-report-plugin</artifactId>
            <version>3.2.5</version>
         </plugin>
      </plugins>
   </reporting>
</project>

Step 3 − Run the test from the command line with the command: mvn clean test suite.

Step 4 − Refresh the project and a new folder called the site should get generated within the target folder.

Selenium JUnit Report 3

Step 5 − Right-click on the surefire-report.html and select the option to open in a browser.

Selenium JUnit Report 4

The JUnit report will be opened in the browser showing the Summary with total number of test methods as 1, with a pass success percentage of 100. It also showed details of the Package List(name of the package, number of tests, passed counts, failed counts, pass success percentage, duration of tests and so on. Also, the test method name verifyLoginAndRegisterPage is also included in the report.

Conclusion

This concludes our comprehensive take on the tutorial on Selenium JUnit Report. We’ve started with describing a JUnit report, prerequisites to set up an JUnit report, and walked through steps to create a JUnit report with an example illustrating how to use it along with Selenium. This equips you with in-depth knowledge of the JUnit. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.

Advertisements