0

I am using TestNG framework. I have a Test1 class which extends BaseTestSuite. Also I have a CustomListeners class which implements WebDriverEventListener. When I use @Listeners(CustomListeners.class) annotation in the Test class, getting following error. Please help to resolve.

Type mismatch: cannot convert from Class<CustomListeners> to Class<? extends ITestNGListener>[]

Test Class

@Listeners(CustomListeners.class) //Error line
public class Test1 extends BaseTestSuite {

    LoginPage lp;
    TabMenu tm;

    @Test(priority = 0, testName = "Verify Login")
    public void login() throws Exception {
        lp = new LoginPage(driver, test);
        tm = new TabMenu(driver, test);
        driver.get(Constants.url);
        lp.verifyLoginPageLogo();
        lp.setUserName("dmin");
        lp.setPassword("admin");
        lp.clickLoginBtn();
        tm.verifyTabMenu();
        tm.isCurrentTab("Dashboard");
    }
}

Listeners Class

public class CustomListeners implements WebDriverEventListener {
/*
         * 
         * All Implemented methods
         * 
         * 
         */
}

2 Answers 2

1

Selenium is not part of TestNG. The WebDriverEventListener interface doesn't extend ITestNGListener. The two classes have nothing in common. TestNG requires a class that extends or implements one of the listeners for this to work. To combine this two classes to work make your class CustomListeners extend the org.testng.TestListenerAdapter class, because it's the only listener class, and not an interface so you would not need to implement TestNG methods.

This should be your class declaration:

 public class CustomListeners extends TestListenerAdapter implements WebDriverEventListener

Try this workaround.

1
  • 1
    The IDE is not smart enough to check if you are sending the correct class to @Listeners annotation, it can check only that you are actually sending class. Commented May 31, 2018 at 11:13
1

Like Kristijan Rusu wrote in his answer, the WebDriverEventListener interface and ITestNGListener Interface are meant for two separate purposes.

If you have written a class that implements WebDriverEventListener all you need to add this listener class to your driver object is create a new EventFiringWebDriver object and pass your driver object to its constructor

EventFiringWebDriver eventDriver = new EventFiringWebDriver(driver);

after this it business as usual, eventDriver supports all the methods that your driver object supported.

Where as,

ITestNGListener has multiple sub interfaces such as IAnnotationTransformer, IAnnotationTransformer2, IConfigurable, IConfigurationListener, IConfigurationListener2, IExecutionListener, IHookable, IInvokedMethodListener, IInvokedMethodListener2, IMethodInterceptor, IReporter, ISuiteListener, ITestListener. These lets you write hooks for TestNG related events such as onTestFailure, onStart etc.

3
  • I tried the workaround public class CustomListeners extends TestListenerAdapter implements WebDriverEventListener, error disappears but none of my listeners work. Eg. beforeFindBy. A simple print statement is written inside the beforeFindBy method. Commented Jun 1, 2018 at 6:25
  • 1
    toolsqa.com/selenium-webdriver/event-listener follow this tutorial
    – GPT14
    Commented Jun 1, 2018 at 6:41
  • Thanks. I just found that I missed to create an object of CustomListerners class and register in the driver. Now it works. Commented Jun 1, 2018 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.