If you do Feature Specs, the act of loading up a real app server and then a real headless browser to do real user feature testing, then you know Capybara and one of its most well-known drivers, Poltergeist. Poltergeist wraps up PhantomJS, which is a well known WebKit-based headless browser.
But WebKit is known for being very complicated to deal with. So I can only imagine the nightmare to maintain PhantomJS, which is akin to main a full-blown web browser like Chrome or Safari.
So it's no wonder that when the Chrome team announced the availability of the Chrome Driver, then the maintainer of PhantomJS decided to step down.
If you know the contributors of PhantomJS, say thank you, as it helped as build more solid user features.
That being said, fear not. You can easily replace Poltergeist/PhantomJS for Selenium WebDriver/Chrome Driver in your RSpec/Capybara setup.
My friend Lucas Caton wrote about it in June this year. Follow his blog as well.
If you're using Linux with the Chromium browser, you don't need to install anything, as the Chrome Driver comes with Chromium. Otherwise, you need to install the proper packages for your operating system. For example, brew install chromedriver
for OS X or pacaur -S chromedriver
on Arch if you don't like to have Chromium around. You may need to tweak your PATH on Ubuntu though.
Rule of thumb: install Chromium.
In my case, this is what had to change:
123 | # Gemfile- gem "poltergeist"+ gem "selenium-webdriver" |
Then in the Capybara setup:
123456789101112131415161718192021222324252627282930 | Capybara.server = :puma # Until your setup is workingCapybara.server = :puma, { Silent: true } # To clean up your test output- Capybara.register_driver :poltergeist do |app|- options = {- timeout: 3.minutes,- phantomjs_options: [- '--proxy-type=none',- '--load-images=no',- '--ignore-ssl-errors=yes',- '--ssl-protocol=any',- '--web-security=false'- ]- }- Capybara::Poltergeist::Driver.new(app, options)- end- Capybara.javascript_driver = :poltergeist+ Capybara.register_driver :chrome do |app|+ Capybara::Selenium::Driver.new app, browser: :chrome,+ options: Selenium::WebDriver::Chrome::Options.new(args: %w[headless disable-gpu])+ end++ Capybara::Screenshot.register_driver :chrome do |driver, path|+ driver.save_screenshot(path)+ end++ Capybara.javascript_driver = :chromeCapybara.default_max_wait_time = 5 # you may want to increase this timeout if your app is heavy to load |
And that should be it. I didn't have to touch any of my feature specs and they all ran beautifully. So kudos to the respective teams that maintain Capybara, Selenium-WebDriver for supporting this.
If you're a Node.js developer as well, you probably used something like Casper, which is said to support Chrome Headless as well. But while we're at it, you should check out Puppeteer as well, from the Google team itself. It is a promise based library where you can code like this:
12345678910 | const puppeteer = require('puppeteer');(async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto('https://example.com'); await page.screenshot({path: 'example.png'}); await browser.close();})(); |
So yeah, Chrome Headless seems like a very good option as most users actually use the Chrome browser, so it means we should have more reliable feature specs and also web crawling tools.