简体   繁体   English

"有没有办法调用扩展 webdriver 类的 java 方法?"

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

I am working with the WebDriverManager java library.我正在使用 WebDriverManager java 库。 It provides a generic manager that can be parameterized to act as a specific manager (for Chrome, Firefox, etc.).它提供了一个通用管理器,可以参数化以充当特定管理器(对于 Chrome、Firefox 等)。 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.我将它与 Selenium Webdriver 和 Junit 5 的 @Parameterized 和 @ValueSource 注释一起使用,以便我可以在多个浏览器上运行我的测试。 I created an implementation as per the WebDriverManager documentation:我根据 WebDriverManager 文档创建了一个实现:

 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.根据@ValueSource 文档<\/a>,您不能使用此注释来传递某些类的实例。 This annotation can be used only to pass primitive values, strings, and CLASSES<\/em> (not objects).此注释只能用于传递原始值、字符串和CLASSES<\/em> (而不是对象)。 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.您不能使用它将实例传递给方法。

INSTEAD<\/em> , what you can do is use the @MethodSource API<\/a> to pass the name of the method that can provide (return) those instances for you (look for the paragraph talking about multiple arguments). INSTEAD<\/em> ,您可以做的是使用@MethodSource API<\/a>传递可以为您提供(返回)这些实例的方法的名称(查找讨论多个参数的段落)。 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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