Sunday, 3 July 2022

TestNG Listeners in Selenium Webdriver with Example | onTestStart, onTestSuccess, onTestFailure etc

                  In this post, we will see TestNG Listeners in Selenium Webdriver with Example | ITestListener Interface Methods TestNG Example 


Program Code (Program1.java)

package mytest;


import org.testng.Assert;

import org.testng.SkipException;

import org.testng.annotations.Test;


public class Program1 {


@Test

public void AdmissionLogin()

{

System.out.println("Admission Login");//code for testcase

Assert.assertTrue(false);

}

@Test

public void AdmissionBrokenlinks()

{

System.out.println("Admission Broken links"); //code for testcase

throw new SkipException("msg");

}

}


Program Code (Program2.java):

package mytest;


import org.testng.Assert;

import org.testng.annotations.Test;


public class Program2 {

int var=1;

@Test(successPercentage=50,invocationCount=2)

public void PlacementLogin()

{

System.out.println("Placement Login");//code for testcase

if (var==1)

{

var++;

Assert.assertTrue(false);

}

else

{

Assert.assertTrue(true);

}

}

@Test

public void PlacementBrokenlinks()

{

System.out.println("Placement Broken links"); //code for testcase

}


}


Program Code (TestListener.java):

package mytest;


import org.testng.ITestContext;

import org.testng.ITestListener;

import org.testng.ITestResult;


public class TestListener implements ITestListener

{


@Override

public void onTestStart(ITestResult result) {

// TODO Auto-generated method stub

ITestListener.super.onTestStart(result);

}


@Override

public void onTestSuccess(ITestResult result) {

// TODO Auto-generated method stub

ITestListener.super.onTestSuccess(result);

}


@Override

public void onTestFailure(ITestResult result) {

// TODO Auto-generated method stub

ITestListener.super.onTestFailure(result);

}


@Override

public void onTestSkipped(ITestResult result) {

// TODO Auto-generated method stub

ITestListener.super.onTestSkipped(result);

}


@Override

public void onTestFailedButWithinSuccessPercentage(ITestResult result) {

// TODO Auto-generated method stub

ITestListener.super.onTestFailedButWithinSuccessPercentage(result);

}


@Override

public void onTestFailedWithTimeout(ITestResult result) {

// TODO Auto-generated method stub

ITestListener.super.onTestFailedWithTimeout(result);

}


@Override

public void onStart(ITestContext context) {

// TODO Auto-generated method stub

ITestListener.super.onStart(context);

}


@Override

public void onFinish(ITestContext context) {

// TODO Auto-generated method stub

ITestListener.super.onFinish(context);

System.out.println("Test Finish");

}


}


Program Code (testng.xml):

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">

<suite name="Suite">


<listeners>

<listener class-name="mytest.TestListener"></listener>


</listeners>


  <test name="Test">

    <classes>

      <class name="mytest.Program1"/>

      <class name="mytest.Program2"/>

    </classes>

  </test> 

</suite> 



Watch following video:


Watch on YouTube: https://www.youtube.com/watch?v=E_VjpmFJkhg

No comments:

Post a Comment