Capybara.current_driver = :chrome versus :selenium_chrome - ruby-on-rails

what are the advantages/pros/cons of using
Capybara.current_driver = :chrome
versus using
Capybara.current_driver = :selenium_chrome
To give a bit more context, I'm using capybara in a standalone ruby script.

They are just names that can be used to identify specific driver configurations so it all depends on what is registered as :chrome and :selenium_chrome. The latest Capybara (2.15.1) release provides a default registration for :selenium_chrome which is defined as
# Configure selenium-webdriver to use chrome as the browser
Capybara.register_driver :selenium_chrome do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
but that can be redefined in your code. There is no gem, that I know of, that provides a default registration of :chrome so if that's currently valid it would most likely be defined in your code (search for register_driver).

Related

Rspec and Chrome/headless flag

I've got headless Chrome and Chrome working for my Rspec tests. I want a flag to switch between the two so I can see the tests happen when I want and hide them when I don't. How can I implement something like:
rspec --headless
Right now I just have this secret tied to a .env var:
Capybara.javascript_driver = Rails.application.secrets.headless ? :headless_chrome : :chrome
in your rails_helper.rb you should create a statement like that:
RSpec.configure do |config|
config.before(:each) do
if ENV['HEADLESS'] == 'true'
Capybara.current_driver = :selenium_chrome_headless
else
Capybara.current_driver = :selenium_chrome
end
end
end
then send a variable when running specs
$ HEADLESS=true rspec ./spec/features
Well, overriding the env var works so that's something.
HEADLESS=true rspec
Hmm, I personally use this code to register driver:
Capybara.register_driver :chrome do |app|
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
chromeOptions: {
args: [
('headless' if ENV.fetch('HEADLESS', '1') == '1')
].compact
}
)
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
desired_capabilities: capabilities
)
end
then in .env you can set the variable to be HEADLESS or not by default and then if you want to overwrite it just type HEADLESS=0 rspec

How to use selenium and poltergeist together in RSpec?

I have a rich frontend in my application. Some of my tests not works well with poltergeist, because of animations and AJAX requests, but works fine with selenium.
How can i use them together in one project and in one test session?
If you're using the standard RSpec configuration with Capybara (require 'capybara/rspec') then you can override the normal driver that would be used for a given test with :driver metadata
it "should do something", driver: :selenium do
# will use the selenium driver for this test
end
it "should do something else", driver: :poltergeist do
# will use the poltergeist driver for this test
end
that could also be specified on the enclosing feature if you want the whole feature to use a specific driver
feature "blah balh", driver: :selenium do
# all scenarios here would use the selenium driver unless overridden with their own :driver metadata
I found solution.
Created macros in spec/support/selenium_macros.rb:
module SeleniumMacros
def use_selenium_webdriver
before(:all) do
Capybara.javascript_driver = :selenium
Capybara.current_driver = :selenium
end
after(:all) do
Capybara.current_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
end
end
end
spec/rails_helper.rb
RSpec.configure do |config|
config.extend SeleniumMacros, type: :feature # add macros for acceptance tests
using example
spec/features/example_feature_spec.rb
feature 'Add files to question' do
use_selenium_webdriver
this feature will be work with selenium, after it will be executed it activates poltergeist webdriver.
P.S. Sorry for my english.

application.js is not found in test mode of rails application which use poltergeist and capybara

i am trying to run a feature spec with poltergeist and capybara.i see
application javascript is not found error
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist
Capybara.default_selector = :css
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, :window_size => [1920, 1080], :phantomjs_logger => nil, :js_errors => false)
end
It sounds like your asset pipeline isn't working correctly in your test environment. This would have nothing to do with Capybara or your driver configuration. Check your config/environements/test.rb to make sure you haven't turned off serving of assets, and try deleting everything in public/assets to see if that will force the tests to rebuild the required assets.

Disabling JavaScript when using Poltergeist and Capybara's default driver

My setup is using poltergeist as the Capybara driver for all my tests, both JS and non-JS.
# spec/rails_helper.rb
require "capybara/poltergeist"
# ...
# ...
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, js_errors: true)
end
Capybara.configure do |config|
config.ignore_hidden_elements = true
Capybara.default_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
end
I have some tests where I confirm that certain features on my app are still working even with javascript disabled. For those tests, I of course disable javascript with js: false.
describe "accessibility" do
describe "JavaScript disabled", js: false do
before(:each) { visit root_path }
it "user can still log in" do
# ...
end
end
end
However, I'm noticing that these js:false tests still use JavaScript. I can confirm this by printing debug statements to the console log in JavaScript.
Is there a way to disable JavaScript when using poltergeist? Or is it always enabled? Is it even valid to use poltergeist as a non-JS driver?
Thanks!
No, there doesn't seem to be a way to use poltergeist without Javascript (unless you modify poltergeist yourself). According to this Github issue it would require support in phantomjs, which is available in a patch but not in master.

How to set the ignore_ssl_errors option for Capybara Webkit in spec_helper.rb

In my spec_helper file I have:
Capybara.javascript_driver = :webkit
capybara_webkit now has a ignore_ssl_errors option that I want to use. How do I specify that in my spec_helper?
Here's how to register the :webkit driver with the :ignore_ssl_errors option.
Capybara.register_driver :webkit do |app|
Capybara::Driver::Webkit.new(app, :ignore_ssl_errors => true)
end
As of writing (capybara-webkit 1.7.1), the configuration seems to have been simplified:
Capybara::Webkit.configure do |config|
config.ignore_ssl_errors
end
(source)
Somehow the above register_driver examples don't work with Capybara 1.1.4. The example below is taken from the capybara browser_spec.rb.
Capybara.register_driver :webkit_ignore_ssl do |app|
browser = Capybara::Webkit::Browser.new(Capybara::Webkit::Connection.new).tap do |browser|
browser.ignore_ssl_errors
end
Capybara::Webkit::Driver.new(app, :browser => browser)
end
Capybara.javascript_driver = :webkit_ignore_ssl
As #hjblok says, the interface has changed in recent versions of capybara-webkit. You can simplify the solution slightly:
Capybara.register_driver :webkit_ignore_ssl do |app|
Capybara::Webkit::Driver.new(app).tap {|d| d.browser.ignore_ssl_errors }
end
Capybara.javascript_driver = :webkit_ignore_ssl
When createing a new webkit Object you can use this to ignore the ssl errors
Capybara::Driver::Webkit.new({ :ignore_ssl_errors => true})

Resources