Selenium With Headless Chrome On Travis CI
How do you run any browser automation tool in a CI environment, like Travis, where there is no graphical browser? How can your Selenium tests run in such conditions?
The answer is: you need a headless browser; that is, a browser which can work without a GUI. Luckily, we don’t have to use phantomJS or xvfb anymore, since Google Chrome now supports headless mode.
I found many posts about running chrome headlessly, but none that would describe exactly what I needed: Selenium + Chrome + Travis. It took me a while to set it up, so I decided to write this short post (even Travis’ documentation on the topic is rather confusing).
You have to do 3 things:
- Get the Google Chrome stable addon
- Get chromedriver
- Pass the path to the Google Chrome executable to the WebDriver.
The first 2 points mean a few lines of code in your .travis.yml
:
Now, in the script
section you will be able to see both google chrome and chromedriver, like this
The 3rd step is building your WebDriver (Java, Selenium 2.41.0):
You see, the --headless
and --disable-gpu
options are needed. As I read in the
documentation, --disable-gpu
won’t be needed in future versions. Note that I didn’t have to set the webdriver.chrome.driver
system property, as the ChromeDriver
object searches for it automatically. Don’t forget to set it if you encounter any issues.
This is it. It seems to work flawlessly for me. It is a perfect replacement for phantomJS. I was previously using PhantomJsDriver (which implements WebDriver) from this project, but I had some problems with phantomJs and I also read that its maintainer is stepping down.