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

寵の児 提交于 2020-01-06 14:35:36

问题


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

回答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.




回答2:


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.



来源:https://stackoverflow.com/questions/50620820/selenium-type-mismatch-cannot-convert-from-classcustomlisteners-to-class-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!