Does capybara replace selenium? - ruby-on-rails

When using capybara for testing, do you still need the likes of selenium?
What are the advantages of either solution?

Capybara uses Selenium as one of its drivers. By default, Capybara uses the Rack::Test driver instead of controlling an actual browser with Selenium, but when you run Javascript tests, the Selenium driver is the default.
The project's README gives a good overview of how the pieces fit together.

Related

Converting Selenium IDE tests to run in rspec without browser

We have a rails app and some users have used Selenium IDE firefox extension to record/create some tests. We'd like to be able to integrate these tests into our codebase of automated tests.
I understand that Selenium IDE has the ability to export test cases, but what is the easiest way to convert these tests into something that rspec can run without a browser (or headless)?
I've used both. Here is what you can do
export the file using the rspec webdriver format
adjust any use of variables or local file storage that is possible in selenium to make sense using rspec. This will depend totally on your usage.
Here's what one goes through:
Select the test you want to export (you can also export the entire suite)
Export as rspec webdriver
Open the resulting file
Adjust as needed, e.g. move common items to spec_helper.rb and make sure it is included.
Put the file in an rspec folder, e.g. spec/views and make sure that spec_helper includes them.
I understand that Selenium IDE has the ability to export test cases, but what is the easiest way to convert these tests into something that rspec can run without a browser (or headless)?
Selenium IDE is only plugin for Firefox.
I believe, you can to do the same steps on another language/way.
But for this you need to have another code.
Code for Selenium Web Driver is possible to use with headless browser as phantomJS. And its easy.
But Selenium IDE code is only for Firefox IDE plugin.
And by the way, its hard to maintain tests written using IDE. I believe, better to rewrite all tests using Selenium WebDriver in your case. Its will be much easier and faster. (you can use Page Object model for this, as example)

RSpec/Capybara: Cross-Domain JS Testing

I'm working on an advertising application, where there is some cross-domain JS. I'd like to write tests that verify that the cross-domain JS is working as intended.
My first thoughts are that I would need to be able to
visit some_url_thats_not_my_rails_app
However, Capybara throws a "No Route Matches", since it obviously goes to only relative paths, and is intended for testing your OWN website. But I really need to go on ANOTHER page, and verify that things like the serialized token are identical.
Is Capybara the right tool for this? If so, what do I need to do to force non-relative paths?
Yes, Capybara is right tool for it. You can also use Ruby with Selenium webdriver(Would be awesome with page object gem) or Ruby with Watir webdriver.
To visit and test any web application, you can use Ruby and Capybara. For this you need to set app_host. Add this one in support/env.rb:
Capybara.app_host = "http://flipkart.com"
And in hooks.rb
Before do
visit('/')
end
You can use Capybara with Selenium webdriver and you will be good to test any deployed application in production, staging or prep env.

what are the required Gem for Automated Acceptance testing

I want to write acceptance testing through cucumber and capybara. i have experience with rspec. I want to write integration/features test cases . I want outcome to be seen on a web browser which shows the test case running. what are the gems required for writing test cases .
If you are already familiar with RSpec, I recommend you to use RSpec with Capybara for acceptance testing. RSpec is testing framework, and Capybara is a library that helps you to test web applications by simulating how a real user would interact with your app.
Acceptance tests in RSpec with Capybara are called "feature specs" and live in /spec/features directory in Rails. Capybara comes with a built in DSL for writing descriptive acceptance tests with RSpec (feature/scenario), but you may also use traditional RSpec DSL (describe/it).
Capybara supports several drivers, and its default driver RackTest doesn't support JavaScript. So, probably, you'll want to use one of the alternative drivers (I prefer Poltergeist, but it is headless, so if you want to see result in the browser, you may use Selenium driver). In this case, you'll need to set up database_cleaner to clear your database between tests. Capybara's README contains a lot of information about its configuration and usage with RSpec, configuring driver and using database_cleaner.
You can start with this and this screencasts. But remember, that they are a little bit outdated and use traditional RSpec DSL (instead of new Capybara DSL), and use old convention, when "feature" specs were called "request" specs. Currently, by convention, "request" specs, are integration tests without capybara. So you'll need to create your capybara specs in spec/features directory, not spec/requests. And if you want to use Capybara DSL, this is easy to fix too. Just replace describe with feature, it with scenario, let with given etc. It is well documented in Capybara's README.
Hope this helps.

jQuery document.ready not called during rails integration test

I have an integration test that fails for a page that depends heavily on javacript. The same page runs just fine from the browser.
Simplifying the test to a bare minimum I found out that just testing for the presence of a selector that is added by javascript after the page load, would fail.
After precompiling the test assets and using save_and_open_page I found that the handler for the jQuery ready event is not running during the integration test.
I didn't find any references to this problem, so I guess I'm doing something wrong.
Can anyone help me figuring this out?
I'm using using rails 3.2.11, RSpec 2.13.0, Capybara 2.0.3 and capybara-webkit 0.14.2
By default Rails uses webdriver which doesn't handle JS. If you want JS support you'd need to use one of the JS-aware drivers, like Selenium (which runs full featured browser), capybara-webkit or poltergeist (which are headless webkit browsers). There are many others but these three are most popular.
I solved this problem for me by examining my AJAX request. The problem was that I was requesting a 'http://..." instead of '/' of my page. This meant that it raised a CORS ('ajax request from outside the domain') even though the request looked like it was coming from inside the domain.
My original symptom was 'javascript-inserted HTML elements are not on the page when testing using Capybara with Poltergeist or Selenium (but are on the page when running the application).'

Using Rails with Webrat + Selenium or only Selenium?

I'm reading The Rspec Book, and in that book they are using Webrat with Selenium.
Is it possible to use Selenium directly for integration tests in Rails instead of using it through Webrat?
What are the pros and cons with each way?
It is most definitely possible to use Selenium by itself. I recommend installing the Selenium IDE plugin for Firefox. This gives you an easy scripting layer to automate clicks and that sort of thing. By integrating Selenium with Rails, you can execute integration tests from the command line, which is nice for a number of reasons: other developers on your team can run them more easily, you can run the same tests against multiple browsers more easily, and you can run the tests from a continuous integration server (that can launch a web browser).
We use Cucumber with Capybara for our integration tests. Webrat does not support JavaScript, so if you click a link that has a click event handler for example, the handler won't fire. Capybara knows Javascript so will fire the event handler. Selenium would let you do this as well, but we already use Cucumber and I prefer Cucumber+Capybara tests to Selenium because it is more integrated and the tests are easier to maintain.

Resources