【问题标题】:Chromedriver on Travis-CITravis-CI 上的 Chrome 驱动程序
【发布时间】:2019-01-07 01:19:00
【问题描述】:

我无法让 Travis-CI 上的 chromedriver 为我的项目 knockout-secure-binding 工作。至少,我正在尝试使用 WebdriverJS 来自动使用 Chrome 进行测试。

I noted Travis-CI 上的 chromedriver 似乎存在一些问题,包括:

该问题似乎是“无法访问 chrome”的变体,据我所知,它需要 Google 的上游参与来解决它。

错误的详细信息是available through the Travis build log

没有明显的解决方法,尽管有一条评论提到使用--no-sandbox,但不清楚在 WebdriverJS 中的何处或如何使用它。

对此的任何想法将不胜感激。

——编辑——

出于兴趣,我使用 Sauce Labs 代替 Chromedriver。

【问题讨论】:

  • 尝试将sudo: true 添加到您的.travis.yml 文件中。这将启动一种不同类型的虚拟机,它可能会更好地工作。 (不过,启动时间会慢一些。)

标签: google-chrome selenium-chromedriver travis-ci


【解决方案1】:

在 Travis CI 上启动 Chrome 有一种更简单的方法,只需在 addons/apt/sources 中指定 google-chrome,在 addons/apt/packages 中指定 google-chrome-package。

这是我的示例配置,以便更好地理解:

sudo: required
dist: trusty
addons:
  apt:
    sources:
      - google-chrome
    packages:
      - google-chrome-stable

language: node_js
node_js:
  - "6"
cache:
  directories: node_modules
branches:
  only: master

before_script:
  - export DISPLAY=:99.0
  - sh -e /etc/init.d/xvfb start
  - npm i -g npm@^3
  - sleep 3

【讨论】:

    【解决方案2】:

    我认为 Travis 确实支持 chrome 驱动程序,如果你在 travis.yml 中添加这些,请提取正确的 chromedriver 并将其解压缩到已知位置,以便以后跟踪它。

    before_script:
      - wget http://chromedriver.storage.googleapis.com/2.10/chromedriver_linux64.zip
      - unzip chromedriver_linux64.zip -d /home/travis/virtualenv/python2.7.9/
      - export CHROME_BIN=chromium-browser
      - "export DISPLAY=:99.0"
      - "sh -e /etc/init.d/xvfb start"
      - sleep 3 
    

    另外,当您调用 selenium 或任何测试自动化库时,您需要在 Python 中添加此代码,但这也可以在 JavaRuby 中完成。

    options = webdriver.ChromeOptions()
    options.binary_location = '/usr/bin/chromium-browser'
    #All the arguments added for chromium to work on selenium
    options.add_argument("--no-sandbox") #This make Chromium reachable
    options.add_argument("--no-default-browser-check") #Overrides default choices
    options.add_argument("--no-first-run")
    options.add_argument("--disable-default-apps") 
    driver = webdriver.Chrome('/home/travis/virtualenv/python2.7.9   /chromedriver',chrome_options=options)
    

    【讨论】:

      【解决方案3】:

      编辑:截至 2018 年 10 月,Travis CI 正在慢慢远离容器(请参阅 official announcement)。因此,可以省略 sudo: false,但给定的 ChromeDriver 设置仍然有效。

      如果你想使用container-based 环境(启动时间快但没有sudo),你也可以这样做(包括language 等等):

      dist: trusty
      sudo: false
      
      addons:
        chrome: stable
        apt:
          packages:
            - chromium-chromedriver
      
      before_script:
        # include ChromeDriver in PATH
        - ln --symbolic /usr/lib/chromium-browser/chromedriver "${HOME}/bin/chromedriver"
        # start Chrome and listen on localhost
        - google-chrome-stable --headless --disable-gpu --remote-debugging-port=9222 http://localhost &
      

      然后,正如您已经提到的,将 --no-sandbox 添加到您的 Chrome 选项中(取自 gist):

      var webdriver = require('selenium-webdriver');
      
      var chromeOptions = {
          'args': ['--no-sandbox']
      };
      
      var chromeCapabilities = webdriver.Capabilities.chrome();
      chromeCapabilities.set('chromeOptions', chromeOptions);
      
      var driver = new webdriver.Builder().withCapabilities(chromeCapabilities).build();
      

      这是由于 Travis CI 中的 issue。但是,如果您无论如何都需要sudo,或者有一个长时间运行的构建,而基于容器的环境只有有限的意义,您也可以设置sudo: true 并省略添加--no-sandbox

      其他资源:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-28
        • 1970-01-01
        • 2021-02-16
        • 1970-01-01
        相关资源
        最近更新 更多