kazurayam / ExtentReportsExample

ExtentReports 5.0.8 Example with TestNG Listeners and Retry Analyzer
https://www.swtestacademy.com/extent-reports-in-selenium-with-testng/
0 stars 0 forks source link

tests.LoginTest need to share an instance of WebDriver with utils.listener.TestListener; an possible alternative implementation using ITestContext #1

Open kazurayam opened 5 months ago

kazurayam commented 5 months ago

utils.listener.TestListner#onTestFailure() is designed to take a screenshot of the browser on a failure detected by the @Test-annotated method of tests.LoginTest. TestListener need to have a reference to the instance of WebDriver which was created by the LoginTest class.

Here we find a technical issue. The Test class creates a WebDriver instance; The Test Listener need a reference to the WebDriver instance. Then how to share the WebDriver instance between the two? There could be multiple design options. The original utils.listener.TestListner#onTestFailure() gets access to the WebDriver instance by the following 2 lines of code:

    @Override
    public void onTestFailure(ITestResult iTestResult) {
        ...
        Object testClass = iTestResult.getInstance();
        WebDriver driver = ((BaseTest) testClass).getDriver();

OK. This certainly works. However I find two issues in the original code set.

Issue1 : utils.listener.TestListener extends tests.BaseTest, which is unnecessary

I think that the author possibly forgot to erase the inheritance.

Issue2 : Should use ITestContext

we should rather use TestNG ITestContextto share the WebDriver instance between the Test class and the TestListener. Here is a brief introduction how to utilize @ITestContext`

kazurayam commented 5 months ago

How can we share the WebDriver instance created by a Test class to a TestListener?

See the following articale to learn about ITestContext of TestNG.

https://www.softwaretestingo.com/itestcontext-testng/