1 package vid.automation.reportportal;
3 import static org.apache.commons.beanutils.MethodUtils.invokeStaticMethod;
5 import org.apache.commons.proxy.ProxyFactory;
6 import org.apache.commons.proxy.factory.javassist.JavassistProxyFactory;
7 import org.apache.commons.proxy.invoker.NullInvoker;
8 import org.openqa.selenium.WebDriver;
9 import org.testng.IExecutionListener;
10 import org.testng.ISuite;
11 import org.testng.ISuiteListener;
12 import org.testng.ITestContext;
13 import org.testng.ITestResult;
14 import org.testng.internal.IResultListener2;
17 * Loads and delegates to ReportPortalListener. When class not found -- yields no-op object, and no side-effect.
19 public class ReportPortalListenerDelegator implements IExecutionListener, ISuiteListener, IResultListener2 {
21 private static final String CLASSNAME_REPORT_PORTAL_LISTENER = "com.att.automation.common.report_portal_integration.listeners.ReportPortalListener";
22 private static final String CLASSNAME_WEB_DRIVER_SCREENSHOTS_PROVIDER = "com.att.automation.common.report_portal_integration.screenshots.WebDriverScreenshotsProvider";
24 private static final Object instance = createReportPortalListener();
26 private final IExecutionListener iExecutionListener;
27 private final ISuiteListener iSuiteListener;
28 private final IResultListener2 iResultListener2;
30 public ReportPortalListenerDelegator() {
31 iExecutionListener = ((IExecutionListener) instance);
32 iSuiteListener = ((ISuiteListener) instance);
33 iResultListener2 = ((IResultListener2) instance);
36 public static void setScreenShotsWebDriver(WebDriver driver) {
38 invokeStaticMethod(instance.getClass(), "setScreenShotsProvider", createScreenshotsProvider(driver));
39 } catch (ClassNotFoundException e) {
40 // if class not found, don't bother
41 } catch (ReflectiveOperationException e) {
42 throw new RuntimeException(e);
48 public void beforeConfiguration(ITestResult tr) {
49 iResultListener2.beforeConfiguration(tr);
53 public void onConfigurationSuccess(ITestResult itr) {
54 iResultListener2.onConfigurationSuccess(itr);
58 public void onConfigurationFailure(ITestResult itr) {
59 iResultListener2.onConfigurationFailure(itr);
63 public void onConfigurationSkip(ITestResult itr) {
64 iResultListener2.onConfigurationSkip(itr);
68 public void onExecutionStart() {
69 iExecutionListener.onExecutionStart();
73 public void onExecutionFinish() {
74 iExecutionListener.onExecutionFinish();
78 public void onStart(ISuite suite) {
79 iSuiteListener.onStart(suite);
84 public void onFinish(ISuite suite) {
85 iSuiteListener.onFinish(suite);
89 public void onTestStart(ITestResult result) {
90 iResultListener2.onTestStart(result);
94 public void onTestSuccess(ITestResult result) {
95 iResultListener2.onTestSuccess(result);
99 public void onTestFailure(ITestResult result) {
100 iResultListener2.onTestFailure(result);
104 public void onTestSkipped(ITestResult result) {
105 iResultListener2.onTestSkipped(result);
109 public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
110 iResultListener2.onTestFailedButWithinSuccessPercentage(result);
114 public void onStart(ITestContext context) {
115 iResultListener2.onStart(context);
119 public void onFinish(ITestContext context) {
120 iResultListener2.onFinish(context);
124 private static Object createReportPortalListener() {
126 final Class<?> classToLoad = Class.forName(CLASSNAME_REPORT_PORTAL_LISTENER,
127 true, ReportPortalListenerDelegator.class.getClassLoader());
128 return classToLoad.getConstructor().newInstance();
129 } catch (ClassNotFoundException e) {
130 // Fallback to NullInvoker
131 final Class[] classes = {IExecutionListener.class, ISuiteListener.class, IResultListener2.class};
132 final ProxyFactory proxyFactory = new JavassistProxyFactory();
134 return proxyFactory.createInvokerProxy(new NullInvoker(), classes);
135 } catch (ReflectiveOperationException e) {
136 throw new RuntimeException(e);
140 private static Object createScreenshotsProvider(WebDriver driver) throws ReflectiveOperationException {
141 Class<?> classToLoad = Class.forName(CLASSNAME_WEB_DRIVER_SCREENSHOTS_PROVIDER,
142 true, driver.getClass().getClassLoader());
144 return classToLoad.getDeclaredConstructor(WebDriver.class).newInstance(driver);