Skip to content

Instantly share code, notes, and snippets.

@RishiHQ
Last active October 17, 2022 15:26
Show Gist options
  • Save RishiHQ/7b98516c1d8291145394d4c5fe137dbd to your computer and use it in GitHub Desktop.
Save RishiHQ/7b98516c1d8291145394d4c5fe137dbd to your computer and use it in GitHub Desktop.
How to get Firefox and geckodriver to work on Heroku

How to get Firefox and geckodriver working with Capybara on Heroku—thank me later

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).

  1. 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?

  1. Use the webdrivers gem, to install latest geckodriver.

Tip: call Webdrivers::Geckodriver.update if you want to force Geckodriver to download

  1. 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')
    
  2. 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

  1. 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.

If you liked this, check out my blog https://rishi.io :)

@sscirrus
Copy link

@RishiHQ I'm following your advice on using the evosystem-jp/heroku-buildpack-firefox but one question: how do you set the Selenium configuration?

Do you add something like GECKODRIVER_PATH and FIREFOX_BIN to the heroku config, or do something like this in the actual repo code?

Selenium::WebDriver::Firefox::Binary.path = "/app/vendor/firefox/firefox"
Selenium::WebDriver::Gecko.path = "/app/vendor/geckodriver/geckodriver"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment