Capybara Poltergeist - Site shows message to enable cookies - capybara

I am running Capybara 1.1.2 with Poltergeist 1.0.1 for headless testing. The test runs fine with selenium driver. However with poltergeist, the site snapshot states that cookies are disabled. Am I missing any config. Shouldn't cookies be enabled by default?

You can make sure that the cookies are enabled in Capybara before setting the session.
Capybara.register_driver :my_firefox_driver do |app|
Capybara::Poltergeist::Driver.new(app, {:js_errors => false,
:cookies => true, :window_size => [1280, 1024]})
end
self.session = Capybara::Session.new(:my_firefox_driver)

Related

Capybara proxy turn on/off while testing

I am using capybara, selenium and geckodriver.
I know that we can configure selenium to launch Firefox with a proxy using a code such as below:
Capybara.register_driver :selenium do |app|
proxy = Selenium::WebDriver::Proxy.new(http: "127.0.0.1:9999", ssl: "127.0.0.1:9999")
desired_caps = Selenium::WebDriver::Remote::Capabilities.firefox({
firefox_profile: "default",
proxy: proxy
})
Capybara::Selenium::Driver.new(app, {:browser => :firefox, :desired_capabilities => desired_caps})
end
Capybara.current_driver = :selenium
#session = Capybara::Session.new(:selenium)
This starts a Firefox instance with the proxy already configured.
Is there a way to turn on/off the proxy after Capybara has started its tests? (ie: I visit url www.xyz.com with proxy. And then, without closing the firefox instance, turn off the proxy, and visit www.abc.com. After that, turn on the proxy and visit www.123.com)
I saw an SO answer that is very close to what I am looking for, instantiate capybara browser and set a proxy, but comments show that it no longer works.
Refs: https://github.com/SeleniumHQ/selenium/wiki/Ruby-Bindings

Capybara selenium_chrome going to example.com instead of localhost

I am setting up Cucumber tests in a Rails project. Everything works fine when I use the default driver; but, when I try to use the :selenium_chrome driver, the browser tries to load example.com instead of the local Rails server. Any idea what I'm missing?
My steps look like this:
Before do |scenario|
Capybara.current_driver = :selenium_chrome
end
When(/^I visit the posts page$/) do
visit posts_url
end
When I run the features, I can see that the rails server gets launched:
Using the default profile...
Feature: Posts
Capybara starting Puma...
* Version 3.12.0 , codename: Llamas in Pajamas
* Min threads: 0, max threads: 4
* Listening on tcp://127.0.0.1:62056
But, the Chrome window that pops up is attempting to access http://www.example.com/posts instead of http://127.0.0.1:62056/posts
Am I missing a configuration step somewhere?
On a related note: If I want to run all my tests using Selenium, should I have to put the Capybara.current_driver line in a Before block? I tried just adding it to features/support/env.rb, but it didn't seem to have any effect.
I have Chrome 73.0.3683.86 and Rails 5.2.2 running on MacOS 10.14.4.
If you want to use :selenium_chrome as the default driver, you can set Capybara.default_driver = :selenium_chrome.
As to the example.com issue that's because you're visiting posts_url and have your Rails default hostname set to be example.com in your test environment. You can either visit posts_path which will allow Capybara to default the hostname to localhost - or update your test environment config so the url helpers produce the urls you expect.
You need to set Capybara app_host config eg Capybara.app_host = .... See full docs here
Generally, you set Capybara config inside spec_helper.rb so that it's enabled in all specs eg:
Capybara.configure do |config|
config.current_driver = :selenium_chrome
config.app_host = ...
end
Hope that answers your questions?

How can I run headless browser system tests in Rails 5.1?

The documentation for Rails 5.1 system tests is a bit sparse. I'm unable to get headless tests that execute javascript running. I think one approach entails installing and running xvf. But this is more manual setup than I'm used to when running capybara in other versions of rails.
What's the most straightforward way to achieve this?
In Rails 5.1 system tests the driver used is set by the driven_by call in ApplicationSystemTestCase (test/application_system_test_case.rb). Assuming you have registered your capybara-webkit driver as 'webkit' you should be able to do
driven_by :webkit
Another potential option if you use Chrome 59+ on linux/mac is to use headless chrome
Capybara.register_driver :headless_chrome do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome, :args => ['headless'])
end
and then in your test case class
driven_by :headless_chrome
That gives you a headless version of chrome so none of the issues of capybara-webkit/poltergeist not supporting current web standards. Unfortunately currently chromedriver has issues with JS system modals (alert, confirm, prompt - workaround in capybara master branch) and hangs if you attempt to close windows during your tests. Hopefully those 2 issues will be fixed soon.
Also note that rails 5.1 should have removed the need for database_cleaner for most peoples testing since it already handles sharing of a single database connection between multiple threads in test mode.

Cannot specify no_proxy for Capybara with poltergeist

I there, I am a bit stuck with this...
I can't find documentation on how to tell Capybara/Poltergeist with phantomjs to avoid the proxy and go directly to the specified IP.
Host machine (Fedora 20) config? Already tried setting no_proxy
Or, Application config?
A bit of context:
I am making a few web tests using Capybara with Poltergeist/PhantomJS. Unfortunately, my company decided that any request for an internal network that hits the proxy won't be forwarded anymore. So, since I am testing between two different machines in the company's intranet, putting the env variable no_proxy should be enough. But no...
Well... after digging a bit more on the PhantomJS github API Reference page I found the answer.
There is an option --proxy-type=[http|socks5|none] that can be passed to phantomjs command line or in the initialization of Capybara with poltergeist like this:
Capybara.run_server = false
Capybara.javascript_driver = :poltergeist
Capybara.default_wait_time = 60
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, { :phantomjs_options => ['--ignore-ssl-errors=yes', '--proxy-type=none'] })
end

Errors getting Selenium running on rails 4 with Safari, chrome or Firefox

I am trying to get selenium (thru Capybara) working on a rails 4, ruby 2.0.0 environment using rspec. I have tried using every browser by setting config settings in my spec_helper.rb. Example with Safari;
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :safari)
end
For all browsers, I have made sure that the appropriate driver (e.g. safaridriver) is in $PATH.
test example (obviously no Javascript/AJAX here, just trying to get it to work);
it 'should post to the publication endpoint', :vcr do
Capybara.current_driver = :selenium
visit root_path
Capybara.use_default_driver
end
on visit root_path, i get the following error;
Selenium::WebDriver::Error::TimeOutError:
timed out waiting for Safari to respond
Environment
rails (4.0.0.beta1)
ruby 2.0.0p0
webmock (1.9.3)
capybara (2.0.2)
selenium-webdriver (2.31.0)
I have been trying to get this to work for two days now so any and all help is greatly appreciated!
The SafariDriver is implemented as a Safari browser extension. That means you need to install it before using Safari with Selenium. Please follow the steps from: https://code.google.com/p/selenium/wiki/SafariDriverInternals
Sign up for Apple's (free) Safari Developer Program and generate a signed certificate for the extension.
Build the SafariDriver extension:
./go safari
Install the extension:
Launch Safari
Enable the Develop menu (Preferences > Advanced > Show Develop menu in menu bar)
Open the Extension Builder (Develop > Show Extension Builder)
Add a new extension: $SELENIUM_CLIENT/build/javascript/safari-driver/SafariDriver.safariextension
Click Install

Resources