All Downloads are FREE. Search and download functionalities are using the official Maven repository.

listeners.TestListener Maven / Gradle / Ivy

Go to download

A OyoTestLibs jar for deployment to the Central Repository via OSSRH

The newest version!

package com.oyo.listeners;

import java.util.Set;

import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

import com.oyo.utils.Helper;

/**
 * @author manish.kumar
 *
 */
public class TestListener extends TestListenerAdapter {

	Helper helper = new Helper();

	@Override
	public void onTestSuccess(ITestResult result) {
		String testClassName = helper.getTestClassName(result.getInstanceName()).trim();
		System.out.println("Passed : " + testClassName + " : " + result.getName() + "() :- "
				+ result.getMethod().getDescription() + "\n");
	}

	@Override
	public void onTestFailure(ITestResult result) {
		String testClassName = helper.getTestClassName(result.getInstanceName()).trim();
		System.out.println("Failed : " + testClassName + " : " + result.getName() + "() :- "
				+ result.getMethod().getDescription() + "\n"+"\n");
		String testMethodName = result.getName().toString().trim();

		try {
			helper.takeScreenShot(testClassName, testMethodName);
			String alertMessage = "Tests Failed:\nTC : "+testClassName + "\nMethod: " + testMethodName+"\nReason: "+result.getMethod().getDescription();
			System.out.println(alertMessage);
			helper.alertUsers(alertMessage);
		} catch (Exception ex) {
			System.out.println("Problem with Screenshot or Alerting" + ": "+ex.getMessage());
		}
	}

	@Override
	public void onTestSkipped(ITestResult result) {

		String testClassName = helper.getTestClassName(result.getInstanceName()).trim();
		System.out.println("Skipped : " + testClassName + " : " + result.getName() + "() :- "
				+ result.getMethod().getDescription() + "\n");
	}

	@Override
	public void onFinish(ITestContext context) {
		Set failedTests = context.getFailedTests().getAllResults();
		for (ITestResult temp : failedTests) {
			ITestNGMethod method = temp.getMethod();
			if (context.getFailedTests().getResults(method).size() > 1) {
				failedTests.remove(temp);
			} else {
				if (context.getPassedTests().getResults(method).size() > 0) {
					failedTests.remove(temp);
				}
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy