Issue with Selenium WebDrive with Docker image and Cucumber.io - ruby

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!

You need to create an instance of Selenium::WebDriver::Chrome::Options and pass it to the options argument. Add this chrome_options = Selenium::WebDriver::Chrome::Options.new in your code like below.
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
# Add options for the Chrome browser
chrome_options = Selenium::WebDriver::Chrome::Options.new
# Disable notifications
chrome_options.add_argument("--disable-notifications")
Capybara.register_driver :remote_chrome do |app|
Capybara::Selenium::Driver.new(app,
:browser => :remote,
:browser_name => :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

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

Related

Capybara using Selenium webdriver undefined method `visit' for #<RSpec::ExampleGroups

The following is my setup
just three files to start with. no folder structure
Gemfile
gem 'capybara'
gem 'selenium-webdriver'
spec_helper.rb
require 'capybara/rspec'
require "selenium/webdriver"
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
end
Capybara.register_driver :headless_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
Capybara.javascript_driver = :headless_chrome
run.rb
require_relative 'spec_helper'
describe "test process" do
it "checks google" do
visit("www.google.com")
puts "LAUNCHED"
end
end
New to testing. Any help would be appreciated.
I ran it using
rspec run.rb
By default Capybaras methods are only included in RSpec tests of type :feature and :system - https://github.com/teamcapybara/capybara/blob/master/lib/capybara/rspec.rb#L10
Tag your test with the correct type and the methods will be available
describe 'test proces', type: :feature do

Launching IE and Chrome browser using Capybara, Selenium, Ruby

I am new to Cucumber and Capybara. I am trying to launch IE and Chrome browser.
I have downloaded drivers of both and copied them to bin folder of Ruby in C drive.
I have set Path in Env var.
Below is my support/env.rb file code
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
require 'capybara/rspec'
require 'selenium-webdriver'
require 'capybara/cucumber'
Capybara.run_server = false
#Set default driver as Selenium
Capybara.default_driver = :selenium
#Set default selector as css
Capybara.default_selector = :css
#Syncronization related settings
module Helpers
def without_resynchronize
page.driver.options[:resynchronize] = false
yield
page.driver.options[:resynchronize] = true
end
end
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => chrome) #Getting error at this line
end
World(Capybara::DSL, Helpers)
I am getting compilation Error at line with comment is as below.
method calls where the number of arguments passed to the method does not match the number of method parameters.
Here is my environment:
cucumber (2.4.0)
selenium-webdriver (3.0.3)
capybara (2.11.0)
rspec (3.5.0)
Ruby 2.3
Capybara::Selenium::Driver.new(app, :browser => chrome) try changing it to Capybara::Selenium::Driver.new(app, :browser => :chrome)

Print all logs in console when running Selenium test with Capybara

I am using Capybara with Selenium driver for my acceptance tests
Versions used:
Selenium 2.53.1
Capybara 2.7.1
cucumber 2.2.0
cucumber-core 1.3.1
According to Selenium/Logging, known log types are browser, client, driver, performance and server. I would like to log all these log types in console output in realtime.
Based on what I understood, to enable full logging I am configuring Capybara as following:
Capybara.configure do |config|
config.run_server = false
config.app_host = ENV['APP_HOST'] # APP HOST from Cucumber.yml file
config.default_driver = :selenium
config.match = :prefer_exact
config.default_max_wait_time = 10
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(
app,
:browser => ENV['BROWSER'].downcase.to_sym,
:desired_capabilities => {
:loggingPrefs => {
:browser => "ALL",
:client => "ALL",
:driver => "ALL",
:server => "ALL"
}
}
)
end
end
Where ENV['BROWSER'] is 'Chrome'.
What I am still unclear is how to print the logs to console. I am looking for a way to print the logs for all scenarios.
I tried adding following code to hooks.rb
After do |scenario|
puts #browser.driver.manage.get_log(:browser)
puts #browser.driver.manage.get_log(:client)
puts #browser.driver.manage.get_log(:server)
puts #browser.driver.manage.get_log(:driver)
end
but #browser is nil.
Also tried following in hooks.rb
After do |scenario|
puts page.driver.browser.manage.logs.get(:browser)
end
But works only for browser logs.
Question: How do I print these logs after each scenario, or even better in realtime?

initialize': rack-test requires a rack application, but none was given (ArgumentError)

I keep getting this error while switching from Selenium over to PhantomJs/Poltergeist.
Anybody know what I'm doing wrong? If I switch out the driver to selenium, the script works perfectly. Whenever I comment out the default_driver = :selenium and replace with javascript_driver = :poltergeist I run into this error.
initialize': rack-test requires a rack application, but none was given (ArgumentError)
This is all in a ruby file, no rails.
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
require "open-uri"
# require "date"
# require 'active_support/core_ext/integer/inflections'
require 'capybara/poltergeist'
# require 'selenium-webdriver'
require 'pry'
require 'phantomjs'
# require 'database_cleaner'
Capybara.run_server = false
Capybara.javascript_driver = :poltergeist
# Capybara.default_driver = :selenium
Capybara.app_host = 'https://www.sameplsite.com'
module MyCapybaraTest
class Test
include Capybara::DSL
def login_site
visit('https://www.sameplsite.com')
# binding.pry
click_link('Log in')
fill_in('email', :with => 'joefrank#sharklasers.com')
fill_in('password', :with => 'passwordpassword')
check('checkbox_remember')
click_button('Log in')
end
def click_right_game
click_link('Create Contest')
all('.boxed')[1].click
check('Free practice')
click_link('Create 1 Head-to-Head')
save_and_open_page
end
def output_game_link
url = URI.parse(current_url)
puts url
end
end
end
t = MyCapybaraTest::Test.new
t.login_fanduel
t.click_right_game
t.output_game_link
Capybara.javascript_driver = :poltergeist doesn't switch the driver. If you want to switch driver , use Capybara.current_driver instead.
That says : Capybara.current_driver = :poltergeist

Guard + Spork slowing down tests, not receiving --drb option

I am developing a Rails project via a Vagrant box (Ubuntu 32-bit, Rails 4.0.3, Ruby 2.1.0p0).
I've just tried adding Spork to my project to speed up my tests (using RSpec, Capybara), and am seeing significantly slower tests. If I run simply "rspec .", all my tests pass in 5.83 seconds. However, when I run guard via "guard -c -p", I save one of my spec files, and I get a time of 26.08 seconds.
Note: I have to run "guard -p" to actually get guard to run my tests on file save through Vagrant.
When guard starts running the tests, it shows the args:
["--color", "--failure-exit-code", "2", "--format", "progress", "--format",
"Guard::RSpec::Formatter", "--require", "/home/vagrant/.rvm/gems/ruby-2.1.0/
gems/guard-rspec-4.2.7/lib/guard/rspec/formatter.rb", "spec"]...
I see that "--format" is listed twice, and "--drb" is not showing up at all.
Here's my Guardfile:
guard 'spork', :rspec_env => { 'RAILS_ENV' => 'test' }, :test_unit => false do
watch('config/application.rb')
watch('config/environment.rb')
watch('config/environments/test.rb')
watch(%r{^config/initializers/.+\.rb$})
watch('Gemfile.lock')
watch('spec/spec_helper.rb') { :rspec }
end
guard :rspec, :cmd => 'rspec --drb' do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
# Rails example
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
watch('config/routes.rb') { "spec/routing" }
watch('app/controllers/application_controller.rb') { "spec/controllers" }
end
guard 'livereload' do
watch(%r{app/views/.+\.(erb|haml|slim)$})
watch(%r{app/helpers/.+\.rb})
watch(%r{public/.+\.(css|js|html)})
watch(%r{config/locales/.+\.yml})
# Rails Assets Pipeline
watch(%r{(app|vendor)(/assets/\w+/(.+\.(css|js|html|png|jpg))).*}) { |m| "/assets/#{m[3]}" }
end
Here is my spec_helper.rb:
require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'
Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
# need to restart spork for it take effect.
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
#Devise
config.include Devise::TestHelpers, type: :controller
#Capybara
config.include Capybara::DSL
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
The only thing in my .rspec :
--color
The relevant part of my Gemfile:
group :development, :test do
gem 'rspec-rails', '~> 2.0'
gem 'factory_girl_rails'
gem 'guard-rspec'
gem 'guard-livereload'
gem 'spork-rails'
gem 'guard-spork'
end
group :test do
gem 'shoulda-matchers'
gem 'capybara'
end
I've noticed that if I have guard running, then save a file, at times I will get an error:
Could not start Spork server for rspec after 30 seconds. I will continue
waiting for a further 60 seconds.
I'm not sure why it's taking so long after installing Spork, especially when rspec is much faster going through the same Vagrant box. Any ideas?
There's a new --listen-on option since Guard 2.5: https://github.com/guard/guard/releases/tag/v2.5.0
From Guard's README:
Use Listen's network functionality to receive file change events from the network. This is most useful for virtual machines (e.g. Vagrant) which have problems firing native filesystem events on the guest OS.
Suggested use:
On the host OS, you need to listen to filesystem events and forward them to your VM using the listen script:
$ listen -f 127.0.0.1:4000
Remember to configure your VM to forward the appropriate ports, e.g. in Vagrantfile:
config.vm.network :forwarded_port, guest: 4000, host: 4000
Then, on your guest OS, listen to the network events but ensure you specify the host path
$ bundle exec guard -o '10.0.2.2:4000' -w '/projects/myproject'

Resources