一直以来使用selenium做UI自动化测试,浏览器驱动版本与浏览器版本适配问题都是很头疼的,需要手动配置,特别麻烦。selenium官方为了解决该文件,在4.6.0版本以后不需要手动配置浏览器驱动了,只需要实例浏览器,selenium会自动根据用户浏览器版本找出最适合的版本并下载然后自动配置。
在4.6.0版本以前是需要配置浏览器驱动的,若未配置则会报错:
Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://chromedriver.chromium.org/. The latest version can be downloaded from https://chromedriver.chromium.org/downloads
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestDriver {
public static void main(String[] args) {
//配置浏览器驱动
System.setProperty("webdriver.chrome.driver","/path/webdriver.chrome.driver");
WebDriver d = new ChromeDriver();
d.quit();
}
}
在4.6.0版本以后就不需要了,需要将selenium版本升级到4.6.0及以上,这样selenium就会自动根据系统预计浏览器版本以及类型自动下载并配置driver
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestDriver {
public static void main(String[] args) {
WebDriver d = new ChromeDriver();
d.quit();
}
}
但是,即便这样,这种方式也有弊端,容易受网络影响,听停容易报错,如下:
Exception in thread "main" org.openqa.selenium.remote.http.ConnectionFailedException: Unable to establish websocket connection to http://localhost:58433/devtools/browser/a5f7765b-694b-4065-88fc-f03c49d37ec7
下面是我提供的库,也是能自动下载driver并且配置的,开源地址项目:GitHub - jiawade/auto-webdriverx
请大家支持点赞哈,支持谷歌,火狐,Edge主流浏览器,同样也是能自动根据系统,浏览器版本下载最合适的driver,配置driver更加稳定
添加依赖
<dependency>
<groupId>io.github.jiawade</groupId>
<artifactId>auto-webdriver</artifactId>
<version>0.0.2</version>
</dependency>
使用例子:
import io.driver.manage.AutoWebdriver; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class TestDriver { public static void main(String[] args) { AutoWebdriver.configChromeDriver(); WebDriver d = new ChromeDriver(); d.quit(); } }