Search code examples
rubyseleniumselenium-webdrivercucumbercapybara

Issue with Selenium WebDrive with Docker image and Cucumber.io


Im trying to follow along with the following tutorial.

https://www.youtube.com/watch?v=cPF3GKkBHHY

I was getting the following error.

sh: 1: /sbin/ip: not found

But I managed to resolve that doing apt update then apt install iproute2 -y.

I am now getting the following error

cucumber
Feature: Search for things on Google and see results.

  Scenario: See related words when searching. # features/basic.feature:3
    When I search for "puppies"               # features/step_defs.rb:1
      Selenium::WebDriver::Remote::Driver needs :options to be set (ArgumentError)
      ./features/step_defs.rb:2:in `"I search for {string}"'
      features/basic.feature:4:in `I search for "puppies"'
    Then I should see "dog"                   # features/step_defs.rb:7

The error im looking to resolve is this one.

Selenium::WebDriver::Remote::Driver needs :options to be set (ArgumentError)

this is my .env.rb file.

require 'rspec' #for page.shoud etc
require 'capybara/cucumber'
require 'selenium-webdriver'
require 'pry'

#if you're accessing an internal app behind a firewall, you may not need the proxy. You can unset it like so:
#ENV['HTTP_PROXY'] = ENV['http_proxy'] = nil

#get IP of host which has 4444 mapped from other container
docker_ip = %x(/sbin/ip route|awk '/default/ { print $3 }').strip

Capybara.register_driver :remote_chrome do |app|
  Capybara::Selenium::Driver.new(app,
  :browser => :remote,
  :desired_capabilities => :chrome,
  :url => "http://#{docker_ip}:4444/wd/hub",
  :options => chrome_options)
end

Capybara.configure do |config|
  config.run_server = false
  config.default_driver = :remote_chrome
  config.app_host = 'http://www.google.com' # change this to point to your application
end

Any help with this would be greatly appreciated.

Thanks!


Solution

  • Solution:

    Update env.rb file.
    Before:

    require 'rspec' #for page.shoud etc
    require 'capybara/cucumber'
    require 'selenium-webdriver'
    require 'pry'
    
    #if you're accessing an internal app behind a firewall, you may not need the proxy. You can unset it like so:
    #ENV['HTTP_PROXY'] = ENV['http_proxy'] = nil
    
    #get IP of host which has 4444 mapped from other container
    docker_ip = %x(/sbin/ip route|awk '/default/ { print $3 }').strip
    
    Capybara.register_driver :remote_chrome do |app|
      Capybara::Selenium::Driver.new(app,
      :browser => :remote,
      :desired_capabilities => :chrome,
      :url => "http://#{docker_ip}:4444/wd/hub")
    end
    
    Capybara.configure do |config|
      config.run_server = false
      config.default_driver = :remote_chrome
      config.app_host = 'http://www.google.com' # change this to point to your application
    end
    

    After:

    require 'rspec' #for page.shoud etc
    require 'capybara/cucumber'
    require 'cucumber'
    require 'pry'
    
    require "selenium-webdriver"
    
    # Ask capybara to register a driver called 'selenium'
    Capybara.register_driver :selenium do |app|
      Capybara::Selenium::Driver.new(
          app,
    
          #what browser do we want? Must match whatever is in our seleniarm stand-alone image
          browser: :firefox, 
          
          #where does it live? By passing a URL we tell capybara to use a selenium grid instance (not local)
          url: "http://#{ENV['SELENIUM_HOST']}:#{ENV['SELENIUM_PORT']}" 
      )
    end
    
    # make the driver we just registered our default driver
    Capybara.default_driver = :selenium
    
    # set the default URL for our tests
    Capybara.app_host = "https://www.google.com/"
    

    Update basic.feature
    Before:

    Feature: Search for things on Google and see results.
    
    Scenario: See related words when searching.
      When I search for "puppies"
      Then I should see "dog"
    
      
    Scenario: Don't see unrelated words when searching.
      When I search for "dachshund"
      Then I should NOT see "fish"
    

    After:

    Feature: Search for things on Google and see results.
    
    Background:
      Given I accept cookies
    
    Scenario: See related words when searching.
      When I search for "puppies"
      Then I should see "dog"
    
      
    Scenario: Don't see unrelated words when searching.
      When I search for "dachshund"
      Then I should NOT see "fish"
    

    Update step_defs.rb
    Before:

    When("I search for {string}") do |string|
      visit "/"
      fill_in "q", with: string
      click_on "Google Search", match: :first
    end
    
    Then("I should see {string}") do |string|
      page.should have_content(string)
    end
    
    Then("I should NOT see {string}") do |string|
      page.should_not have_content(string)
    end
    

    After:

    Given('I accept cookies') do
      visit "/"
      click_on "Accept all"
    end
    
    When("I search for {string}") do |string|
      visit "/"
      fill_in "q", with: string
      click_on "Google Search", match: :first
    end
    
    Then("I should see {string}") do |string|
      page.should have_content(string)
    end
    
    Then("I should NOT see {string}") do |string|
      page.should_not have_content(string)
    end
    
    # a handy debugging step you can use in your scenarios to invoke 'pry' mid-scenario and poke around
    When('I debug') do
      binding.pry
    end
    

    Update Dockerfile
    Before:

    #start with the base ruby image
    FROM ruby
    
    #make sure we have a folder called /app
    RUN mkdir /app
    
    #cd into our app folder each time we start up
    WORKDIR /app
    
    #copy our Gemfile and Gemfile.lock
    COPY Gemfile* /app/
    
    #install the gems
    RUN bundle
    
    CMD cucumber
    

    After:

    #start with the base ruby image
    FROM ruby
    
    #always a good idea to update and have an editor
    RUN apt-get update; apt-get install -y vim
    
    #make sure we have a folder called /app
    RUN mkdir /app
    
    #cd into our app folder each time we start up
    WORKDIR /app
    
    #copy our Gemfile and Gemfile.lock
    COPY Gemfile* /app/
    
    #install the gems
    RUN bundle
    
    CMD cucumber
    

    Update Gemfile
    Before:

    source 'https://rubygems.org'
    
    gem 'cucumber'
    gem 'capybara'
    #gem 'capybara-cucumber'
    gem 'capybara-screenshot'
    gem 'pry'
    gem 'rspec'
    gem 'selenium-webdriver'
    

    After:

    source 'https://rubygems.org'
    
    gem 'cucumber'
    gem 'capybara'
    gem 'capybara-screenshot'
    gem 'pry'
    gem 'rspec'
    #gem 'selenium-webdriver', '3.142.7' # Haven't been able to make 4.x work, yet...
    gem 'selenium-webdriver', '~> 4.4.0'
    

    Update docker-compose.yml
    Before:

    version: '3'
    services:
      browser:
        # See inside via VNC with the "debug" version
        image: selenium/standalone-chrome-debug
        
        # Slightly faster headless version
        #image: selenium/standalone-chrome    
        
        ports:
          - "5900:5900" #for VNC access
          - "4444:4444" #for webdriver access
    
      ruby:
        build: .
        volumes:
          - .:/app
        depends_on: 
          - browser
    

    After:

    version: '3'
    services:
      browser:
        
        #use for Apple Silicon
        #image: seleniarm/standalone-chromium
    
        # Chrome is crashing for me so I'm using firefox for now
        image: seleniarm/standalone-firefox
        
        ports:
          - "5900:5900" #for VNC access
          - "4444:4444" #for webdriver access
          - "7900:7900" #for web VNC access
    
      ruby:
        build: .
        volumes:
          - .:/app
        depends_on: 
          - browser
        environment: 
          - SELENIUM_HOST=browser
          - SELENIUM_PORT=4444