Arquillian本身不直接支持Chrome的无头模式,但可以结合使用Selenium和Arquillian Drone来实现。
以下是一个可以使用Arquillian和Selenium WebDriver在Chrome无头模式下运行的示例代码:
首先,确保你的项目中包含以下依赖项:
org.jboss.arquillian
arquillian-drone-webdriver-depchain
1.4.1.Final
test
org.seleniumhq.selenium
selenium-java
3.141.59
test
org.seleniumhq.selenium
selenium-chrome-driver
3.141.59
test
然后,创建一个Arquillian测试类,使用@Drone
注解注入一个WebDriver实例,并使用@Deployment
注解指定部署方法:
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.drone.api.annotation.Drone;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
@RunWith(Arquillian.class)
public class ChromeHeadlessTest {
@Drone
private WebDriver driver;
@Deployment
public static WebArchive createDeployment() {
return ShrinkWrap.create(WebArchive.class)
// 添加你的应用程序代码和资源文件
.addClasses(YourTestClass.class)
.addAsResource("your-test-resource.xml");
}
@Test
public void testChromeHeadless() {
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless"); // 启用无头模式
options.addArguments("--no-sandbox"); // 避免Chrome运行时的权限问题
driver = new RemoteWebDriver(options);
// 执行测试代码
}
}
在上面的示例中,我们使用ChromeOptions
来配置Chrome的无头模式,并添加一些启动参数。
最后,运行该测试类,Arquillian将启动一个无头模式的Chrome浏览器实例,并执行测试代码。
请注意,你需要确保你的系统中已安装Chrome浏览器和对应版本的ChromeDriver。