Cucumber + Capybara - HTTP request path is empty in Firefox/Chrome - ruby

in env.rb, I have this:
if ENV['BROWSER']
Capybara.default_driver = :selenium
else
# DEFAULT: headless tests with poltergeist/PhantomJS
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(
app,
window_size: [1280, 1024] #,
#debug: true
)
end
Capybara.default_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
end
Tests run fine in poltergeist, but if I try to run in firefox, a browser opens, nothing happens, and then the test fails with the first visit statement.
HTTP request path is empty (ArgumentError)
What's wrong with my cucumber/capybara setup? Is there something more I need to do to run tests in a real browser?

In env.rb
ENV['no_proxy'] = '127.0.0.1'

Related

Headless mode in cucumber capybara ruby automation

I have seeing a lot of similar question, but non of them have help me to understand.
how to run my .feature file in headless mode using chromedrive, selenium, capybara, ruby and cucumber.
this is my env.rb:
require_relative 'helper.rb'
BROWSER = ENV['BROWSER']
World(Helper)
Capybara.register_driver :selenium do |app|
if BROWSER.eql?('chrome_headless')
Capybara::Selenium::Driver.new(app,
:browser => :chrome,
:desired_capabilities => Selenium::WebDriver::Remote::Capabilities.chrome(
'chromeOptions' => {
'args' => [ "headless", "window-size=1440x768", "disable-gpu"]
}
)
)
elsif BROWSER.eql?('chrome')
Capybara::Selenium::Driver.new(app,browser: :chrome)
end
end
Capybara.configure do |config|
config.default_driver =:selenium
Capybara.page.driver.browser.manage.window.resize_to(1440,768)
end
Capybara.default_max_wait_time = 60
this is my helper.rb:
module Helper
def take picture(file_name, res)
file_path = "reports/screenshot/"
dateTime = DateTime.now.to_s
dateTime.split(':')
date = dataHora[0..12].to_s+dataHora[14..15].to_s+dataHora[17..21].to_s
picture = "#{file_path}#{date}#{nome_arquivo}-#{res}.png"
temp_shot = page.save_screenshot(picture)
shot = Base64.encode64(File.open(temp_shot, "rb").read)
attach(shot, 'image/png')
end
end
this is my step_definition file .rb
just a normal rb file containing capybara elements
adm = LoginAdmin.new
admInic = TelaInicialAdmin.new
varGlobal = YAML.load(File.read('./configuracoesGlobaisTeste.yaml'))
#nomeOferta = varGlobal["nomeOferta"]
#nomeProduto = varGlobal["nomeProduto"]
Dado('que acessei a página da Vivo') do
acesso.load
end
Quando('clicar em Entrar posso digitar minhas credenciais ') do
page.driver.browser.navigate.refresh
sleep 3
inicio.btnEntrar.click
inicio.usuario.set #usuario
inicio.senha.set #senha
inicio.btnAcessarConta.click
end
is there missing something?
this is how i run the automation on terminal using the tag in my feature file:
cucumber -t#criarClienteCompraLojaClone
this command opens a browser using a GUI. i wish not open GUI. I wish to perform a headless test.
i have just try every tutorial in stackoverflow and google.
Please any suggestion will help!
My headless works updating the env.rb file for this:
require_relative 'helper.rb'
BROWSER = ENV['BROWSER']
#HEADLESS
World(Helper)
Capybara.register_driver :headless_chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome,
options: Selenium::WebDriver::Chrome::Options.new(args: %w[headless no-sandbox disable-gpu]))
end
Capybara.configure do |config|
config.default_driver =:headless_chrome
#Capybara.page.driver.browser.manage.window.resize_to(1440,768)
end
Capybara.default_max_wait_time = 60

Browser is launching for headless chrome capybara with Selenium webdriver

I'm trying to launch chrome in headless but the browser keeps on launching. Tried several different ways
used chrome options and added arguments
used chrome capabilities as well
My chrome version: 86
OS - ubuntu
capybara - 3.32.2
First Helper file:
spec_helper.rb
Capybara.register_driver :headless_chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome,
options: Selenium::WebDriver::Chrome::Options.new(args: %w[headless no-sandbox disable-gpu]))
end
Capybara.default_driver = :headless_chrome
Capybara.javascript_driver = :headless_chrome
Second try of Helper file:
Capybara.register_driver :headless_chrome do |app|
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
desired_capabilities:Selenium::WebDriver::Remote::Capabilities.chrome(
chromeOptions: {
args: %w[headless disable-gpu disable-popup-blocking no-sandbox]
}
)
)
end
Capybara.default_driver = :headless_chrome
Capybara.javascript_driver = :headless_chrome
Thanks in advance
Simplest way is to not register your own driver and just use the one provided by Capybara
Capybara.default_driver = :selenium_chrome_headless
Capybara.javascript_driver = :selenium_chrome_headless
Note - this will only work if you're not using rails system tests (no indication in your question that you're using rails though) since they override these settings - see the rails/rspec system test docs (driven_by) for that.
Resolved this by adding chromeOptions separately and had to remove -- from all arguments
Apparently, for Selenium >= 3.8, you have to specify goog:chromeOptions instead of chromeOptions.
So in your example:
...
Selenium::WebDriver::Remote::Capabilities.chrome(
"goog:chromeOptions": {
args: %w[headless disable-gpu disable-popup-blocking no-sandbox]
}
)
)
I was experiencing the same issue and this solved it for me.

Changing default drivers using Capybara per scenario

I have written a small block that will launch Google Chrome when I set a tag #chrome above a scenario within a feature file.
Before ('#chrome') do
Capybara.default_driver = :selenium
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app,
:browser => :chrome,
desired_capabilities: {
'chromeOptions' => {
'args' => %w{ window-size=1920,1080 }
}
}
)
end
end
The problem I have is that all subsequent scenarios within the feature file will also run in Chrome, even when the tag isn't set for them.
Is there a way to add to it to say revert back to Poltergeist once I'm done. I've tried the following but it didn't work:
After do |scenario|
if #chrome == true
Capybara.register_driver = :poltergeist
Capybara::Poltergeist::Driver.new(
app,
phantomjs_options: ['--ignore-ssl-errors=yes', '--ssl-protocol=TLSv1'],
window_size: [1280, 1024],
js_errors: false,
debug: false
)
end
end
Thanks in advance for any help you might have
Capybara already provides this behavior here . To use it you just need to require it and register a driver with the tag name you want to use. This would usually be in your env.rb/custom_env.rb
require 'capybara/cucumber'
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app,
browser: :chrome,
...
)
end
The important thing is the name you use to register the driver has to match the tag used (in this case #chrome). It also shows you shouldn't be changing Capybara.default_driver on a test by test basis, that's what Capybara.current_driver is for. You also shouldn't be registering a new driver every scenario, register_driver is meant to be called once for each driver configuration you will need and then referenced by name later.

First test suite failing with allure-rspec

I'm working on integrating allure into my tests but have come across a problem with the first test suite failing and returning a undefined method `find_element' for nil:NilClass error with every test in the suite. The tests run as expected when I'm not using allure and the same thing happens regardless of what suite is run.
I've had a look into it and it seems the before and after hooks in my spec helper aren't being used in the failed tests. Any ideas what could be causing this?
require 'rspec'
require 'selenium-webdriver'
require 'allure-rspec'
require 'nokogiri'
require 'uuid'
require 'pathname'
RSpec.configure do |c|
c.include AllureRSpec::Adaptor
c.expect_with :rspec do |c|
### Enable both should and expect syntax ###
c.syntax = [:should, :expect]
end
c.before(:all) do
#Start the Phantom-js driver before running any headless tests
case ENV['browser']
when 'headless'
## Run in command line before a headless test: phantomjs --webdriver=8001
system('start cmd /k phantomjs --webdriver=8001')
sleep 3
end
end
c.before(:each) do
#Find the browser to the used and set the driver to the appropriate one
case ENV['browser']
when 'chrome'
#driver = Selenium::WebDriver.for :chrome
##driver = Selenium::WebDriver.for(:remote, url: 'http://localhost:5555/wd/hub', desired_capabilities: :chrome)
when 'ie'
#driver = Selenium::WebDriver.for :internet_explorer
##driver = Selenium::WebDriver.for(:remote, url: 'http://localhost:5555/wd/hub', desired_capabilities: :ie)
when 'firefox'
#driver = Selenium::WebDriver.for :firefox
##driver = Selenium::WebDriver.for(:remote, url: 'http://localhost:5555/wd/hub', desired_capabilities: :firefox)
when 'headless'
#driver = Selenium::WebDriver.for :remote, url: 'http://localhost:8001'
end
#Maximize the browser
#driver.manage.window.maximize
#driver.get ENV['base_url']
end
c.after(:each) do |c|
#Takes a screen shot if an exception is thrown and attaches it to the allure XML when running Rake tests
if c.exception != nil
$failure = true
c.attach_file("screenshot", File.new(#driver.save_screenshot(File.join(Dir.pwd, "test_reports/allure/#{UUID.new.generate}.png"))))
end
#driver.quit
end
end
AllureRSpec.configure do |c|
#Outputs the Allure XML
c.output_dir = "test_reports/allure"
end

Getting Cucumber/Capybara to close and open another browser after scenarios

Say I have 3 Scenario Outlines and I need to run scenario 1 with Firefox, close the browser, then run scenario 2 with Chrome, close the browser, then finally run scenario 3 with Firefox.
Is there any way to close/quit the browser in Cucumber/Capybara after each scenario?
So far I have registered two drivers, one for Firefox and one for Chrome:
Capybara.register_driver :selenium_firefox do |app|
Capybara::Driver::Selenium.new(app, :browser => :firefox)
end
Capybara.register_driver :selenium_chrome do |app|
Capybara::Driver::Selenium.new(app, :browser => :chrome)
end
Then in hooks.rb, I'm using a 'custom' tag "#alternate_browser"
Before('#alternate_browser') do
driver = Capybara.current_driver
if driver == :selenium_firefox
Capybara.current_driver = :selenium_chrome
else
Capybara.current_driver = :selenium_firefox
end
end
Is there a way to force the browser to close after each scenario?
EDIT: I tried:
page.evaluate_script("window.close()")
page.execute_script("window.close()")
But both statements had no effect.
This is what you should do.
Stop closing the windows.
Change to explicit browser tags (#firefox,#chrome)
I haven't tried it but this should work.
Capybara.register_driver :selenium_firefox do |app|
Capybara::Driver::Selenium.new(app, :browser => :firefox)
end
Capybara.register_driver :selenium_chrome do |app|
Capybara::Driver::Selenium.new(app, :browser => :chrome)
end
Before('#firefox') do
Capybara.current_driver = :selenium_firefox
end
Before('#chrome') do
Capybara.current_driver = :selenium_chrome
end

Resources