seleniumtestnglisteners

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


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
         * 
         * 
         */
}

Solution

  • 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.