Capybara - visit() not working with firefox - 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.

Related

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

Capybara app_host with colon won't work with Firefox

I'm having a very odd issue with some capybara/selenium tests. For context I'm running ruby-rspec with selenium and capybara. My test work just fine when I run them on Chrome, but for some reason with Firefox, the 2nd URL fails.
spec_helper.rb
BROWSER = :chrome
BROWSER = :firefox
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, browser: BROWSER)
end
Capybara.default_driver = :selenium
# This one works
Capybara.app_host = "https://bar.foo.com/home"
# This one doesn't
Capybara.app_host = "https://foo.bar.com:18661/home"
The error I get:
Selenium::WebDriver::Error::WebDriverError:
WebDriverError#chrome://marionette/content/error.js:172:5
InsecureCertificateError#chrome://marionette/content/error.js:291:5
handleReadyState#chrome://marionette/content/listener.js:274:21
handleEvent#chrome://marionette/content/listener.js:247:9

Capybara Selenium Firefox untrusted issuer screen

Is there a way to bypass the untrusted issuer screen that appears on Firefox? I'm struggling to follow with geckodriver etc. It's changed a lot since the last time I needed Firefox in Selenium.
My env file has the following:
Capybara.default_driver = :selenium
Capybara.register_driver :selenium do |app|
opts = Selenium::WebDriver::Firefox::Options.new
opts.add_argument('acceptInsecureCerts')
Capybara::Selenium::Driver.new(app, browser: :firefox, options: opts)
end
Capybara.default_driver = :selenium
I'm using Firefox 54
selenium webdriver 3.4.3
geckodriver 0.17
I think I'm using options wrong but I can't find the right solution.
You need to specify accept_insecure_certs in the desired capabilities. The following should do it for you
Capybara.register_driver :selenium do |app|
caps = Selenium::WebDriver::Remote::Capabilities.new(accept_insecure_certs: true)
Capybara::Selenium::Driver.new(
app,
browser: :firefox,
desired_capabilities: caps
)
end

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"

How do I run chromedriver free of any extensions?

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.

Resources