我们遇到了几个问题,关键似乎是Chrome的无沙箱选项。以下是在桌面和在前台或通过服务运行的jenkins从属服务器上运行的解决方案。
第一部分:Chrome和驱动程序的Maven解压缩
- 下载PortableApps GoogleChrome
- 安装
- 将目录重命名为通用名称(GoogleChrome)
- 邮编目录
- 添加到存储库管理器
- 设置Maven依赖插件执行以解压
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.8</version> <executions> <execution> <id>extract portable google chrome</id> <phase>process-test-resources</phase> <goals> <goal>unpack</goal> </goals> <configuration> <skip>${skipWinChromeUnpack}</skip> <markersDirectory>${project.build.directory}/dependency-maven-plugin-markers/googlechrome</markersDirectory> <overWriteIfNewer>false</overWriteIfNewer> <artifactItems><artifactItem> <groupId>com.google.chromium</groupId> <artifactId>chromedriver</artifactId> <version>${win.chromedriver.version}</version> <classifier>win32</classifier> <type>zip</type> <outputDirectory>${project.build.directory}</outputDirectory></artifactItem><artifactItem> <groupId>com.portableapps</groupId> <artifactId>googlechrome</artifactId> <version>${win.chrome.version}</version> <classifier>win64</classifier> <type>zip</type> <outputDirectory>${project.build.directory}</outputDirectory></artifactItem> </artifactItems> </configuration> </execution> 结果 在测试执行时,我们有target / chromedriver.exe和target / GooglePortable / Google
… exe文件要使用
第二部分:Maven Surefire配置
我们为驱动程序和chrome exe的位置设置了系统属性,以传递给所有单元测试
<systemPropertyVariables> <webdriver.chrome.driver>${project.build.directory}/chromedriver.exe</webdriver.chrome.driver> <win.google.chrome.bin>${win.chrome.exe}</win.google.chrome.bin> </systemPropertyVariables>第三部分:测试代码
我们使用chrome驱动程序服务构建器将详细程度设置为11,然后使用我们最喜欢的功能启动驱动程序
public class ChromeLocator { private static final Logger log = Logger.getLogger(ChromeLocator.class); public DesiredCapabilities getCapabilities() throws IOException { ChromeOptions chromeOptions = new ChromeOptions(); chromeOptions.setBinary(getChromeExecutableLocation().getAbsolutePath()); chromeOptions.addArguments("no-sandbox"); DesiredCapabilities capabilities = DesiredCapabilities.chrome(); capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions); return capabilities; } // Windows defaults to unpacked location private File getChromeExecutableLocation() throws IOException { File chromeExe; if (SystemUtils.IS_OS_WINDOWS) { chromeExe = new File(System.getProperty("win.google.chrome.bin")); } else { // Use the standard locator option for all other operating systems GoogleChromeLocator locator = new GoogleChromeLocator(); BrowserInstallation installation = locator.findBrowserLocationOrFail(); chromeExe = new File(installation.launcherFilePath()); } System.out.println("Chrome Exe: " + chromeExe.getAbsolutePath() + " Is File: " + chromeExe.isFile()); if (! chromeExe.exists() || ! chromeExe.isFile()) { throw new IOException("Cannot locate Chrome Executable. Expected Location: " + chromeExe.getAbsolutePath()); } return chromeExe; }} public class WebTest { static ChromeDriverService service = null; static WebDriver driver = null; @BeforeClass static public void setuponce() throws IOException { // Setup ChromeDriver with Verbosity on - perhaps control via system property - off by default? service = new ChromeDriverService.Builder() .withVerbose(true) .usingAnyFreePort() .build(); service.start(); // Setup locator to find unpacked Portable chrome exe ChromeLocator locator = new ChromeLocator(); // Use service + capabilities from locator to open driver with settings and chrome bin driver = new RemoteWebDriver(service.getUrl(), locator.getCapabilities()); } @AfterClass static public void teardownonce() { if (null != service) { service.stop(); service = null; } } @Test public void testGoogleSearch() throws InterruptedException, IOException { driver.get("http://www.google.com/xhtml"); assertEquals("Google", driver.getTitle()); WebElement searchBox = driver.findElement(By.name("q")); String searchString = "ChromeDriver"; searchBox.sendKeys(searchString); searchBox.submit(); String source = driver.getPageSource().toString(); assertTrue("Expected DOCTYPE inn" + source, source.contains("DOCTYPE")); driver.quit(); service.stop(); }}


