Selenium-Maven-是否可以更改Selenium配置以检测浏览器版本并自动下载网络驱动程序



我必须在maven中自动执行测试用例运行确认。下载合适的Chrome或Firefox网络驱动程序也是其中的一部分

在构建标签下的pom.xml中

<plugin>
<groupId>com.github.webdriverextensions</groupId>
<artifactId>webdriverextensions-maven-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<goals>
<goal>install-drivers</goal>
</goals>
</execution>
</executions>
<configuration>
<installationDirectory>${testbench.driver.path}/drivers</installationDirectory>
<drivers>
<driver>
<name>geckodriver</name>
<platform>windows</platform>
<version>0.29.0</version>
</driver>
<driver>
<name>chromedriver</name>
<platform>windows</platform>
<version>90.0.4430.24</version>
</driver>
</drivers>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.17</version>
<executions>
<execution>
<id>failsafe-integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>failsafe-verify</id>
<phase>verify</phase>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<trimStackTrace>false</trimStackTrace>
<systemPropertyVariables>
<webdriver.chrome.driver>${testbench.driver.path}/drivers/chromedriver-windows-32bit.exe</webdriver.chrome.driver>
<webdriver.gecko.driver>${testbench.driver.path}/drivers/geckodriver-windows-64bit.exe</webdriver.gecko.driver>
</systemPropertyVariables>
</configuration>
</plugin>
<!-- The Jetty plugin allows us to easily test the development build by
running jetty:run on the command line. -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<configuration>
<scanIntervalSeconds>2</scanIntervalSeconds>
<stopKey>STOP</stopKey>
<stopPort>8005</stopPort>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>

在这里,我手动输入了要下载和安装的chrome驱动程序的版本。但我希望oorwser版本能在开发人员的机器中自动检测到,并自动下载合适的驱动程序。如果我没有提到驱动程序版本,我在构建过程中会出错。

这怎么能带来?我需要同时支持Chrome和Firefox网络驱动程序。

您可以使用WebDriver Manager API。-它将driver.exe下载到您的/m2/repository/webdriver文件夹中(使用Maven时(。它还自动检查您的浏览器版本并下载driver.exe的匹配版本。

要使用Webdriver Manager API,在pom.xml文件中包括以下行:

<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.6.2</version>
<scope>test</scope>
</dependency>

-然后在您的代码中,只需在初始化Webdriver对象之前调用此行:

//Call any of the line based on which browser you are using.
WebDriverManager.chromedriver().setup();
WebDriverManager.firefoxdriver().setup();
WebDriverManager.iedriver().setup();
WebDriverManager.operadriver().setup();
WebDriverManager.phantomjs().setup();
WebDriverManager.edgedriver().setup();

因此,您可以取消以下步骤:

1( 手动下载驱动程序二进制文件并放置在位置

2( 以下代码行也需要删除:

System.setProperty("webdriver.chrome.driver", "path to your driver location");

更多信息点击这里

添加最新答案。

从Selenium 4.6.0版本开始,没有必要使用第三个WebDriverManager。相反,SeleniumManager类内置了对相同功能的支持。Selenium manager支持所有浏览器,包括Chrome、Edge和Firefox。无需手动下载任何驱动程序,因为它将从SeleniumManager自动处理。

实施步骤:

将POM文件中的Selenium升级至4.6.0。

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java ->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.6.0</version>
</dependency>

chrome的代码实现示例如下,不需要添加任何额外的代码行来处理它,类似地,其他浏览器也会实现[目前处于测试版]。有关详细信息,请参阅链接。

WebDriver driver = new ChromeDriver();
driver.get("https://someurl");

最新更新