Vaadin 24 TestBench启动时如何添加Chrome自定义配置参数?
解决Vaadin 24 TestBench中ChromeDriver自定义参数不生效的问题
问题分析
- 官方默认的
LocalDriver类初始化ChromeDriver时,会直接创建全新的ChromeOptions实例,并未合并@BrowserConfiguration返回的自定义配置,导致你添加的参数被完全忽略。 vaadin.launch-browser=false仅用于禁用Vaadin开发模式自动打开浏览器,和TestBench启动测试浏览器的逻辑无关,因此无法阻止浏览器提前启动。- 你之前的参数缺少
--前缀(如disable-software-rasterizer应改为--disable-software-rasterizer),这也是参数不生效的潜在原因。
解决方案
方案一:自定义DriverProvider(推荐)
通过实现自定义驱动提供者,正确合并@BrowserConfiguration传入的配置,替换TestBench默认的LocalDriver逻辑:
- 实现
DriverProvider接口,自定义ChromeDriver初始化逻辑:
import com.vaadin.testbench.parallel.BrowserUtil; import com.vaadin.testbench.parallel.DriverProvider; import com.vaadin.testbench.parallel.Parameters; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.remote.DesiredCapabilities; public class CustomChromeDriverProvider implements DriverProvider { @Override public ChromeDriver createDriver(DesiredCapabilities desiredCapabilities) { // 合并@BrowserConfiguration传入的自定义配置 ChromeOptions options; if (desiredCapabilities instanceof ChromeOptions) { options = (ChromeOptions) desiredCapabilities; } else { options = new ChromeOptions(); options.merge(desiredCapabilities); } // 保留TestBench默认的必要参数 options.addArguments("--test-type"); if (Parameters.isHeadless()) { options.addArguments("--headless=new"); } return new ChromeDriver(options); } @Override public boolean supports(DesiredCapabilities desiredCapabilities) { return BrowserUtil.isChrome(desiredCapabilities); } }
- 在测试类上指定使用自定义Provider,并修正参数前缀:
import com.vaadin.testbench.annotations.BrowserConfiguration; import com.vaadin.testbench.annotations.TestBenchConfiguration; import com.vaadin.testbench.elementsbase.ButtonElement; import com.vaadin.testbench.parallel.BrowserTest; import com.vaadin.testbench.parallel.BrowserTestBase; import org.junit.jupiter.api.BeforeEach; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.remote.DesiredCapabilities; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.List; @TestBenchConfiguration(driverProvider = CustomChromeDriverProvider.class) public class LoginTest extends BrowserTestBase { @BrowserConfiguration public List<DesiredCapabilities> init() { ChromeOptions chromeOptions = new ChromeOptions(); // 所有参数添加--前缀 chromeOptions.addArguments("--headless=new"); chromeOptions.addArguments("--window-size=1920,1080"); chromeOptions.addArguments("--remote-allow-origins=*"); chromeOptions.addArguments("--disable-gpu"); chromeOptions.addArguments("--disable-software-rasterizer"); chromeOptions.addArguments("--disable-infobars"); chromeOptions.addArguments("--disable-extensions"); chromeOptions.addArguments("--disable-dev-shm-usage"); return List.of(chromeOptions); } @BeforeEach public void setup() throws Exception { getDriver().get("http://localhost:8080/"); } @BrowserTest public void clickButton() { ButtonElement button = $(ButtonElement.class).first(); button.click(); assertEquals("Clicked", button.getText()); } }
方案二:通过系统属性传递参数
如果不想自定义Provider,可以直接通过JVM系统属性传递Chrome启动参数:
在测试启动命令中添加以下参数:
-Dcom.vaadin.testbench.Parameters.headless=true \ -Dchrome.args="--disable-software-rasterizer --remote-allow-origins=* --window-size=1920,1080 --disable-gpu --disable-infobars --disable-extensions --disable-dev-shm-usage"
此方式无需修改测试类代码,系统属性会被TestBench默认的驱动初始化逻辑读取并应用。
关键注意事项
- Chrome启动参数必须以
--开头,否则无法被浏览器识别。 - 优先使用
--headless=new而非旧版--headless,避免兼容性问题。 vaadin.launch-browser=false对TestBench的测试浏览器无效,无需配置。
内容的提问来源于stack exchange,提问作者Dario Zamuner
相关产品推荐
相关产品推荐

