[Selenium+Java] Listeners and their use in Selenium WebDriver

Original URL: https://www.guru99.com/listeners-selenium-webdriver.html

TestNG Listeners in Selenium WebDriver

There are two main listeners.

  1. WebDriver Listeners
  2. TestNG Listeners

In this tutorial, we will discuss on Testng Listeners. Here is what you will learn-

What is Listeners in Selenium WebDriver?

Listener is defined as interface that modifes the default TestNG's behavior. As the name suggests Listeners "listen" to the event defined in the selenium script and behave accordingly. It is used in selenium by implementing Listeners Interface. It allows customizing TestNG reports or logs. There are many types of TestNG listeners available.

TestNG Listeners in Selenium WebDriver

Types of Listeners in TestNG

There are many types of listeners which allows you to change the TestNG's behavior.

Below are the few TestNG listeners:

  1. IAnnotationTransformer ,
  2. IAnnotationTransformer2 ,
  3. IConfigurable ,
  4. IConfigurationListener ,
  5. IExecutionListener,
  6. IHookable ,
  7. IInvokedMethodListener ,
  8. IInvokedMethodListener2 ,
  9. IMethodInterceptor ,
  10. IReporter,
  11. ISuiteListener,
  12. ITestListener .

Above Interface are called TestNG Listeners. These interfaces are used in selenium to generate logs or customize the Testing reports.

In this tutorial, we will implement the ITestListener.

ITestListener has following methods

  • OnStart- OnStart method is called when any Test starts.
  • onTestSuccess- onTestSuccess method is called on the success of any Test.
  • onTestFailure- onTestFailure method is called on the failure of any Test.
  • onTestSkipped- onTestSkipped method is called on skipped of any Test.
  • onTestFailedButWithinSuccessPercentage- method is called each time Test fails but is within success percentage.
  • onFinish- onFinish method is called after all Tests are executed.

Test Scenario:

In this test scenario, we will automate Login process and implement the 'ItestListener'.

  1. Launch the Firefox and open the site "http://demo.guru99.com/V4/ "

TestNG Listeners in Selenium WebDriver

  1. Login to the application.

TestNG Listeners in Selenium WebDriver

Steps to create a TestNG Listener

For the above test scenario, we will implement Listener.

Step 1) Create class "Listener_Demo" and implements 'ITestListener '. Move the mouse over redline text, and Eclipse will suggest you 2 quick fixes as shown in below screen:

TestNG Listeners in Selenium WebDriver

Just click on "Add unimplemented methods". Multiple unimplemented methods (without a body) is added to the code. Check below-

package Listener_Demo;		

import org.testng.ITestContext ;		
import org.testng.ITestListener ;		
import org.testng.ITestResult ;		

public class ListenerTest implements ITestListener						
{		

    @Override		
    public void onFinish(ITestContext arg0) {					
        // TODO Auto-generated method stub				
        		
    }		

    @Override		
    public void onStart(ITestContext arg0) {					
        // TODO Auto-generated method stub				
        		
    }		

    @Override		
    public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {					
        // TODO Auto-generated method stub				
        		
    }		

    @Override		
    public void onTestFailure(ITestResult arg0) {					
        // TODO Auto-generated method stub				
        		
    }		

    @Override		
    public void onTestSkipped(ITestResult arg0) {					
        // TODO Auto-generated method stub				
        		
    }		

    @Override		
    public void onTestStart(ITestResult arg0) {					
        // TODO Auto-generated method stub				
        		
    }		

    @Override		
    public void onTestSuccess(ITestResult arg0) {					
        // TODO Auto-generated method stub				
        		
    }		
}		

Let's modify the 'ListenerTest' class. In particular, we will modify following methods-

onTestFailure, onTestSkipped, onTestStart, onTestSuccess, etc.

The modification is simple. We just print the name of the Test.

Logs are created in the console. It is easy for the user to understand which test is a pass, fail, and skip status.

After modification, the code looks like-

package Listener_Demo;		

import org.testng.ITestContext;		
import org.testng.ITestListener;		
import org.testng.ITestResult;		

public class ListenerTest implements ITestListener						
{		

    @Override		
    public void onFinish(ITestContext Result) 					
    {		
                		
    }		

    @Override		
    public void onStart(ITestContext Result)					
    {		
            		
    }		

    @Override		
    public void onTestFailedButWithinSuccessPercentage(ITestResult Result)					
    {		
    		
    }		

    // When Test case get failed, this method is called.		
    @Override		
    public void onTestFailure(ITestResult Result) 					
    {		
    System.out.println("The name of the testcase failed is :"+Result.getName());					
    }		

    // When Test case get Skipped, this method is called.		
    @Override		
    public void onTestSkipped(ITestResult Result)					
    {		
    System.out.println("The name of the testcase Skipped is :"+Result.getName());					
    }		

    // When Test case get Started, this method is called.		
    @Override		
    public void onTestStart(ITestResult Result)					
    {		
    System.out.println(Result.getName()+" test case started");					
    }		

    // When Test case get passed, this method is called.		
    @Override		
    public void onTestSuccess(ITestResult Result)					
    {		
    System.out.println("The name of the testcase passed is :"+Result.getName());					
    }		

}			

Step 2) Create another class "TestCases" for the login process automation. Selenium will execute this 'TestCases' to login automatically.

package Listener_Demo;		

import org.openqa.selenium.By;		
import org.openqa.selenium.WebDriver;		
import org.openqa.selenium.firefox.FirefoxDriver;		
import org.testng.Assert;		
import org.testng.annotations.Listeners;		
Import org.testng.annotations.Test;		

public class TestCases {				
WebDriver driver= new FirefoxDriver();					

// Test to pass as to verify listeners .		
@Test		
public void Login()				
{		
    driver.get("http://demo.guru99.com/V4/");					
    driver.findElement(By.name("uid")).sendKeys("mngr34926");							
    driver.findElement(By.name("password")).sendKeys("amUpenu");							
    driver.findElement(By.name("btnLogin")).click();					
}		

// Forcefully failed this test as to verify listener.		
@Test		
public void TestToFail()				
{		
    System.out.println("This method to test fail");					
    Assert.assertTrue(false);			
}		
}

Step 3) Next, implement this listener in our regular project class i.e. "TestCases". There are two different ways to connect to the class and interface.

The first way is to use Listeners annotation (@Listeners) as shown below:

@Listeners(Listener_Demo.ListenerTest.class)				

We use this in the class "TestCases" as shown below.

So Finally the class " TestCases " looks like after using Listener annotation:

package Listener_Demo;		

import org.openqa.selenium.By;		
import org.openqa.selenium.WebDriver;		
import org.openqa.selenium.firefox.FirefoxDriver;		
import org.testng.Assert;		
import org.testng.annotations.Listeners;		
import org.testng.annotations.Test;             		

@Listeners(Listener_Demo.ListenerTest.class)			

public class TestCases {				
WebDriver driver= new FirefoxDriver();					

//Test to pass as to verify listeners.		
@Test		
public void Login()				
{		
    driver.get("http://demo.guru99.com/V4/");					
    driver.findElement(By.name("uid")).sendKeys("mngr34926");							
    driver.findElement(By.name("password")).sendKeys("amUpenu");							
    driver.findElement(By.id("")).click();					
}		

//Forcefully failed this test as verify listener.		
@Test		
public void TestToFail()				
{		
    System.out.println("This method to test fail");					
    Assert.assertTrue(false);			
}		
}			

The project structure looks like:

TestNG Listeners in Selenium WebDriver

Step 4): Execute the "TestCases " class. Methods in class "ListenerTest " are called automatically according to the behavior of methods annotated as @Test.

Step 5): Verify the Output that logs displays at the console.

Output of the 'TestCases' will look like this:

TestNG Listeners in Selenium WebDriver

[TestNG] Running:

C:\Users\gauravn\AppData\Local\Temp\testng-eclipse--1058076918\testng-customsuite.xml

Login Test Case started

The name of the testcase passed is:Login

TestToFail test case started

This method to test fail

The name of the testcase failed is:TestToFail

PASSED: Login

FAILED: TestToFail

java.lang.AssertionError: expected [true] but found [false]

Use of Listener for multiple classes.

If project has multiple classes adding Listeners to each one of them could be cumbersome and error prone.

In such cases, we can create a testng.xml and add listeners tag in XML.

TestNG Listeners in Selenium WebDriver

This listener is implemented throughout the test suite irrespective of the number of classes you have. When you run this XML file, listeners will work on all classes mentioned. You can also declare any number of listener class.

Summary:

Listeners are required to generate logs or customize TestNG reports in Selenium Webdriver.

  • There are many types of listeners and can be used as per requirements.
  • Listeners are interfaces used in selenium web driver script
  • Demonstrated the use of Listener in Selenium
  • Implemented the Listeners for multiple classes
posted on 2018-05-28 10:10  alicegu  阅读(219)  评论(0编辑  收藏  举报