Chromedriver on Travis-CI

假如想象 提交于 2019-12-02 20:31:25

There's an easier way to launch Chrome on Travis CI, simply specify google-chrome in addons/apt/sources and google-chrome-package in addons/apt/packages.

Here's my sample config for a better understanding:

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

I think Travis does support chrome driver, if you add these in your travis.yml, extract the right chromedriver and unzip it to a known location, so that you can trace it later.

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 

Plus when you call selenium or any testing automation library, you would need to add this the code here is in Python but this can be done in Java and Ruby as well.

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)

EDIT: As of October 2018, Travis CI is slowly moving away from containers (see official announcement). Therefore, one can omit sudo: false, but the given ChromeDriver setup still works.

If you want to use a container-based environment (fast boot time but no sudo), you can also do it as follows (include language and so forth accordingly):

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 &

Afterwards, as you already mentioned, add --no-sandbox to your Chrome options (taken from this 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();

This is due to an issue in Travis CI. However, if you need sudo anyway or have a long-running build where a container-based environment makes only limited sense, you can also set sudo: true and omit adding --no-sandbox.

Additional resources:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!