Screenshot of Failed Test Cases in Selenium WebDriver

HOME

In the previous tutorials, I have explained how we can take screenshots in Selenium using FileUtils or FileHandler. In this tutorial, I will explain how to capture screenshots of Failed Test Cases in Selenium.

We are going to use TestNG to capture screenshot of failed test cases.

We will be using below mentioned features of TestNG

1) ITestResult – This  Interface will provide us the result of test case execution. @AfterMethod method can declare a parameter of type ITestResult, which will reflect the result of the test method that was just run.

2) @AfterMethod – The annotated method will be run after each test method. Any @AfterMethod can declare a parameter of type java.lang.reflect.Method. This parameter will receive the test method that will be called once this after the method as run.

3) result.getName() – will return name of test case so that screenshot name will be same as test case name

4) @BeforeTest – The annotated method will be run before any test method belonging to the classes inside the tag is run.

5) @AfterTest – The annotated method will be run after all the test methods belonging to the classes inside the tag have run.

We are executing 2 test cases. One of the Test Case will pass and another will fail. This program will only capture the screenshot of failed test case, not the passed one as we have used condition

if (ITestResult.FAILURE == result.getStatus())

Let see this as a program. 

import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
 
public class ScreenshotFailedCases {
    static WebDriver driver;
 
    @BeforeTest
    public static void init() {
        System.setProperty("webdriver.gecko.driver", "src\\test\\resources\\webdrivers\\window\\geckodriver.exe");
 
        // Initiate Firefox browser
        driver = new FirefoxDriver();
 
        // Maximize the browser
        driver.manage().window().maximize();
 
        // Pass application url
        driver.get("https://duckduckgo.com/");
        System.out.println("BeforeTest");
    }
 
    @Test
    public void captureCorrectScreenMethod() throws Exception {
        String Text = driver.findElement(By.xpath("//*[@id='logo_homepage_link']")).getText();
        // Verify the text on the landing page
        Assert.assertTrue(Text.contains("About DuckDuckGo"));
    }
 
    @Test
    public void captureIncorrectScreenMethod() throws Exception {
                        
		// Fail test by using incorrect XPath to find the search box
        driver.findElement(By.xpath("//*[@name='qe']")).sendKeys("agile");
    }
 
    @AfterTest
    public static void exit() {
                        
		// Close the WebPage
        driver.quit();
    }
 
    // AfterMethod annotation - This method executes after every test execution
    @AfterMethod
    public void screenShot(ITestResult result) {
 
        // ITestResult.FAILURE is equals to result.getStatus then it enter into
        // if condition
                        
	if (ITestResult.FAILURE == result.getStatus()) {
            try {
                    
		         // To create reference of TakesScreenshot
                 TakesScreenshot screenshot = (TakesScreenshot) driver;
 
                 // Call method to capture screenshot
                 File src = screenshot.getScreenshotAs(OutputType.FILE);
 
                 // Copy files to specific location result.getName() will 
                 // return  name of test case so that screenshot name will be same as test case name
                    
		   FileUtils.copyFile(src, new File("./Screenshots/" + result.getName() + System.currentTimeMillis() + ".png"));
                    System.out.println("Successfully captured a screenshot");
                } catch (Exception e) {
                    System.out.println("Exception while taking screenshot " + e.getMessage());
           }
        }
    }
}

A folder with name Screenshots is created and the screenshot is placed in that folder as you can see the image below

Execution Status as shown below

TestNG Report – Go to test-output folder and open emailable-report.html

TestNG – How to group Tests in Selenium using TestNG

HOME

<?xml version = "1.0"encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name = "Suite1">
  <test name = "Test Demo">
      <groups>
         <run>
              <include name = "e2etest"/>
          </run>
        </groups>
    <classes>
          <class name = "TestNGGroupDemo"/>
     </classes>  
   </test>
</suite>

In below example, we have shown the syntax of how to use groups in the XML file. @Test(groups = { “e2etest”, “integerationtest” }) .

Below is the sample code.

import org.testng.annotations.Test;

public class TestNGGroupDemo {

	@Test(alwaysRun = true, groups = { "e2etest", "integerationtest" })
	public void testPrintMessage() {
		System.out.println("This method is run by both e2e and integeration test");
	}

	@Test(alwaysRun = true, groups = { "e2etest" })
	public void testE2EMessage() {
		System.out.println("This method is run by e2e test");
	}

	@Test(alwaysRun = true, groups = { "integerationtest" })
	public void testingIntegrationMessage() {
		System.out.println("This method is run by integeration test");
	}

	@Test(alwaysRun = true, groups = { "acceptancetest" })
	public void testingAcceptanceMessage() {
		System.out.println("This method is run by Acceptance test");
	}

	@Test(alwaysRun = true, groups = { "e2etest", "acceptancetest" })
	public void testE2EAndAcceptanceMessage() {
		System.out.println("This method is run by both e2e and acceptance test");
	}

	@Test(alwaysRun = true, groups = { "e2etest", "integerationtest", "acceptancetest" })
	public void testE2EAndAcceptanceAndIntegrationMessage() {
		System.out.println("This method is run by e2e, integration and acceptance test");
	}

}

Output
[RemoteTestNG] detected TestNG version 7.4.0
This method is run by e2e, integration and acceptance test
This method is run by both e2e and acceptance test
This method is run by e2e test
This method is run by both e2e and integeration test

===============================================
Suite
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================

The output of the above program is

The result will look like something shown below. Here, we can see that Test Case Passed is 4, Failed 0 and Skipped 0.

To view the report, go to the Eclipse folder and you can see a folder with name test-output inside the Project where we have created TestNG class. Here, it is  C:\Users\vibha\eclipse-workspace\Demo\test-output

Groups of Groups

Groups can also include other groups. These groups are called “MetaGroups”. TestNG provides the flexibility of providing groups inside another group and running them according to your needs.
Let’s create a group inside a group in our XML file.

Here, I have created a created a new group with the name ‘SuperGroup‘ and included our group “acceptancetest” into it. Then we called the newly created group (SuperGroup) for execution by including it in the run tag. The output will be like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
   <test name = "Test Demo">
       <groups>
        	<define name = "SuperGroup">
   			   <include name = "acceptancetest"></include>
   		</define>
          <run>
              <include name = "SuperGroup"/>
          </run>
        </groups>
    <classes>
      <class name="TestNGGroupDemo"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

Lets, add another group “e2etest” to SuperGroup. In this case, we will see all the tests marked with “e2etest” and “acceptancetest“.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
   <test name = "Test Demo">
       <groups>
        	<define name = "SuperGroup">
   			   <include name = "acceptancetest"></include>
   			    <include name = "e2etest"></include>
   		</define>
          <run>
              <include name = "SuperGroup"/>
          </run>
        </groups>
    <classes>
      <class name="TestNGGroupDemo"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

How to disable Selenium Test Cases using TestNG Feature – @Ignore

HOME

The previous tutorial discussed prioritizing the Test Cases using TestNG. In this tutorial, we will see how we can disable the Test Cases using TestNG. 

Imagine there are 100 test cases in a Regression Test Suite. We need to execute 99 test cases in a release and do want not to execute any particular test case. But we do not want to delete that test case from the Test Suite also. In this case, TestNG has a feature that allows skipping a particular test case by setting the parameters to.

@Enabled Annotation

@Test(enabled = false)

To use two or more parameters in a single annotation, separate them with a comma:

@Test(priority = 3, enabled = false)

To Run the TestNG program, Right-click on the program, and select Run As TestNG Test.

Below is an example to implement the above-mentioned scenario.

import org.testng.annotations.Test;
 
public class TestNGDisableDemo {
 
    @Test(priority = 3)
     public static void FirstTest() {
           System.out.println("This is Test Case 1, but after priority Test Case 3");
     }
 
     @Test(priority = 4)
     public static void SecondTest() {
          System.out.println("This is Test Case 2, but after priority Test Case 4");
     }
 
     @Test(enabled = false)
     public static void ThirdTest() {
           System.out.println("This is Test Case 3, but now skipped");
     }
 
     @Test(priority = 1)
     public static void FourthTest() {
            System.out.println("This is Test Case 4, but after priority Test Case 1");
     }
}


The output of the above program is

@Ignore Annotation

There is another way of skipping the tests by using the @Ignore annotation.

TestNG lets you ignore all the @Test methods :

  • In a class (or)
  • In a particular package (or)
  • In a package and all of its child packages

Ignore a Test

Below is an example where we have skipped the execution of a particular test.

import org.testng.annotations.Ignore;
import org.testng.annotations.Test;

public class IgnoreDemo{
	
	@Test
	public void FirstTest() {
	
		System.out.println("This is Test Case 1");
    
	}
	
	@Test
	public void SecondTest() {
	
		System.out.println("This is Test Case 2");
    
	}
	
	@Ignore
	@Test
	public void ThirdTest() {
	
		System.out.println("This is Test Case 3");
    
	}
	
	@Test
	public void FourthTest() {
	
		System.out.println("This is Test Case 4");
    
	}

}

In the above example, we have assigned @Ignore to the third test. So, this test should be skipped while the execution.

The output of the above program is

Ignore a Class

To understand this concept, we need to create 2 test classes – IgnoreDemo and IgnoreDemo2. Imagine we want to ignore all the tests in the class, so we can use @Ignore at the class level.

IgnoreDemo

import org.testng.annotations.Ignore;
import org.testng.annotations.Test;

@Ignore
public class PriorityDemo {
	
	@Test
	public void FirstTest() {
	
		System.out.println("This is Test Case 1");
    
	}
	
	@Test
	public void SecondTest() {
	
		System.out.println("This is Test Case 2");
    
	}
	

	@Test
	public void ThirdTest() {
	
		System.out.println("This is Test Case 3");
    
	}
	
	@Test
	public void FourthTest() {
	
		System.out.println("This is Test Case 4");
    
	}

}

IgnoreDemo2

package com.example;

import org.testng.annotations.Ignore;
import org.testng.annotations.Test;


public class PriorityDemo2 {
	
	@Test
	public void FirstTest() {
	
		System.out.println("This is Test Case 1 of Class 2");
    
	}
	
	@Test
	public void SecondTest() {
	
		System.out.println("This is Test Case 2 of Class 2");
    
	}
	

	@Test
	public void ThirdTest() {
	
		System.out.println("This is Test Case 3 of Class 2");
    
	}
	
	@Test
	public void FourthTest() {
	
		System.out.println("This is Test Case 4 of Class 2");
    
	}

}

To run both the classes together, we need to create a testng.xml. The easiest way is to select both the classes and Right-Click and select TestNG -> Convert to TestNG.

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test thread-count="5" name="Test">
    <classes>
      <class name="com.example.PriorityDemo2"/>
      <class name="com.example.PriorityDemo"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

Right-click on the testng.xml and select Run As -> TestNG Suite.

As we have assigned the @Ignore annotation at the class level of PriorityDemo class, it will ignore all the 4 tests present in that particular class.

The output of the above program is

We are done. Congratulations on making it through this tutorial and hope you found it useful!

TestNG Framework – How to download and install TestNG in Eclipse

HOME

In the previous tutorial, we discussed what is TestNG and why it is important. This tutorial will discuss how can we download and install TestNG in Eclipse and how to use it.

Pre-Requisite 

1) Eclipse should be installed and configured. Please refer to Install and Configure to set up Eclipse on your system.

Install/Setup TestNG

1) Launch Eclipse and go to the “Help” option present at the top and select –“Install New Software”.

2) A dialog box will appear, click the Add button.

3) A new dialog box will appear. Mention the Name as TestNG and the location asTestNG P2 – https://testng.org/testng-p2-update-site&#8221; and click the Add button.

4) This time we will see TestNG is added to Install dialog box.

5) Accept the terms and conditions and then click the Finish button.

6) Once the installation is completed, you will get a message to Restart the Eclipse. Select Restart the Eclipse

7) To verify if TestNG is installed successfully or not, go to Window, select Show View, and then Other.

8) Select Java and see, within the Java folder, you will see TestNG. This shows that TestNG is successfully installed on the machine.

Steps to follow to create a TestNG class

1) Create a new TestNG class. Right-click on the Folder where you want to create the TestNG class. Select TestNG and then Create the TestNG class as shown in the below image.

2) In the below image, we can see that the Source folder is the name of the folder we want to create the class, and we can mention the name of the class in the Class name. Under annotations, I have checked @BeforeTest and @AfterTest and click the Finish button.

3) We can see that the structure of the new TestNG class looks like as shown below.

4) In the below example, we want to navigate to an Amazon page and search for Hard Drive.

@BeforeTest : Launch Firefox and direct it to the Base URL

@Test : Search for HardDrive

@AfterTest : Close Firefox browser

import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;

public class TestNG_Demo {

   public WebDriver driver;

   @BeforeTest
    public void beforeTest() {
      
     System.setProperty("webdriver.gecko.driver","C:\\Users\\vibha\\Downloads\\geckodriver-v0.26.0-win64\\geckodriver.exe");
    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    driver.get("https://www.amazon.com//");
 }

@Test
public void Validation() {
    driver.findElement(By.xpath("//*[@id='twotabsearchtextbox']")).sendKeys("hard drive");
    //XPath for search button
      driver.findElement(By.xpath("//*[@class='nav-input']")).click();
   }

@AfterTest
public void afterTest() {
    driver.quit();
  } 
}

5) To execute this program, we need to Right-click and select Run as – TestNG Test.

6) The result will look like something shown below. Here, we can see that Test Case Passed is 1, Failed 0, and Skipped 0.

7) As we know that TestNG also produce HTML Reports. To access the report, go to the Eclipse folder, and you can see a folder with name test-output inside the Project where we have created TestNG class. Here, it is  C:\Users\vibha\Downloads\eclipse-workspace\Demo

We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

TestNG Framework – Introduction to TestNG

HOME

Selenium Introduction

HOME

    WebDriver talks to a browser through a driver. Communication is two-way: WebDriver passes commands to the browser through the driver and receives information back via the same route. The driver is specific to the browser, such as ChromeDriver for Google’s Chrome/Chromium, GeckoDriver for Mozilla’s Firefox, etc. The driver runs on the same system as the browser.