Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrating from chromedriver-helper (Heroku production) #72

Closed
hwhelchel opened this issue Apr 20, 2019 · 33 comments
Closed

Migrating from chromedriver-helper (Heroku production) #72

hwhelchel opened this issue Apr 20, 2019 · 33 comments

Comments

@hwhelchel
Copy link

I use Watir and chromedriver-helper for parallel chrome headless execution on Heroku.

This configuration has worked well for chromedriver-helper. Just tried migrating to webdrivers and now when I call

browser = Watir::Browser.new :chrome, options: options

The app hangs with this message

DevTools listening on ws://127.0.0.1:9222/devtools/browser/ec46578f-304d-4a11-8441-fee4092aebac

Does anyone have a similar setup or advice on how I can get webdrivers and watir to work together on Heroku?

class Browser::Builder
  attr_reader :downloads

  def self.call(downloads: false)
    new(downloads: downloads).call
  end

  def initialize(downloads: false)
    @downloads = downloads
  end

  def key
    @key ||= object_id
  end

  def number
    @number ||= rand(1..20_000)
  end

  def call
    options = Selenium::WebDriver::Chrome::Options.new

    # make a directory for chrome if it doesn't already exist
    chrome_dir = File.join Dir.pwd, ["tmp", "chrome#{key}"]
    FileUtils.mkdir_p chrome_dir
    user_data_dir = "--user-data-dir=#{chrome_dir}"
    # add the option for user-data-dir
    options.add_argument user_data_dir

    # let Selenium know where to look for chrome if we have a hint from
    # heroku. chromedriver-helper & chrome seem to work out of the box on osx,
    # but not on heroku.
    if chrome_bin = ENV["GOOGLE_CHROME_SHIM"]
      options.add_argument "--no-sandbox"
      options.binary = chrome_bin
    end

    # headless!
    options.add_argument "--window-size=2048x1080"
    options.add_argument "--headless"
    options.add_argument "--disable-gpu"
    options.add_argument "--disable-dev-shm-usage"
    options.add_argument "--port=#{9514 + number}"

    # make the browser
    browser = Watir::Browser.new :chrome, options: options

    # setup downloading options
    if downloads
      # make download storage directory
      downloads_dir = File.join Dir.pwd, ["tmp", "downloads#{key}"]
      FileUtils.mkdir_p downloads_dir

      # tell the bridge to use downloads
      bridge = browser.driver.send :bridge
      path = "/session/#{bridge.session_id}/chromium/send_command"
      params = { behavior: "allow", downloadPath: downloads_dir }
      bridge.http.call(:post, path, cmd: "Page.setDownloadBehavior",
                                    params: params)
    end
    browser
  end
end
@kapoorlakshya
Copy link
Collaborator

kapoorlakshya commented Apr 20, 2019

Hi @hwhelchel, your issue reminds me of this related thread where setting Selenium::WebDriver::Chrome.path to the Chrome binary path (ENV["GOOGLE_CHROME_SHIM"] in your case) solved the problem on Heroku.

Give it a try and if it doesn't work, we can troubleshoot this further.

Edit: This will make sure that the downloaded chromedriver is compatible with the Chrome version on Heroku.

@hwhelchel
Copy link
Author

@kapoorlakshya thanks I'm going to try this

@hwhelchel
Copy link
Author

@kapoorlakshya hmm still not able to get it to work on Heroku. Still hanging with message:

DevTools listening on ws://127.0.0.1:9222/devtools/browser/ae8af7ee-c806-4396-8331-99a2f892ec27

I added this line: Selenium::WebDriver::Chrome.path = chrome_bin

in the conditional block:

    if chrome_bin = ENV["GOOGLE_CHROME_SHIM"]
      options.add_argument "--no-sandbox"
      options.binary = chrome_bin
      Selenium::WebDriver::Chrome.path = chrome_bin
    end

Context:

class Browser::Builder
  attr_reader :downloads

  def self.call(downloads: false)
    new(downloads: downloads).call
  end

  def initialize(downloads: false)
    @downloads = downloads
  end

  def key
    @key ||= object_id
  end

  def number
    @number ||= rand(1..20_000)
  end

  def call
    options = Selenium::WebDriver::Chrome::Options.new

    # make a directory for chrome if it doesn't already exist
    chrome_dir = File.join Dir.pwd, ["tmp", "chrome#{key}"]
    FileUtils.mkdir_p chrome_dir
    user_data_dir = "--user-data-dir=#{chrome_dir}"
    # add the option for user-data-dir
    options.add_argument user_data_dir

    # let Selenium know where to look for chrome if we have a hint from
    # heroku. chromedriver-helper & chrome seem to work out of the box on osx,
    # but not on heroku.
    if chrome_bin = ENV["GOOGLE_CHROME_SHIM"]
      options.add_argument "--no-sandbox"
      options.binary = chrome_bin
      Selenium::WebDriver::Chrome.path = chrome_bin
    end

    # headless!
    options.add_argument "--window-size=2048x1080"
    options.add_argument "--headless"
    options.add_argument "--disable-gpu"
    options.add_argument "--disable-dev-shm-usage"
    options.add_argument "--port=#{9514 + number}"

    # make the browser
    browser = Watir::Browser.new :chrome, options: options

    # setup downloading options
    if downloads
      # make download storage directory
      downloads_dir = File.join Dir.pwd, ["tmp", "downloads#{key}"]
      FileUtils.mkdir_p downloads_dir

      # tell the bridge to use downloads
      bridge = browser.driver.send :bridge
      path = "/session/#{bridge.session_id}/chromium/send_command"
      params = { behavior: "allow", downloadPath: downloads_dir }
      bridge.http.call(:post, path, cmd: "Page.setDownloadBehavior",
                                    params: params)
    end
    browser
  end
end

@kapoorlakshya
Copy link
Collaborator

@hwhelchel Which version of Chrome are you using on Heroku?

Since this works with chromedriver-helper, which downloads chromedriver 2.46 as latest, let's try downloading and using v2.46 through webdrivers and see what happens. Place this anywhere before Watir::Browser#new:

Webdrivers::Chromedriver.version = '2.46'

@hwhelchel
Copy link
Author

@kapoorlakshya thanks for the suggestion. I'm trying out browserless.io right now. If I revert to running on Heroku will try this!

Hopefully if others have this same problem they can come to this issue and get some help. Again thank you for your help.

@kapoorlakshya
Copy link
Collaborator

Sounds good! Closing this issue for now. Feel free to revive it if/when needed.

@rgalanakis
Copy link

Hi @kapoorlakshya, I just upgraded and am having the same issue. Setting Webdrivers::Chromedriver.version = '2.46' fixed the problem. Happy to try things further if you give me suggestions on what to try.

@kapoorlakshya kapoorlakshya reopened this Apr 30, 2019
@kapoorlakshya
Copy link
Collaborator

Hi @rgalanakis, which version of Chrome are you using on Heroku, and what does which google-chrome and ENV["GOOGLE_CHROME_SHIM"] return?

@rgalanakis
Copy link

I assume that, after the pin, I am using 2.46 on Heroku. Before that, I believe 73- the Heroku buildpack downloads their latest stable release: https://github.com/heroku/heroku-buildpack-chromedriver

which: /app/.apt/usr/bin/google-chrome
shim: /app/.apt/usr/bin/google-chrome-stable

@kapoorlakshya
Copy link
Collaborator

The stable is 74 now, so that's probably being used for google-chrome-stable unless you have GOOGLE_CHROME_CHANNEL set.

Try setting Selenium::WebDriver::Chrome.path = /app/.apt/usr/bin/google-chrome-stable before launching the browser in your test.

@rgalanakis
Copy link

No dice- adding that and removing the .version = '2.46' results in:
DevTools listening on ws://127.0.0.1:9222/devtools/browser/8620f152-bc3a-4df9-b25d-0754a5f6acaa

@kapoorlakshya
Copy link
Collaborator

kapoorlakshya commented Apr 30, 2019

@rgalanakis Hmm... okay. I just read the README for heroku-buildpack-google-chrome and it says:

You'll need to tell Selenium/chromedriver that the chrome binary is at /app/.apt/usr/bin/google-chrome instead.

The location above and what ENV['GOOGLE_CHROME_SHIM'] returns are different. Let's try this now:

Webdrivers.logger.level = :DEBUG
Selenium::WebDriver::Chrome.path = '/app/.apt/usr/bin/google-chrome'

And if you're using Capybara or Watir, set the binary path in options to '/app/.apt/usr/bin/google-chrome' as well.

@rubendinho
Copy link

Also running into the same issue - if the version is not explictly set, the test suite hangs with the DevTools listening on ws://127.0.0.1... message.

@kapoorlakshya
Copy link
Collaborator

Hi @rubendinho, did you already attempt what I shared above?

@kapoorlakshya
Copy link
Collaborator

@rgalanakis Let's also set the Selenium logging level to :INFO' to see if anything in there can provide any clues.

@rubendinho
Copy link

rubendinho commented May 1, 2019

@kapoorlakshya I tried setting the path inside the Heroku CI debug console and that did not fix anything.

The only way I've been able to stop it from hanging and execute my tests is by explicitly setting the version to the latest stable version - "74.0.3729.6" as of this writing.

Possibly related: heroku/heroku-buildpack-google-chrome#70

@neosepulveda
Copy link

I'm running into the same problem. I tried setting up the path, changing the version, and explicitly setting the version to the latest stable. Nothing has worked for me so far.

I have more information and logs about it in heroku/heroku-buildpack-google-chrome#70

@kapoorlakshya
Copy link
Collaborator

@rubendinho Can you check what /app/.apt/usr/bin/google-chrome --product-version and /app/.apt/usr/bin/google-chrome-stable --product-version return? Also share the debug logs from webdrivers please. I would like to understand why chromedriver v74 isn't being downloaded if that is what Chrome needs.

I don't have access to Heroku CI (using free account), so I'll have to completely rely on you all for the information we need to troubleshoot this.

@neosepulveda I looked at the log in heroku/heroku-buildpack-google-chrome#70 (comment) and you have both the chromedriver buildpack and webdrivers gem included. Let's only use webdivers to troubleshoot this.

Also, your logs don't have this portion (example from my machine) which tells us that the driver is actually not being downloaded for some reason:

< top portion removed as it matches your logs >

2019-05-02 08:52:54 DEBUG Webdrivers Browser executable: '/usr/bin/google-chrome'
2019-05-02 08:52:54 DEBUG Webdrivers Get response: #<Net::HTTPOK 200 OK readbody=true>
2019-05-02 08:52:54 DEBUG Webdrivers Latest version available: 74.0.3729.6
2019-05-02 08:52:54 DEBUG Webdrivers Checking current version
2019-05-02 08:52:54 DEBUG Webdrivers File is already downloaded: false
2019-05-02 08:52:54 DEBUG Webdrivers Deleting /home/kapoorlakshya/.webdrivers/chromedriver
2019-05-02 08:52:54 DEBUG Webdrivers Looking for Site: https://chromedriver.storage.googleapis.com
2019-05-02 08:52:54 DEBUG Webdrivers Get response: #<Net::HTTPOK 200 OK readbody=true>
2019-05-02 08:52:54 DEBUG Webdrivers Found Site: https://chromedriver.storage.googleapis.com
2019-05-02 08:52:55 DEBUG Webdrivers Get response: #<Net::HTTPOK 200 OK readbody=true>
2019-05-02 08:52:55 DEBUG Webdrivers Versions now located on downloads site: [#<Gem::Version "2.0">, #<Gem::Version "2.1">, #<Gem::Version "2.10">, #<Gem::Version "2.11">, #<Gem::Version "2.12">, #<Gem::Version "2.13">, #<Gem::Version "2.14">, #<Gem::Version "2.15">, #<Gem::Version "2.16">, #<Gem::Version "2.17">, #<Gem::Version "2.18">, #<Gem::Version "2.19">, #<Gem::Version "2.2">, #<Gem::Version "2.20">, #<Gem::Version "2.21">, #<Gem::Version "2.22">, #<Gem::Version "2.23">, #<Gem::Version "2.24">, #<Gem::Version "2.25">, #<Gem::Version "2.26">, #<Gem::Version "2.27">, #<Gem::Version "2.28">, #<Gem::Version "2.29">, #<Gem::Version "2.3">, #<Gem::Version "2.30">, #<Gem::Version "2.31">, #<Gem::Version "2.32">, #<Gem::Version "2.33">, #<Gem::Version "2.34">, #<Gem::Version "2.35">, #<Gem::Version "2.36">, #<Gem::Version "2.37">, #<Gem::Version "2.38">, #<Gem::Version "2.39">, #<Gem::Version "2.4">, #<Gem::Version "2.40">, #<Gem::Version "2.41">, #<Gem::Version "2.42">, #<Gem::Version "2.43">, #<Gem::Version "2.44">, #<Gem::Version "2.45">, #<Gem::Version "2.46">, #<Gem::Version "2.5">, #<Gem::Version "2.6">, #<Gem::Version "2.7">, #<Gem::Version "2.8">, #<Gem::Version "2.9">, #<Gem::Version "70.0.3538.16">, #<Gem::Version "70.0.3538.67">, #<Gem::Version "70.0.3538.97">, #<Gem::Version "71.0.3578.137">, #<Gem::Version "71.0.3578.30">, #<Gem::Version "71.0.3578.33">, #<Gem::Version "71.0.3578.80">, #<Gem::Version "72.0.3626.69">, #<Gem::Version "72.0.3626.7">, #<Gem::Version "73.0.3683.20">, #<Gem::Version "73.0.3683.68">, #<Gem::Version "74.0.3729.6">, #<Gem::Version "75.0.3770.8">]
2019-05-02 08:52:55 DEBUG Webdrivers Looking for Site: https://chromedriver.storage.googleapis.com
2019-05-02 08:52:55 DEBUG Webdrivers Get response: #<Net::HTTPOK 200 OK readbody=true>
2019-05-02 08:52:55 DEBUG Webdrivers Found Site: https://chromedriver.storage.googleapis.com
2019-05-02 08:52:55 DEBUG Webdrivers Browser executable: '/usr/bin/google-chrome'
2019-05-02 08:52:55 DEBUG Webdrivers Browser executable: '/usr/bin/google-chrome'
2019-05-02 08:52:55 DEBUG Webdrivers Get response: #<Net::HTTPOK 200 OK readbody=true>
2019-05-02 08:52:55 DEBUG Webdrivers Latest version available: 74.0.3729.6
2019-05-02 08:52:57 DEBUG Webdrivers Get response: #<Net::HTTPOK 200 OK readbody=true>
2019-05-02 08:52:57 DEBUG Webdrivers Successfully downloaded chromedriver_linux64.zip
2019-05-02 08:52:57 DEBUG Webdrivers Decompressing zip
2019-05-02 08:52:57 DEBUG Webdrivers Decompression Complete
2019-05-02 08:52:57 DEBUG Webdrivers Deleting chromedriver_linux64.zip
2019-05-02 08:52:57 DEBUG Webdrivers Completed download and processing of /home/kapoorlakshya/.webdrivers/chromedriver

Let's set Selenium::WebDriver::Chrome.path = '/app/.apt/usr/bin/google-chrome' and see if the logs change. And just to rule out any permission issues, let's also set the download location (Webdrivers.install_dir) to a dir relative to your project root.

@kapoorlakshya kapoorlakshya added the need-info Need more information to troubleshoot. label May 3, 2019
@brenogazzola
Copy link

brenogazzola commented May 7, 2019

The same problem happens on normal Heroku dynos, outside of CI. You can test it on hobby.

1 - Create a new project, put selenium-webdriver and webdrivers in your Gemfile
2 - Deploy to Heroku
3 - Connect to a one-off dyno using heroku run rails c -a APP_NAME
4 - Run your tests there.

I did that test and got this:

breno@festalab-lenovo:~/RubymineProjects/festalab-app$ heroku run rails c -a festalab-selenium-test
Running rails c on ⬢ festalab-selenium-test... up, run.1146 (Hobby)
Loading production environment (Rails 6.0.0.rc1)
irb(main):001:0> options = Selenium::WebDriver::Chrome::Options.new
=> #<Selenium::WebDriver::Chrome::Options:0x00007f66a9c757d8 @args=#<Set: {}>, @binary=nil, @prefs={}, @extensions=[], @options={}, @emulation={}, @encoded_extensions=[]>
irb(main):002:0> options.add_argument('--headless')
=> #<Set: {"--headless"}>
irb(main):003:0> options.binary = ENV.fetch('GOOGLE_CHROME_SHIM', nil)
=> "/app/.apt/usr/bin/google-chrome-stable"
irb(main):004:0> driver = Selenium::WebDriver.for :chrome, options: options

DevTools listening on ws://127.0.0.1:9222/devtools/browser/7b852d85-ea4e-49ff-8351-4a88fb18d5eb

As you can see, it hangs on Devtool instead of returning so my code can continue. So I aborted and tried again:

irb(main):005:0> Webdrivers.logger.level = :DEBUG
=> :DEBUG
irb(main):006:0> Selenium::WebDriver::Chrome.path = '/app/.apt/usr/bin/google-chrome'
=> "/app/.apt/usr/bin/google-chrome"
irb(main):007:0> driver = Selenium::WebDriver.for :chrome, options: options
2019-05-07 21:47:03 DEBUG Webdrivers Looking for Site: https://chromedriver.storage.googleapis.com
2019-05-07 21:47:03 DEBUG Webdrivers Get response: #<Net::HTTPOK 200 OK readbody=true>
2019-05-07 21:47:03 DEBUG Webdrivers Found Site: https://chromedriver.storage.googleapis.com
2019-05-07 21:47:03 DEBUG Webdrivers Looking for Site: https://chromedriver.storage.googleapis.com
2019-05-07 21:47:03 DEBUG Webdrivers Get response: #<Net::HTTPOK 200 OK readbody=true>
2019-05-07 21:47:03 DEBUG Webdrivers Found Site: https://chromedriver.storage.googleapis.com
2019-05-07 21:47:03 DEBUG Webdrivers Browser executable: '/app/.apt/usr/bin/google-chrome'

DevTools listening on ws://127.0.0.1:9222/devtools/browser/a63137c2-a96d-467f-b8d0-02fc8fc5ec1d

Hope this helps. My app relies on Selenium in production to generate website screenshots so I'm definitely interested in seeing this problem fixed.

@brenogazzola
Copy link

brenogazzola commented May 7, 2019

I'm taking a wild guess here, since I don't know how webdrivers actually works, but Heroku only allows writing to a few directories (such as $HOME and /tmp), so webdrivers might be silently failing the download because it doesn't have permission to write in the directory it expect to be able to.

Also, strangely, when I try to get the product version from chrome I instead get a prompt about DevTools:

breno-macbook:festalab-app brenogazzola$ heroku run bash -a festalab-selenium-test
Running bash on ⬢ festalab-selenium-test... up, run.4014 (Hobby)
~ $ /app/.apt/usr/bin/google-chrome --product-version

DevTools listening on ws://127.0.0.1:9222/devtools/browser/1ecf302f-cfde-4e69-aa87-85fd5a20aaa9
^C
~ $ /app/.apt/usr/bin/google-chrome-stable --product-version

DevTools listening on ws://127.0.0.1:9222/devtools/browser/edb6c25e-8603-4569-9277-fd214bea35e4
^C
~ $ 

@kapoorlakshya
Copy link
Collaborator

@brenogazzola Can you hop on Slack? I would also like to see this issue resolved as well. It's a strange one.

https://seleniumhq.slack.com/messages/CJE1WFAHE

@brenogazzola
Copy link

brenogazzola commented May 7, 2019

@kapoorlakshya Tried to, but it expects me to have @seleniumhq.org, @slack-corp.com, or @saucelabs.com emails to allow me to create an account, which I don't have.

@kapoorlakshya
Copy link
Collaborator

@brenogazzola Try the Slack link from here instead - https://seleniumhq.herokuapp.com. Once you are in, join the #webdrivers-gem channel.

@kapoorlakshya
Copy link
Collaborator

kapoorlakshya commented May 7, 2019

Thanks to @brenogazzola and his Heroku setup, we've figured out the problem. The problem is that the shim responds to --product-version by launching the browser instead of returning the version. This means that webdrivers keeps waiting to receive the version and it never does.

I looked at the code for heroku-buildpack-google-chrome and learned that it stores the actual binary as chrome\chrome and $GOOGLE_CHROME_BIN points to it - /app/.apt/opt/google/chrome/chrome. This binary returns the --product-version that webdrivers needs.

We were able to successfully download the correct chromedriver and launch the browser with this code:

Selenium::WebDriver::Chrome.path = ENV.fetch('GOOGLE_CHROME_BIN', nil)
options = Selenium::WebDriver::Chrome::Options.new
options.binary = ENV.fetch('GOOGLE_CHROME_SHIM', nil)
driver = Selenium::WebDriver.for :chrome, options: options

I have documented this in the wiki - https://github.com/titusfortner/webdrivers/wiki/heroku-buildpack-google-chrome

@kapoorlakshya kapoorlakshya removed the need-info Need more information to troubleshoot. label May 7, 2019
@neosepulveda
Copy link

Thanks @kapoorlakshya for resolving this issue so quickly.

One quick question, in terms of best practises, where would be the best place to add the configuration code above?

Thank you

@kapoorlakshya
Copy link
Collaborator

@neosepulveda I am not sure if there is an official best practice for this, but I would do it right before browser initialization to keep the driver and browser related code close together.

@thomasdarde
Copy link

On a rails app, I'm still having issues with this setup on Heroku

Before RSpec.configure I did add the following

Webdrivers.cache_time = 86_400

require 'webdrivers'

# From : https://github.com/titusfortner/webdrivers/issues/72
if ENV.fetch('GOOGLE_CHROME_BIN', nil)
  Selenium::WebDriver::Chrome.path = ENV.fetch('GOOGLE_CHROME_BIN', nil)
  options = Selenium::WebDriver::Chrome::Options.new
  options.binary = ENV.fetch('GOOGLE_CHROME_SHIM', nil)
  driver = Selenium::WebDriver.for :chrome, options: options
end

Capybara.register_driver(:chrome) do |app|
  capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
    chromeOptions: { args: %w[headless disable-gpu] }
  )

  Capybara::Selenium::Driver.new(
    app,
    browser: :chrome,
    desired_capabilities: capabilities
  )
end

During Rspec configure :

config.before(:each, type: :system, js: true) do
    # Chrome driver, visible , good for debugging
    # driven_by :selenium_chrome

    # Default driver: headless.
    driven_by :chrome
  end

And it still fails listening forever to ws://127.0.0.1:9222/devtools/browser/bc537da7-79e6-4443-9f89-fc297c94a113

Tried a lot of different setups with no avail.. Any tip would be appreciated !

@kapoorlakshya
Copy link
Collaborator

Hi @thomasdarde, please set Webdrivers.logger.level = :DEBUG after requiring the gem and share the log here.

@brenogazzola
Copy link

@thomasdarde Maybe Rspec is overwriting your configuration when starting? Is there a good point, just before the test suit opens the browser, where you can have the test suit print Selenium::WebDriver::Chrome.path to ensure it's still correct? In minitest I'd do that in the setup method.

@thomasdarde
Copy link

Thanks a lot for the replies !

Here is the updated code :

Webdrivers.cache_time = 86_400
puts "setting debug level for webdrivers logger"
puts "GOOGLE_CHROME_BIN is: #{ENV.fetch('GOOGLE_CHROME_BIN', nil)} "
puts "GOOGLE_CHROME_SHIM is: #{ENV.fetch('GOOGLE_CHROME_SHIM', nil)} "

puts "--product-version : #{`/app/.apt/opt/google/chrome/chrome --product-version`} "
Webdrivers.logger.level = :DEBUG
require 'webdrivers'

# From : https://github.com/titusfortner/webdrivers/issues/72
if ENV.fetch('GOOGLE_CHROME_BIN', nil)
  Selenium::WebDriver::Chrome.path = ENV.fetch('GOOGLE_CHROME_BIN', nil)
  options = Selenium::WebDriver::Chrome::Options.new
  options.binary = ENV.fetch('GOOGLE_CHROME_SHIM', nil)
  driver = Selenium::WebDriver.for :chrome, options: options
end

and the output

setting debug level for webdrivers logger
GOOGLE_CHROME_BIN is: /app/.apt/usr/bin/google-chrome 
GOOGLE_CHROME_SHIM is: /app/.apt/usr/bin/google-chrome 
--product-version : 74.0.3729.157
 
2019-05-17 07:41:43 DEBUG Webdrivers Checking current version
2019-05-17 07:41:43 DEBUG Webdrivers /app/.webdrivers/chromedriver is not already downloaded
2019-05-17 07:41:43 DEBUG Webdrivers Browser executable: '/app/.apt/usr/bin/google-chrome'
2019-05-17 07:41:43 DEBUG Webdrivers making System call: /app/.apt/usr/bin/google-chrome --product-version
DevTools listening on ws://127.0.0.1:9222/devtools/browser/fca91d6e-3e9d-4dfe-8f2d-cbe7c11f1550

@thomasdarde
Copy link

So the issue was found : GOOGLE_CHROME_BIN was set as an env var to another value (/app/.apt/usr/bin/google-chrome) in the CI due to an old configuration...

In the end, this is my (working) config (Rspec/capybara/webdrivers) :

At the beginning of the rails_helper:

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

The file spec/support/capybara_webdrivers.rb

Webdrivers.cache_time = 86_400

# Some help to debug on CI
# puts "--product-version : #{`/app/.apt/opt/google/chrome/chrome --product-version`} "
# Webdrivers.logger.level = :DEBUG
require "webdrivers"

chrome_shim = ENV.fetch("GOOGLE_CHROME_SHIM", nil)
chrome_bin = ENV.fetch("GOOGLE_CHROME_BIN", nil)

# From : https://github.com/titusfortner/webdrivers/issues/72

Selenium::WebDriver::Chrome.path = chrome_bin if chrome_bin
chrome_args = {args: %w[headless disable-gpu window-size=1280,1024]}
selenium_options = Selenium::WebDriver::Chrome::Options.new(chrome_args)
selenium_options.binary = chrome_shim if chrome_shim

Capybara.register_driver :chrome do |app|
  Capybara::Selenium::Driver.new(
    app,
    browser: :chrome, options: selenium_options
  )
end

# Silent puma in logs
Capybara.server = :puma, { Silent: true }

@kapoorlakshya
Copy link
Collaborator

@thomasdarde Glad you figured it out. It got stuck on the DevTools listening line because it was attempting to extract the --product-version from the binary, but the binary was responding by launching the browser instead. Using the correct binary will always solve this problem.

jabrown85 pushed a commit to heroku/heroku-buildpack-google-chrome that referenced this issue Jun 21, 2019
)

This change adds out-of-the-box compatibility with the webdrivers gem, which apparently quite a few Heroku users are using. Currently, the users have to use a workaround in their project to get the Chrome version from $GOOGLE_CHROME_BIN, so that webdrivers can download the appropriate version of chromedriver, and then switch back to $GOOGLE_CHROME_SHIM to launch Chrome.

See titusfortner/webdrivers#40, titusfortner/webdrivers#72, and titusfortner/webdrivers#134.

This simple change will provide the convenience of not needing any hacks to make the two work :).
@kapoorlakshya
Copy link
Collaborator

The latest version of the google-chrome buildpack no longer needs the workaround described here. We've updated the SHIM to support webdrivers. See heroku/heroku-buildpack-google-chrome#73 for more information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants