Why all this trouble to use Firefox/geckodriver on Heroku? I had to use Firefox and not Chrome because getting Chromedriver up with proxy credentials I think is impossible actually (not just extremely difficult).
- Use this Heroku buildpack to install latest Firefox.
Tip: Set your Webdriver path with Selenium::WebDriver::Firefox::Binary.path = "/app/vendor/firefox/firefox"
Tip: To only do this on production branch on Rails.env.production?
- Use the webdrivers gem, to install latest geckodriver.
Tip: call Webdrivers::Geckodriver.update
if you want to force Geckodriver to download
-
Use the headless option for Capybara:
Capybara.register_driver :selenium_headless do |app| browser_options = Selenium::WebDriver::Firefox::Options.new browser_options.args << '--headless' Capybara::Selenium::Driver.new(app, { browser: :firefox, options: browser_options, }) end driver = Capybara::Session.new(:selenium_headless) driver.visit('https://google.com')
-
Add the Heroku Aptfile buildpack and put the following dependencies for Firefox in your Aptfile
libappindicator1 libasound2 libatk1.0-0 libatk-bridge2.0-0 libcairo-gobject2 libgconf-2-4 libgtk-3-0 libice6 libnspr4 libnss3 libsm6 libx11-xcb1 libxcomposite1 libxcursor1 libxdamage1 libxfixes3 libxi6 libxinerama1 libxrandr2 libxss1 libxt6 libxtst6 fonts-liberation
How the flying heck people can figure this out, I don't know. Btw an Aptfile is like a Gemfile that's language agnostic
- If anything goes wrong, you can get some useful debug output by calling the following:
Selenium::WebDriver.logger.level = :debug
Webdrivers.logger.level = :DEBUG
Also to get a faster feedback loop, run your Capybara code in the rails console on Heroku (heroku ps:exec
then rails c
). That was big for me.
Thank you for sharing the knowledge!