Lister concept in TestNG is very easy to understand and anyone can learn it doesn't required that you should be expert in it. in Simple words TestNG listners as the name say it 'Listen' something. it helps you to keep track of execution of script , It helps in generating logs and many more things.
There are 2 Type of Listener in TestNG :
1) TestNG listner (iTestResult,iRetryAnalyser,etc)
2) WebDriver listner(WebDriverEventListner)
Here, i will show you example of WebDriverEventListner.
To work with WebDriverEventListner. we need 2 things :-
1) class A with implement abstract method of WebDriverEventListener
2) class B which will register the class A's action
Lets see how we actually work.
This is my class A which implements the abstract of WebDriverEventListner
package com.Listners;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;
import com.baseclass.Baseclass;
import com.pageobject.Pageobjects;
public class Listner extends Baseclass implements WebDriverEventListener {
@Override
public void afterAlertAccept(WebDriver arg0) {
System.out.println("alert got----->" +arg0.switchTo().alert());
}
@Override
public void afterAlertDismiss(WebDriver arg0) {
}
@Override
public void afterChangeValueOf(WebElement arg0, WebDriver arg1, CharSequence[] arg2) {
}
@Override
public void afterClickOn(WebElement arg0, WebDriver arg1) {
System.out.println("clicked on---->"+arg0.getText().toString());
}
@Override
public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) {
}
@Override
public <X> void afterGetScreenshotAs(OutputType<X> arg0, X arg1) {
// TODO Auto-generated method stub
}
@Override
public void afterGetText(WebElement arg0, WebDriver arg1, String arg2) {
}
@Override
public void afterNavigateBack(WebDriver arg0) {
}
@Override
public void afterNavigateForward(WebDriver arg0) {
}
@Override
public void afterNavigateRefresh(WebDriver arg0) {
}
@Override
public void afterNavigateTo(String arg0, WebDriver arg1) {
System.out.println("navigated to--->"+arg1.getCurrentUrl());
}
@Override
public void afterScript(String arg0, WebDriver arg1) {
}
@Override
public void afterSwitchToWindow(String arg0, WebDriver arg1) {
System.out.println("switch to the window--->"+driver.switchTo().window(arg0));
}
@Override
public void beforeAlertAccept(WebDriver arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeAlertDismiss(WebDriver arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeChangeValueOf(WebElement arg0, WebDriver arg1, CharSequence[] arg2) {
// TODO Auto-generated method stub
}
@Override
public void beforeClickOn(WebElement arg0, WebDriver arg1) {
System.out.println("Trying to click on---->"+arg0.getText().toString());
}
@Override
public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) {
// TODO Auto-generated method stub
}
@Override
public <X> void beforeGetScreenshotAs(OutputType<X> arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeGetText(WebElement arg0, WebDriver arg1) {
// TODO Auto-generated method stub
}
@Override
public void beforeNavigateBack(WebDriver arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeNavigateForward(WebDriver arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeNavigateRefresh(WebDriver arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeNavigateTo(String arg0, WebDriver arg1) {
// TODO Auto-generated method stub
}
@Override
public void beforeScript(String arg0, WebDriver arg1) {
// TODO Auto-generated method stub
}
@Override
public void beforeSwitchToWindow(String arg0, WebDriver arg1) {
// TODO Auto-generated method stub
}
@Override
public void onException(Throwable arg0, WebDriver arg1) {
// TODO Auto-generated method stub
}
}
As you can see there are many methods available but for the practice purpose i have used only few
methods.
Now, we just need class which will use this class as registering actions.
package com.baseclass;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import com.Listners.Listner;
import io.github.bonigarcia.wdm.WebDriverManager;
public class Baseclass {
public static WebDriver driver;
public static EventFiringWebDriver e_driver;
public static void browsersetup()
{
WebDriverManager.chromedriver().setup();
driver=new ChromeDriver();
e_driver=new EventFiringWebDriver(driver);
Listner listner=new Listner();
e_driver.register(listner);
driver=e_driver;
driver.get("https://wordpress.com/log-in");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
}
}
Now we are done with the setup and can use this class for capturing the events
So, to use this i am showing you an example of wordress site. i will write script for creating one post and than delete it. and we will see how this listner will work.
package com.test;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import com.baseclass.Baseclass;
import com.pageobject.Pageobjects;
public class Login extends Baseclass {
public static Pageobjects pom;
@BeforeSuite
public void setupm()
{
browsersetup();
pom=new Pageobjects(driver);
}
@Test
public static void login(Pageobjects pom)
{
pom.username.sendKeys("xxxx@gmail.com");
pom.continue_button.click();
pom.password.sendKeys("xxxxx");
pom.signin_button.click();
}
}
Now, if we run this script we will see whatever action is performing in screen will be shown in console like this.