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?
Related
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
I am trying to set up environment to run my TC in different browsers. But currently TC are run normally just in Chrome browser, Firefox browser is opened but nothing happens (test scenarios are not run) when I am trying to run TC in FF, Opera, Safari browsers.
How to adjust my settings to be able to run TC successfully in all browsers?
Also is there a way to run TC in parallel?
Here is my rails_helper.rb file:
require 'test/unit'
require 'selenium-webdriver'
require 'capybara'
require 'rspec'
require "rails/all"
require 'capybara/rspec'
require "page-object"
require 'rspec/expectations'
require 'securerandom'
require '../Test_helpers/login_helper'
require 'capybara-screenshot/rspec'
require 'launchy'
RSpec.configure do |config|
config.include LoginHelper
config.include RSpec::Matchers
config.include Capybara::DSL
environment = 'firefox'
if environment =='chrome'
Selenium::WebDriver::Chrome.driver_path = '../Resources/chromedriver.exe'
Capybara.default_driver = :chrome
Capybara.register_driver :selenium do |app|
options = {
:js_errors => false,
:timeout => 360,
:debug => false,
:inspector => false,
}
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
elsif environment =='firefox'
Capybara.default_driver = :firefox
Capybara.register_driver :selenium do |app|
options = {
:js_errors => true,
:timeout => 360,
:debug => false,
:inspector => false,
}
Capybara::Selenium::Driver.new(app, :browser => :firefox)
end
elsif environment == 'safari'
Capybara.default_driver = :safari
Capybara.register_driver :safari do |app|
options = {
:js_errors => false,
:timeout => 360,
:debug => false,
:inspector => false,
}
Capybara::Selenium::Driver.new(app, :browser => :safari)
end
elsif environment == 'opera'
Capybara.default_driver = :opera
Capybara.register_driver :opera do |app|
options = {
:js_errors => false,
:timeout => 360,
:debug => false,
:inspector => false,
}
Capybara::Selenium::Driver.new(app, :browser => :opera)
end
elsif
Capybara.default_driver = :poltergeist
Capybara.register_driver :poltergeist do |app|
options = {
:js_errors => false,
:timeout => 360,
:debug => false,
:phantomjs_options => ['--load-images=no', '--disk-cache=false'],
:inspector => false,
}
Capybara::Poltergeist::Driver.new(app, options)
end
end
Capybara.save_path = "../Reports" # path where screenshots are saved
config.after { |example_group| CapybaraScreenshot.save_and_open_page_path if example_group.exception }
Capybara::Screenshot.register_filename_prefix_formatter(:rspec) do |example|
"screenshot_#{example.description.gsub(' ', '-').gsub(/^.*\/spec\//, '')}"
end
end
Capybara.default_max_wait_time = 15
If you happen to pass here looking for a way to run every test through many browsers, I wrote this (working!) (overly talkative) snippet based on Thomas Walpole's accepted answer:
Rspec.configure do |conf|
Capybara.register_driver :chrome do |mode|
Capybara::Selenium::Driver.new mode, :browser => :remote, :desired_capabilities => :chrome
end
Capybara.register_driver :firefox do |mode|
Capybara::Selenium::Driver.new mode, :browser => :remote, :desired_capabilities => :firefox
end
## Here we politely ask every example to run twice, for each browser we want
conf.around do |example|
puts '~> ' + example.metadata[:example_group][:full_description]
# avoid looping over Capybara.drivers as it will also contain "selenium" generic driver and "rack_test" basic (js-less) browser
[:chrome, :firefox].each do |browser|
Capybara.current_driver = browser
puts "~~> #{example.description} # #{browser}"
example.run
end
end
Capybara.default_driver = :chrome
end
The output ends being something like:
~> The contact page
~~> loads # chrome
~~> loads # firefox
.
Finished in 20.9 seconds (files took 2.69 seconds to load)
1 example, 0 failures
Obviously all puts lines can be removed / commented out, they're there just for sample purposes :)
After each ~~> line the said browser opens and runs the example.
Care must be taken, however, to remove :js => true from your examples, or else Capybara will force-run them against the default javascript_driver instead.
The options you're passing to the drivers you're registering are only valid for poltergeist. Rather than the big if/else you can just register all the drivers and then use the one you need.
RSpec.configure do |config|
config.include LoginHelper # You probably want to only include this for feature tests
config.include RSpec::Matchers # not needed if you use the standard capybara rspec setup by including capybara/rspec
config.include Capybara::DSL # not needed if you use the standard capybara rspec setup by including capybara/rspec
end
Capybara.save_path = "../Reports" # path where screenshots are saved
config.after { |example_group| CapybaraScreenshot.save_and_open_page_path if example_group.exception }
Capybara::Screenshot.register_filename_prefix_formatter(:rspec) do |example|
"screenshot_#{example.description.gsub(' ', '-').gsub(/^.*\/spec\//, '')}"
end
end
Selenium::WebDriver::Chrome.driver_path = '../Resources/chromedriver.exe'
Capybara.register_driver :selenium_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
Capybara.register_driver :selenium_safari do |app|
Capybara::Selenium::Driver.new(app, :browser => :safari)
end
Capybara.register_driver :selenium_opera do |app|
Capybara::Selenium::Driver.new(app, :browser => :opera)
end
Capybara.register_driver :poltergeist do |app|
options = {
:js_errors => false,
:timeout => 360,
:debug => false,
:phantomjs_options => ['--load-images=no', '--disk-cache=false'],
:inspector => false,
}
Capybara::Poltergeist::Driver.new(app, options)
end
Then set either Capybara.default_driver and/or Capybara.javascript_driver (depending on whether or not you run some tests with rack_test and tag tests that need JS) to the driver you want to use to run your tests.
Capybara.default_driver = :selenium_chrome
To run all your tests against multiple browsers it's normal to configure your CI system to run multiple times and just set a different driver (based on an environment variable for instance) for each run. If you need to run them all in one run you could make your RSpec tests all shared examples and then configure test blocks that include the shared examples but each set a different driver in a before block.
For parallel testing you can look at the parallel_tests gems - https://github.com/grosser/parallel_tests
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.
I am playing around with capybara/poltergeist perfect duo, but I am having trouble to properly debugging.
I was testing a simple script:
logger = Logger.new "./log/who-scored-com.log"
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, js_errors: false,
debug: true,
logger: logger)
end
browser = Capybara.current_session
browser.visit 'https://www.whoscored.com/LiveScores'
browser.save_page 'page.html'
I am expecting that the script grap the page normally and saves it, but the page is empty and this is returned:
`Capybara::Poltergeist::StatusFailError: Capybara::Poltergeist::StatusFailError
from /home/vagrant/local/ruby-2.3.0/lib/ruby/gems/2.3.0/gems/poltergeist-1.9.0/lib/capybara/poltergeist/browser.rb:351:in `command'
from /home/vagrant/local/ruby-2.3.0/lib/ruby/gems/2.3.0/gems/poltergeist-1.9.0/lib/capybara/poltergeist/browser.rb:34:in `visit'`
Now, this don't give me nothing about this error. I catch the exception and print it and it gives me:
"Request to 'https://www.whoscored.com/LiveScores' failed to reach server, check DNS and/or server status"
Even if I have no idea why the address do not respond for capybara (and any hint would be appreciate :) ) I don't understand why the :debug options used in configuration doesn't seem to give me no info
You have a couple of issues
the poltergeist logger option is defined as '(Object responding to puts) - The ruby 2.3.0 standard library Logger object doesnt respond to puts so it's not valid.
You're example doesn't have Capybara.current_driver = :poltergeist so I'm not sure if it is actually using the driver you're configuring there or a previously defined one (I would have expected an error on the Logger object if it was)
debug: true will add debugging for poltergeist to the log, but there is also debugging info from phantomjs. That is generated by passing phantomjs_options: ['--debug=true'], phantomjs_logger: <an IO object - again not a Logger object> to the driver
The error you're actually hitting is the connection being refused due to not being able to negotiate an ssl protocol - to fix it add the required ssl protocol as a phantomjs option - `phantomjs_options: ['--ssl-protocol=TLSv1.2']
I'd use something like the following in a standalone ruby script, adjust driver_options to suit your taste.
require 'capybara'
require 'capybara/poltergeist'
class NilLogger
def puts * ; end
end
def setup_session
driver_options = { js_errors: false,
logger: NilLogger.new,
phantomjs_logger: STDOUT,
phantomjs_options: ['--debug=true'],
debug: false }
Capybara.configure do |conf|
conf.run_server = false
conf.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new app, driver_options
end
conf.current_driver = :poltergeist
end
Capybara.current_session
end
browser = setup_session()
browser.visit 'https://www.whoscored.com/LiveScores'
browser.save_page 'page.html'
Have you tried
Capybara.register_driver :poltergeist_debug do |app|
Capybara::Poltergeist::Driver.new(app, :inspector => true)
end
Capybara.javascript_driver = :poltergeist_debug
as shown here?
I'm trying to set the environment for testing using Selenium and selenium-client gem.
I prefer unit test style over RSpec style of tests.
Do I have to build my own system for reporting then?
How can I add exception handling without having begin-rescue-end in each test? Is there any way to do that using mixins?
I'm not sure I understand what your question means in terms of reporting but the selenium-client gem handles both BDD and UnitTesting.
Below is code copied from the rubyforge page:
require "test/unit"
require "rubygems"
gem "selenium-client", ">=1.2.16"
require "selenium/client"
class ExampleTest < Test::Unit::TestCase
attr_reader :browser
def setup
#browser = Selenium::Client::Driver.new \
:host => "localhost",
:port => 4444,
:browser => "*firefox",
:url => "http://www.google.com",
:timeout_in_second => 60
browser.start_new_browser_session
end
def teardown
browser.close_current_browser_session
end
def test_page_search
browser.open "/"
assert_equal "Google", browser.title
browser.type "q", "Selenium seleniumhq"
browser.click "btnG", :wait_for => :page
assert_equal "Selenium seleniumhq - Google Search", browser.title
assert_equal "Selenium seleniumhq", browser.field("q")
assert browser.text?("seleniumhq.org")
assert browser.element?("link=Cached")
end
end
As for exception handling, UnitTesting handles the exceptions with an Error message.
That being said, I may have misunderstood your question.
Initial build of Extent is available for Ruby. You can view the sample here. Latest source is available at github.
Sample code:
# main extent instance
extent = RelevantCodes::ExtentReports.new('extent_ruby.html')
# extent-test
extent_test = extent.start_test('First', 'description string')
# logs
extent_test.log(:pass, 'step', 'details')
extent.end_test(extent_test)
# flush to write everything to html file
extent.flush