How do I run chromedriver free of any extensions? - ruby

How do I set chromedriver to run without any extensions via Capybara env.rb?
Here is my env.rb
require 'capybara'
require 'capybara/cucumber'
require 'rspec'
require 'selenium/webdriver'
#require 'capybara/rails'
caps = Selenium::WebDriver::Remote::Capabilities.chrome #chrome|firefox
caps.version = "8"
caps.platform = :WINDOWS
Capybara.server_port = 3001
Capybara.app_host = "http://www.google.com"
Capybara.default_driver = :selenium
Capybara.ignore_hidden_elements = false
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app,
:browser => :chrome #chrome|firefox
#,
#:url => "localhost:4444/wd/hub",
#:desired_capabilities => caps
)
end
Thanks in advance for the assistance!

So the answer I think is what Andrey Botalov said.
In my case the problem is that I installed a viral software called Conduit. While posing as legitimate software, it is nearly impossible to remove without some sort of anti-virus.
What was happening is in each instance of lauching chromedriver to run a test, conduit would load NewTabAPI.js onto the new chromdriver. This in turn launched a crappy toolbar. In my case InternetHelper1.5.
At the time of kill NewTabAPI.js had multiple copies sitting in the \Users[user]\AppData\Local\Temp\scoped_directory_....
Removing these killed the virus, but I found a number of conduit directories sitting about that I felt prudent to remove as well.
Anyway, that's what I needed to do in my case. In most cases Andrey's comment should do just fine and answers my question.

Related

Why is capybara looking for "Mozilla geckodriver" when I have specified "chromedriver"?

I have an Rspec setup with a spec support file for Capybara that looks like this:
require 'capybara/rails'
require 'capybara/rspec'
require 'selenium-webdriver'
Capybara.configure do |config|
config.always_include_port = true
config.app_host = 'http://www.myteaspoon.pdev'
config.default_max_wait_time = 5
end
Capybara.register_driver(:chrome_headless) do |app|
options = Selenium::WebDriver::Chrome::Options.new(
args: %w[headless disable-gpu no-sandbox]
)
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
options: options
)
end
Capybara.default_driver = :chrome_headless
RSpec.configure do |config|
config.infer_spec_type_from_file_location!
end
However when I try to run some specs I get the following error:
Selenium::WebDriver::Error::WebDriverError: Unable to find Mozilla geckodriver. Please download the server from
https://github.com/mozilla/geckodriver/releases and place it somewhere on your PATH.
More info at https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver.
from /Users/nk/.rvm/gems/ruby-2.5.3#au/gems/selenium-webdriver-3.142.3/lib/selenium/webdriver/common/service.rb:136:in `binary_path'
What might be wrong and how do I debug it?
I have installed chromedriver using: brew cask install chromedriver
And it seems to be in path:
$ chromedriver -v
ChromeDriver 76.0.3809.126 (d80a294506b4c9d18015e755cee48f953ddc3f2f-refs/branch-heads/3809#{#1024})
When you require capybara/rspec a hook is installed that sets the current driver to the value of Capybara.javascript_driver for any tests with :js metadata - https://github.com/teamcapybara/capybara/blob/master/lib/capybara/rspec.rb#L23 -. If you want all tests (with and without :js metadata) to use the same driver then you need to set Capybara.default_driver = Capybara.javascript_driver = :chrome_headless

Chrome binary not found on Heroku with Selenium for Ruby on Rails

Two weeks ago, I managed to have a working environment on Heroku, combining Capybara, Selenium, Chromedriver and Chrome for web scraping. However, one week ago I must have changed something, which causes the setup to crash due to the Chrome binary not being found.
WARN: Selenium::WebDriver::Error::UnknownError: unknown error: cannot find Chrome binary (Driver info: chromedriver=2.40.565383 (76257d1ab79276b2d53ee976b2c3e3b9f335cde7),platform=Linux 4.4.0-1019-aws x86_64)
I am using the two relevant buildpacks on the Heroku-14 Stack
https://github.com/heroku/heroku-buildpack-xvfb-google-chrome
https://github.com/heroku/heroku-buildpack-chromedriver
Used gems:
gem 'selenium-webdriver','>=3.6.0'
gem 'chromedriver-helper'
I've spent the weekend trying to get this to work by passing various paths directly into the capybara.rb initializer (and compared these by running heroku run bash), but could not get it working.
capybara.rb
require "selenium/webdriver"
chrome_bin = ENV.fetch('GOOGLE_CHROME_SHIM', nil)
chrome_opts = chrome_bin ? { "chromeOptions" => { "binary" => 'app/.apt/usr/bin/google-chrome-stable' } } : {}
puts chrome_opts.to_s
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
desired_capabilities: Selenium::WebDriver::Remote::Capabilities.chrome(chrome_opts)
)
end
Capybara.default_driver = :chrome
Capybara.javascript_driver = :chrome
I have also set the ENV vars in Heroku via the interface but when checking with ENV via heroku run rails c, it seems that the BIN var is loaded from the buildpack, and overrides my configuration.
I set GOOGLE_CHROME_BIN and GOOGLE_CHROME_SHIM to: /app/.apt/usr/bin/google-chrome
I'm no sure what kind of changes I have to make to get it working again. There are quite a few puzzle pieces, which one do I need to fix? Suggestions welcome!
SOLVED:
require "selenium/webdriver"
chrome_bin = ENV.fetch('GOOGLE_CHROME_SHIM', nil)
Capybara.register_driver :chrome do |app|
browser_options = ::Selenium::WebDriver::Chrome::Options.new
browser_options.binary = chrome_bin
Capybara::Selenium::Driver.new(app, browser: :chrome, options: browser_options)
end
Capybara.default_driver = :chrome
Capybara.javascript_driver = :chrome
I'm guessing you upgraded to the latest selenium-webdriver and chromedriver in the last few weeks. chromeOptions is no longer a valid key to pass, you can try changing it to goog:chromeOptions but you really should just be using an instance of the Selenium::WebDriver::Chrome::Options class
Capybara.register_driver :chrome do |app|
options = ::Selenium::WebDriver::Chrome::Options.new
options.binary = ...
Capybara::Selenium::Driver.new(app, browser: :chrome, options: browser_options)
end

Ruby Capybara::ExpectationNotMet: Timed out waiting for Selenium session reset Failure/Error

I am continually getting flakey errors on CI where my Selenium session is not restarting. I'm not having this issue when I run tests locally. It only ever happens on my CI server.
I'm using Capybara 2.18.0, rspec 3.7.0, slenium-webdriver 3.9.0 and site_prism 2.13. Is there
Capybara::ExpectationNotMet: Timed out waiting for Selenium session reset
Failure/Error: raise Capybara::ExpectationNotMet.new('Timed out waiting for Selenium session reset') if (Capybara::Helpers.monotonic_time - start_time) >= 10
Capybara::ExpectationNotMet:
Timed out waiting for Selenium session reset
C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/capybara-2.18.0/lib/capybara/selenium/driver.rb:145:in `reset!'
C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/capybara-2.18.0/lib/capybara/session.rb:127:in `reset!'
C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/capybara-2.18.0/lib/capybara.rb:314:in `block in reset_sessions!'
my spec_helper.rb looks like this:
require 'rspec'
require 'capybara/rspec'
require 'capybara-screenshot/rspec'
require 'capybara/dsl'
require 'selenium-webdriver'
require 'site_prism'
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
end
Capybara.save_path = "#{Dir.pwd}/screenshots"
Capybara.default_driver = :selenium
Capybara.app_host = ENV['url']
if ENV['timeout']
Capybara.default_max_wait_time = ENV['timeout'].to_i
else
Capybara.default_max_wait_time = 15
end
RSpec.configure do |config|
config.full_backtrace = true
config.before(:each) do
config.include Capybara::DSL
end
config.after(:each) do |example|
if example.exception
name = example.example_group.to_s
name.slice!('RSpec::ExampleGroups::')
name.gsub!('::', '#')
whole_page.save_screenshot("#{name}##{example.description.tr(' ', '_')}-#{Time.now.strftime('%H_%M_%S')}.png")
end
end
end
When Capybara resets the driver, it tells the browser to go to about:blank and then waits up to 10 seconds for there to be no elements on the page matching CSS '/html/body/*'. In your case that isn't happening within 10 seconds.
One reason for this could be you having onunload handlers that are doing something that could take longer than 10 seconds on your CI hardware (or opening alert messages, etc)?? If that's the case a workaround is to have the test visit a page that doesn't have an onunload handler and check for something visible on that page at the end of the test (potentially in an after block to keep the test clean). Another thing to verify is that the versions of Chrome and chromedriver are the same between local and CI.

Launch different browsers using Capybara/Cucumber

I wish to be able to run my tests against different browsers. I have written the following method to do this and this is in my env file.
def startbrowser()
if BROWSER == "ff"
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :firefox )
end
else
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome )
end
end
session = startbrowser()
session.visit(#base_url)
The above should launch firefox if ff is supplied but should default to chrome as this is the browser I use for most of my testing. So the command I would use in the terminal would be: cucumber --tags #tests BROWSER=ff.
However this does not work. It does not give me an error, but it always launches firefox even if I dont supply 'BROWSER = ff' part. In theory it should default to chrome. I can successfully launch chrome browser if I don't have the command in the method, but I wish to be able to switch between browsers and run different jobs from jenkins. Anybody got any idea what im doing wrong here?
Thanks!
The problem is that you're trying to access an environment variable incorrectly. You should change the following line:
if BROWSER == "ff"
...to...
if ENV['BROWSER'] == "ff"

Capybara - visit() not working with firefox

I'm a noob in cucumber and capybara, so please bear with me.
I'm trying to setup a cucumber project that uses chrome and firefox as the test platform. So far, I have got the test to work on chrome, but not on firefox.
Below is the code snippet:
require 'uri'
require 'net/http'
require 'fileutils'
require 'selenium-webdriver'
require File.dirname(__FILE__) + '/throttle.rb'
#CAPYBARA
require 'capybara/cucumber'
require 'capybara/session'
#require 'capybara-webkit'
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
Capybara.register_driver :selenium_firefox do |app|
Capybara::Selenium::Driver.new(app, :browser => :firefox)
end
driver = case ENV['BROWSER']
when 'chrome'
:chrome
when 'firefox'
:selenium_firefox
when 'webkit'
:webkit
when 'ie'
:internet_explorer
else
:chrome
end
Capybara.default_driver = driver
Capybara.javascript_driver = :selenium_firefox
Capybara.run_server = false
Capybara.default_selector = :css
Then, for the test, I just simply did a visit("http://www.google.com").
If I set my browser parameter to chrome, it worked fine. Cucumber openned up chrome and automatically typed in "www.google.com", and the page loaded without a problem.
However, the moment I set it to firefox: $cucumber features/test.feature BROWSER=firefox, it didn't work. It did open up the firefox browser, but nothing was being automatically typed into the URL box.
So, I must be missing something in the setup process that I'm not aware of. I have been looking for solutions on the web (require 'selenium webdriver', put 'selenium webdriver' into my Gemfiles, etc.), but so far none has worked for me.
What am I missing here? How come visit() didn't automatically populate the URL box of firefox, but it did just fine with chrome?
Thank you for your help.

Resources