aboutsummaryrefslogtreecommitdiffstats
path: root/vid-automation/src/main/java/vid/automation/reportportal/ReportPortalListenerDelegator.java
blob: 6daa6d604daca8ce9bca60a53aefebde3cb34ac8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package vid.automation.reportportal;

import static org.apache.commons.beanutils.MethodUtils.invokeStaticMethod;

import org.apache.commons.proxy.ProxyFactory;
import org.apache.commons.proxy.factory.javassist.JavassistProxyFactory;
import org.apache.commons.proxy.invoker.NullInvoker;
import org.openqa.selenium.WebDriver;
import org.testng.IExecutionListener;
import org.testng.ISuite;
import org.testng.ISuiteListener;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.internal.IResultListener2;

/**
 * Loads and delegates to ReportPortalListener. When class not found -- yields no-op object, and no side-effect.
 */
public class ReportPortalListenerDelegator implements IExecutionListener, ISuiteListener, IResultListener2 {

    private static final String CLASSNAME_REPORT_PORTAL_LISTENER = "com.att.automation.common.report_portal_integration.listeners.ReportPortalListener";
    private static final String CLASSNAME_WEB_DRIVER_SCREENSHOTS_PROVIDER = "com.att.automation.common.report_portal_integration.screenshots.WebDriverScreenshotsProvider";

    private static final Object instance = createReportPortalListener();

    private final IExecutionListener iExecutionListener;
    private final ISuiteListener iSuiteListener;
    private final IResultListener2 iResultListener2;

    public ReportPortalListenerDelegator() {
        iExecutionListener = ((IExecutionListener) instance);
        iSuiteListener = ((ISuiteListener) instance);
        iResultListener2 = ((IResultListener2) instance);
    }

    public static void setScreenShotsWebDriver(WebDriver driver) {
        try {
            invokeStaticMethod(instance.getClass(), "setScreenShotsProvider", createScreenshotsProvider(driver));
        } catch (ClassNotFoundException e) {
            // if class not found, don't bother
        } catch (ReflectiveOperationException e) {
            throw new RuntimeException(e);
        }
    }


    @Override
    public void beforeConfiguration(ITestResult tr) {
        iResultListener2.beforeConfiguration(tr);
    }

    @Override
    public void onConfigurationSuccess(ITestResult itr) {
        iResultListener2.onConfigurationSuccess(itr);
    }

    @Override
    public void onConfigurationFailure(ITestResult itr) {
        iResultListener2.onConfigurationFailure(itr);
    }

    @Override
    public void onConfigurationSkip(ITestResult itr) {
        iResultListener2.onConfigurationSkip(itr);
    }

    @Override
    public void onExecutionStart() {
        iExecutionListener.onExecutionStart();
    }

    @Override
    public void onExecutionFinish() {
        iExecutionListener.onExecutionFinish();
    }

    @Override
    public void onStart(ISuite suite) {
        iSuiteListener.onStart(suite);

    }

    @Override
    public void onFinish(ISuite suite) {
        iSuiteListener.onFinish(suite);
    }

    @Override
    public void onTestStart(ITestResult result) {
        iResultListener2.onTestStart(result);
    }

    @Override
    public void onTestSuccess(ITestResult result) {
        iResultListener2.onTestSuccess(result);
    }

    @Override
    public void onTestFailure(ITestResult result) {
        iResultListener2.onTestFailure(result);
    }

    @Override
    public void onTestSkipped(ITestResult result) {
        iResultListener2.onTestSkipped(result);
    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
        iResultListener2.onTestFailedButWithinSuccessPercentage(result);
    }

    @Override
    public void onStart(ITestContext context) {
        iResultListener2.onStart(context);
    }

    @Override
    public void onFinish(ITestContext context) {
        iResultListener2.onFinish(context);
    }


    private static Object createReportPortalListener() {
        try {
            final Class<?> classToLoad = Class.forName(CLASSNAME_REPORT_PORTAL_LISTENER,
                true, ReportPortalListenerDelegator.class.getClassLoader());
            return classToLoad.getConstructor().newInstance();
        } catch (ClassNotFoundException e) {
            // Fallback to NullInvoker
            final Class[] classes = {IExecutionListener.class, ISuiteListener.class, IResultListener2.class};
            final ProxyFactory proxyFactory = new JavassistProxyFactory();

            return proxyFactory.createInvokerProxy(new NullInvoker(), classes);
        } catch (ReflectiveOperationException e) {
            throw new RuntimeException(e);
        }
    }

    private static Object createScreenshotsProvider(WebDriver driver) throws ReflectiveOperationException {
        Class<?> classToLoad = Class.forName(CLASSNAME_WEB_DRIVER_SCREENSHOTS_PROVIDER,
            true, driver.getClass().getClassLoader());

        return classToLoad.getDeclaredConstructor(WebDriver.class).newInstance(driver);
    }

}