简体   繁体   中英

Is there a way to call a java method that extends a webdriver class?

I am working with the WebDriverManager java library. It provides a generic manager that can be parameterized to act as a specific manager (for Chrome, Firefox, etc.). I am using it with Selenium Webdriver and Junit 5's @Parameterized and @ValueSource annotations so that I can run my tests on multiple browsers. I created an implementation as per the WebDriverManager documentation:

 public class WebTest {

 WebDriver driver = null;


 @ParameterizedTest
 @ValueSource(classes = { ChromeDriver.class, FirefoxDriver.class })
 public void navigateToUrl(String url, Class<? extends WebDriver> webDriverClass) { //WebDriver class gets extended
 driver = WebDriverManager.getInstance(webDriverClass).create();
 driver.manage().window().maximize();
 driver.get(url);

According to the @ValueSource documentation<\/a> , you cannot use this annotation to pass instances of some class. This annotation can be used only to pass primitive values, strings, and CLASSES<\/em> (not objects). You can use the classes to test if some method or some object is an instance of the parameter class. You cannot use it to pass instances to a method.

For example, I think you can do something like this:

@ParameterizedTest
@MethodSource("getWebDrivers")
public void navigateToUrl(String url, List<WebDriver> drivers) {
 driver = WebDriverManager.getInstance(webDriverClass).create();
 driver.manage().window().maximize();
 driver.get(url);
}


public Stream<Arguments> getWebDriversAndUrl() {
    List<WebDrivers> drivers = new ArrayList<>();
    ... // Here you need to instantiate your drivers (skip if you have them already instantiated and set as global variables.
    drivers.add(chromeDriver);
    drivers.add(firefoxDriver);
    return return Stream.of(
        arguments("url1", drivers),
        arguments("url2", drivers);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM