Launch different browsers using Capybara/Cucumber - ruby

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"

Related

Unprocessable entity using Capybara, Rspec, Selenium-webdriver

Issue:
I am not able to login a web application using Capybara, rspec and selenium webdriver.
I am able to fill the username and password to the respective fields, but when i tried to click on login button, application is not logging in. Instead the application returns 'Unprocessable entity' (tried enabling and disabling the cookies). Using Headless chromium 64 Linux.
lib/abcd.rb
def click_login user, password
visit "https://www.******.com/users/sign_in"
fill_in 'user[email]', :with => user
fill_in 'user[password]', :with => password
click_button 'Login'
end
Test case:
require_relative 'lib/*****.rb'
describe 'Visit Websites', type: :feature, driver: :selenium_chrome_headless do
it "TC001_Test case 1" do
click_login "user#account.com", "password123"
expect(page).to have_title "Welcome to home page"
end
output:// application stays in the same page
it "TC002_Test case 2" do
find(:xpath,".account menu").click
expect(page).to have_title "Account details page"
end
driver setup - Approach 1
def setup_driver
Capybara.register_driver :selenium_chrome_headless do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome, options: driver_options, :driver_path => 'bin/chromedriver')
end
Capybara.configure do |config|
config.run_server = false
config.default_driver = :selenium_chrome_headless
end
end
def driver_options
options = Selenium::WebDriver::Chrome::Options.new(binary: 'bin/headless-chromium')
arguments = %w[--headless --disable-gpu --window-size=1280x1696
--disable-application-cache --disable-infobars --no-sandbox
--hide-scrollbars --enable-logging --log-level=0
--single-process --ignore-certificate-errors --homedir=/tmp]
arguments.each do |argument|
options.add_argument(argument)
end
options
end
driver setup - Approach 2
def setup_driver
Capybara.register_driver :selenium_chrome_headless do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome, options: driver_options, :driver_path => 'bin/chromedriver')
end
Capybara.configure do |config|
config.run_server = false
config.default_driver = :selenium_chrome_headless
end
end
def driver_options
options = Selenium::WebDriver::Chrome::Options.new(binary: 'bin/headless-chromium')
arguments = %w[--headless --disable-gpu --window-size=1280x1696
--disable-application-cache --disable-infobars --no-sandbox
--hide-scrollbars --enable-logging --log-level=0
--single-process --ignore-certificate-errors --homedir=/tmp]
arguments.each do |argument|
options.add_argument(argument)
end
options
end
You haven't explained exactly what you're trying to do here (test a local rails app, test a remote app, etc) but overall everything sounds like it is working exactly as expected in a default setup. By default every RSpec test is expected to be fully independent of every other test (you should be able to run any test singly and/or run them all in a random order), so everything gets reset between each test (it block). This means you would need to log in for every test (before blocks can help DRY that up). Also feature tests are different from normal unit tests, you should be testing whole user flows/behaviors rather than just single expectations - otherwise your tests will end up taking hours to run.
For the login issue it sounds like you haven't actually created the account in the system you're testing. If this is a local app you're testing, where you have direct connectivity to the DB you should look into fixtures or factories to help with setting up the correct state for a test. Check your logs to confirm but my guess is you'll see the login is actually failing due to invalid username/password combo.

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 cucumber tests on multiple browsers

I am using windows 10 32 bit ruby version 233, I am facing these issues with running the tests on a ie 11 browser and the chrome browser for the tests(using page object) that are already running on firefox browser,
ISSUE with IE: Watir::Exception::NoMatchingWindowFoundException:
browser window was closed (eval):1:in `process_watir_call'
ISSUE with Chrome: Errno::ECONNREFUSED: Failed to open TCP connection
to 127.0.0.1:9515 (No connection could be made because the target
machine actively refused it. - connect(2) for "127.0.0.1" port 9515)
i have used the following hooks for ie and chrome:
Before do
case $browser
when 'mozilla'
#browser = Watir::Browser.new :firefox
#browser.window.maximize
when 'chrome'
#browser = Watir::Browser.new :chrome, :profile => "default"
when 'ie'
#browser = Watir::Browser.new :ie
# #browser.window.maximize
# #browser.visible = true
else
#browser = Watir::Browser.new :firefox
#browser.window.maximize
end
# #browser = Watir::Browser.new :firefox
# #browser.window.maximize
# this file contains test data that needs to be changed if tests are being executed in a different environment
$test_data = YAML.load_file('features/support/input_data/data/login_information.yml')
# this file contains base URL that needs to be changed if tests are being executed in a different environment
FigNewton.load('default.yml')
end
I was able to run a small sample test on a separate project from ruby mine with Watir gem.
Is there any way to make it work on the existing firefox tests?
The problem with chrome has been fixed by using the right chromedriver version, but still having problems with the IE using watir.
Found the solution for IE too its something to do with the internet options security and lower down the security level and uncheck the Enable Protected Mode.

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.

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