Skip to content

Instantly share code, notes, and snippets.

@gururajhm
Created December 3, 2016 14:19
IExecutionListener
// ExecutionListener1
package com.Iexecutelistener;
import org.testng.IExecutionListener;
public class ExecutionListener1 implements IExecutionListener {
private long startTime;
@Override
public void onExecutionStart() {
startTime = System.currentTimeMillis();
System.out.println("Starting On execution test from ExecutionListener1 ");
}
@Override
public void onExecutionFinish() {
System.out.println("Starting On execution test has finished, took around " + (System.currentTimeMillis() - startTime) + "ms");
}
}
// ExecutionListener2
package com.Iexecutelistener;
import org.testng.IExecutionListener;
public class ExecutionListener2 implements IExecutionListener {
@Override
public void onExecutionStart() {
System.out.println("Starting On execution test from ExecutionListener2");
}
@Override
public void onExecutionFinish() {
System.out.println("Starting On execution test TestNG is finished");
}
}
Create a testNG test class TestClass, it has a @BeforeSuite, a test and an @AfterSuite method.
package com.Iexecutelistener;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
public class TestClass {
@BeforeSuite
public void beforeSuite() {
System.out.println("Am in Before Suite");
}
@Test
public void t() {
System.out.println("In Test Method t...");
}
}
Create XML file that calls these listeners like;
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<listeners>
<listener class-name="com.Iexecutelistener.ExecutionListener1" />
<listener class-name="com.Iexecutelistener.ExecutionListener2" />
</listeners>
<test name="Test">
<classes>
<class name="com.Iexecutelistener.TestClass" />
</classes>
</test>
</suite>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment