微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – chromedriver在前台运行的windows jenkins slave上失败

是否有一个技巧让Win Jenkins奴隶与chromedriver合作?

我的测试从maven repo中提取chromedriver和便携式chrome,然后执行它们.我的本地工作正常,当我的构建用户在我的构建系统上做同样的事情.

当jenkins做同样的事情时,即使在前台(而不是svc)中运行它也会失败并出现以下错误.我试过通过params以增加冗长无济于事.

org.openqa.selenium.WebDriverException: unkNown error: Chrome Failed to start: exited normally
(Driver info: chromedriver=2.23.409699 (49b0fa931cda1caad0ae15b7d1b68004acd05129),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace @R_378_4045@ion)
Command duration or timeout: 62.63 seconds
Build info: version: ‘2.41.0’, revision: ‘3192d8a6c4449dc285928ba024779344f5423c58’, time: ‘2014-03-27 11:29:39’
System info: host: ‘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’
Driver info: org.openqa.selenium.chrome.ChromeDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:240)
at org.openqa.selenium.chrome.ChromeDriver.startSession(ChromeDriver.java:181)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:126)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:139)
at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:160)
at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:128)

我设置了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;
  }
}

解决方法:

我们有几个问题,关键似乎是Chrome的无沙箱选项.以下是适用于桌面和在前台或通过服务运行的jenkins slave的解决方案.

第一部分:Maven Unpack of Chrome&司机

>下载PortableApps GoogleChrome
>安装
>将dir重命名为通用名称(GoogleChrome)
>拉链目录
>添加到存储库管理器
>设置maven-dependency-plugin执行以解压缩

  <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>         

 

结果
在测试执行时,我们使用目标/ 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);

/**
 * 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();

}

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐