cucumber capybara how does it work - ruby-on-rails

I am using capybara and cucumber in my rails3 application. Everything is working just fine. However I don't understand how tests are working.
Let's say that I have a standard cucumber feature test. I am not using #selenium etc. It is a plain vanilla test. Now when I run
bundle exec cucumber
does that rails server start on a port? If yes then what's the port number? Also in the plain-vanilla case capybara uses rack-test. However rack-test is not responsible to running the server. So I am all confused how the whole thing works. What role cucumber is playing. What job rack-test is doing and what capybara is doing.
If you have links to articles then please share with me. I really want to know how things are working together.

While I don't really know the deepest details of how this works. But I do enough to provide you some perspective.
Cucumber is a ruby acceptance testing framework, it lets you write acceptance in plain english. Now in general it sits on top of Webrat or Capybara which provide it its true power, ie Simulated Browser or Automated Browser testing.
Capybara or Webrat make it easy to run acceptance tests.Capybara makes it very simple to use various drivers to run acceptance testing. Drivers ie Selenium, celerity or rack-test. In a vanilla case, rack-test is used for simulated browser testing.
Rack-Test itself is just responsible for creating the session in which you run your tests in, the step definitions etc have been created by capybara. So basically there is no server running its a just session ( mock-session to be precise ) created by rack-test. Capybara now provides a finders methods and matchers etc on top of this session created by the driver ( rack-test or otherwise ) to help you create your own step definitions.
Cucumber runs the steps, ie the finders /matchers etc in them, on the session and so your tests.

Related

Capybara vs Page Object

I'm currently using Cucumber and Ruby Page Object for testing my Ruby on Rails website.
I'm trying to understand Capybara and decide if I should add it to the mix, or maybe replace Page Object with it.
I'd appreciate if somebody provides more insights into benefits of using Capybara when compared to Page Object, and does it make sense to use them together?
Thanks
You cannot use Cheezy's page-object gem with Capybara.
The page-object gem only supports Watir-Webdriver and Selenium-WebDriver. It does not support Capybara.
Capybara, Watir and Selenium are for driving browsers while page-object is for modelling your pages. If you switch to Capybara, you would need to pick a different page-object library such as SitePrism. Switching would mean you either need to support two completely separate stacks or re-write your existing tests.

Testing Rails app with RSpec and Capybara

In testing my rails app (using RSpec and Capybara) I am having troubles testing AngularJS functions (from the Users point of view). Can I not test with only RSpec and Capybara or do I also need something like protractor?
any help would be great! thank you!
You could test with Capybara, so long as you use a driver that supports javascript, such as Selenium or Poltergeist - see https://github.com/jnicklas/capybara#drivers
However, although I'm not an AngularJS user, I do know that it's designed to be testable, and I imagine you'd be much better off using a tool specifically for javascript testing, rather than capybara.
As long as angular provides it's own methods and functionalities you should separate the testing suite into two. The first one, rspec, capybara and rails will be testing just the non-angular pages.
Second one, using a tool like jasmine should be testing the angular behaviour. Both combined provides good coverage of the application.
https://docs.angularjs.org/guide/unit-testing

Is it possible to use Cucumber to test an existing Rails Application?

I have just finished reading many different web pages on the subject of Cucumber. It appears to be impossible to use Cucumber to test an existing Ruby on Rails application. I'm just asking in case I missed something. (We are considering using it for future RoR app development.)
While I am asking, what is the current consensus on the best tool to test an existing Ruby on Rails application?
I did a little exploration with WATIR and it seems easy to use, but driving the web browser results in being not scalable. I have read some have moved from WATIR to Celerity....
However, I am in an environment where we are starting from zero previous testing. What is the 'best' choice to quickly write tests for an existing Ruby on Rails application?
In no way is Cucumber unable to test an existing application. Not sure what gave you this idea, but perhaps because it's typically used with test-driven development?
Cucumber is by far the most widely used integration testing software for Rails. I would definitely suggest using Cucumber with Capybara, Guard, Spork, and RSpec for your testing suite. (RSpec for unit testing, Capybara for integration tests, Guard and Spork for running your tests quickly).
If you've never looked at Cucumber before, you might want to take a look at some tutorials first.
To get started on your project, try to make a Cucumber feature for a simple feature. For example, write a test to visit the homepage. Something like:
Feature: View homepage
Given I am on the profile page
When I click "home"
Then I should see the homepage logo
Once you've understood that, move on to more complicated features. Examples might include create a user account, login, view a profile, etc. (I don't know what your application does but I think you get the point.)
Cucumber is able to test an existing application.
You have to set configuration setting for cucumber and generate cucumber folder by rails g cucumber
before run this command you have to include gem: gem 'cucumber'.

Recommended testing frameworks for Rails 2 app with randomness

I have a Rails 2.3.5 app which is serving a card game. I've been a bit lax in writing tests for it (read: I haven't written any :embarrassed:), and I'd like to make a start now.
From reading other questions, I think I probably want to be using Shoulda extending Test::Unit for unit testing.
I wondered about using Capybara extending RSpec for functional testing, but the majority of users' interaction with the app is via POST, which I understand Capybara doesn't handle.
Since this is a card game, I obviously need the ability to control rand; I also definitely need the framework to handle Javascript.
Is Test::Unit + Shoulda suitable?
Am I correct that Capybara can't handle POST? Can I work around that?
Is there a better option instead of Capybara?
Can these methods handle the randomness involved in a card-game? Presumably, for Test::Unit at least, I can just call srand at some early stage to fix the generator, but I don't know if that's doable for Capybara/RSpec/anything else.
Can anyone point me to resources dealing specifically with setting up Shoulda, Capybara and RSpec for Rails 2.3.5? It's a little hard to tell what's Rails 3 specific and what isn't.
Would I gain anything from adding Watir / Firewatir into the mix?
I realise the above questions are manifold, but they basically boil down to "does this work, or have you any better suggestions?"
If the majority of the users' interactions are via POST, is it via an API (as opposed to filling out forms or something)?
Just about any combination of RSpec/Shoulda/Capybara/Test Unit/Rack::Test could work for you depending on your need. They're all capable. However, these are my recommendations:
If you've decided you want integration testing via HTML pages, use Cucumber and Capybara
If you've decided you want integration testing via HTTP API, use RSpec and Rack::Test
You probably want to fake out randomness.
You probably don't need Watir/Firewatir.
It does look like you can make POST requests via some Capybara drivers:
http://suffix.be/blog/capybara-post-requests
When Rails moved to 3.0, RSpec went to 2.0 so for at least RSpec, you'd want RSpec and RSpec Rails 1.3.2.
By "fake out randomess", I mean redefine srand in your tests so you can predictably run them.
module Kernel
def rand
YourApp.rand
end
end
module MyApp
class << self
attr_accessor :rand
end
end
Then, before you have the user press the button, run a step definition like "When random returns 6", which will set MyApp.rand = 6.

Totally confused about rails testing.. which tools are for which jobs?

I'm learning Rails after a long time manually testing my own .NET code,
I'm loving what ive seen but i am SO confused about how it all fits together!
So my questions are
1 - Where would i use:
Rspec
Cucumber
Test Unit
Shoulda
Selenium (not really a ruby thing but more of a web thing ive heard)
I've sort-of been testing my code with some very basic RSpec on my models and using factory girl..
2 - Do i need all of these tools?
For example could i choose cucumber and factory girl and never have to learn rspec or is cucumber a pretty dsl wrapper for rspec and test unit...
3 - Are any of them usable / have a port on .NET as well?
Thanks!
Daniel
My current stack of Testing tools is:
Steak, instead of Cucumber.
Capybara with driver Akephalos, instead of Selenium.
RSpec
Machinist2, instead of factory girl.
https://github.com/cavalle/steak
https://github.com/jnicklas/capybara
http://rspec.info/
https://github.com/notahat/machinist
I learned a lot about testing with the book: The Rspec Book, by the Pragmatic Programmers.
http://pragprog.com/
You have more detailed information in this other question:
Rails: Good Rspec2 example usage? (Also: Cucumber, Pickle, Capybara)
For 1)
I do use Rspec for unit and Functional testing.
I do use Cucumber for integration testing. Cucumber uses Capybara or Selenium. I like Cucumber because it enables me to write tests with the customers. They feel implicated and thus give sometimes more details about their expectations.
Selenium could be used as a stand alone app to test your web app directly in your browser.
Many other tools exist, it's really a matter of choice. As you said, fixtures are not used anymore, Factory Girl is one of the best way to create testing data sets.
For 2)
You don't need all these tools, of course. You could even write your tests with the native Rails helpers.
But they provide convenient helpers you can take benefit from. So get the one you prefer. Some, like Cucumber, have extensions (like Pickle), to provide even more helpers.
For 3)
The strength of Rspec, Cucumber, Selenium (those I know) is they can be used to test any app.
I'm curious to listen to other's point of view concerning Ajax testing.

Resources