栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在前台运行的Windows Jenkins从属设备上chromedriver失败

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在前台运行的Windows Jenkins从属设备上chromedriver失败

我们遇到了几个问题,关键似乎是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();    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/507975.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号