Java chromedriver在前台运行的windows jenkins slave上失败
有没有一个诀窍可以让Win Jenkins slave与chromedriver配合得很好 我的测试从maven repo中提取chromedriver和便携式chrome,然后执行它们。当我的本地构建用户在我的构建系统上执行同样的操作时,我的本地构建可以正常工作 当jenkins做同样的事情时,即使在前台(不是svc)运行,它也会失败,错误如下。我试着传递参数来增加冗长的内容,但没有用 org.openqa.selenium.WebDriverException:未知错误:Chrome无法启动:正常退出 (驱动程序信息:chromedriver=2.23.409699(49b0fa931cda1caad0ae15b7d1b68004acd05129),平台=Windows NT 6.1.7601 SP1 x8664)(警告:服务器未提供任何堆栈跟踪信息) 命令持续时间或超时:62.63秒 构建信息:版本:“2.41.0”,修订版:“3192D8A6C4449DC285928BA024793444F5423C58”,时间:“2014-03-27 11:29:39” 系统信息:主机:'winengbld15',ip:'10.2.2.105',os.name:'Windows Server 2008 R2',os.arch:'amd64',os.version:'6.1',java.version:'1.8.0_40' 驱动程序信息:org.openqa.selenium.chrome.ChromeDriver 位于sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) 位于sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 位于java.lang.reflect.Constructor.newInstance(Constructor.java:422) 位于org.openqa.selenium.remote.ErrorHandler.CreateTrowable(ErrorHandler.java:193) 位于org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145) 位于org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595) 位于org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:240) 位于org.openqa.selenium.chrome.ChromeDriver.startSession(ChromeDriver.java:181) 位于org.openqa.selenium.remote.RemoteWebDriver。(RemoteWebDriver.java:126) 位于org.openqa.selenium.remote.RemoteWebDriver。(RemoteWebDriver.java:139) 位于org.openqa.selenium.chrome.ChromeDriver(ChromeDriver.java:160) 位于org.openqa.selenium.chrome.ChromeDriver(ChromeDriver.java:128) 我将Chrome驱动程序设置为:Java chromedriver在前台运行的windows jenkins slave上失败,java,maven,selenium,jenkins,selenium-chromedriver,Java,Maven,Selenium,Jenkins,Selenium Chromedriver,有没有一个诀窍可以让Win Jenkins slave与chromedriver配合得很好 我的测试从maven repo中提取chromedriver和便携式chrome,然后执行它们。当我的本地构建用户在我的构建系统上执行同样的操作时,我的本地构建可以正常工作 当jenkins做同样的事情时,即使在前台(不是svc)运行,它也会失败,错误如下。我试着传递参数来增加冗长的内容,但没有用 org.openqa.selenium.WebDriverException:未知错误:Chrome无法启动
defaultPath = "target/drivers/chromedriver.exe";
System.setProperty("webdriver.chrome.driver", defaultPath);
ChromeLocator locator = new ChromeLocator();
driver = new ChromeDriver(locator.getCapabilities());
public class ChromeLocator {
private static final Logger log = Logger.getLogger(ChromeLocator.class);
/**
* Obtain Chrome Configuration with location of binary
* @return
* @throws IOException
*/
public DesiredCapabilities getCapabilities() throws IOException {
Map<String, Object> chromeOptions = new HashMap<String, Object>();
chromeOptions.put("binary", getChromeExecutableLocation().getAbsolutePath());
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"));
log.info("*** win.google.chrome.bin: " + 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());
}
log.info("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;
}
}
defaultPath=“target/drivers/chromedriver.exe”;
System.setProperty(“webdriver.chrome.driver”,defaultPath);
ChromeLocator定位器=新ChromeLocator();
驱动程序=新的ChromeDriver(locator.getCapabilities());
公共级色度计{
私有静态最终记录器log=Logger.getLogger(ChromeLocator.class);
/**
*获取具有二进制文件位置的Chrome配置
*@返回
*@抛出异常
*/
public DesiredCapabilities getCapabilities()引发IOException{
Map chromeOptions=new HashMap();
put(“二进制”,getChromeExecutableLocation().getAbsolutePath());
DesiredCapabilities=DesiredCapabilities.chrome();
能力。设置能力(ChromeOptions.CAPABILITY,ChromeOptions);
返回能力;
}
//Windows默认为未打包位置
私有文件getChromeeExecutableLocation()引发IOException{
文件chromeExe;
if(SystemUtils.IS\u OS\u WINDOWS){
chromeExe=新文件(System.getProperty(“win.google.chrome.bin”);
log.info(“***win.google.chrome.bin:”+System.getProperty(“win.google.chrome.bin”);
}否则{
//对所有其他操作系统使用标准定位器选项
GoogleChromeLocator定位器=新的GoogleChromeLocator();
BrowserInstallation安装=locator.findBrowserLocationOrFail();
chromeExe=新文件(installation.launcherFilePath());
}
log.info(“Chrome Exe:+chromeExe.getAbsolutePath()+”是文件:+chromeExe.isFile());
如果(!chromeExe.exists()| |!chromeExe.isFile()){
抛出新IOException(“无法找到Chrome可执行文件。预期位置:”+chromeExe.getAbsolutePath());
}
返回chromeExe;
}
}
我们有几个问题,关键似乎是Chrome没有沙箱选项。下面是一个在桌面和jenkins从机上工作的解决方案,该从机运行在前台或通过服务
第一部分:Maven打开Chrome和Driver的包装
- 下载PortableApps谷歌浏览器
- 安装
- 将目录重命名为通用名称(谷歌浏览器)
- Zip目录
- 添加到存储库管理器
- 设置要解压缩的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>
<systemPropertyVariables> <webdriver.chrome.driver>${project.build.directory}/chromedriver.exe</webdriver.chrome.driver> <win.google.chrome.bin>${win.chrome.exe}</win.google.chrome.bin> </systemPropertyVariables>
public class ChromeLocator { private static final Logger log = Logger.getLogger(ChromeLocator.class); /** * Obtain Chrome Configuration with location of binary * @return * @throws IOException */ 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 in\n" + source, source.contains("DOCTYPE")); driver.quit(); service.stop(); }