49

Can someone explain the difference between these two platforms?

Are both part of BDD but why should I use one or other, or both together?

Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
piermaria
  • 551
  • 1
  • 4
  • 6

4 Answers4

105

Capybara is a tool that interacts with a website the way a human would (like visiting a url, clicking a link, typing text into a form and submitting it). It is used to emulate a user's flow through a website. With Capybara you can write something like this:

describe "the signup process", :type => :feature do
  before :each do
    User.make(:email => 'user@example.com', :password => 'caplin')
  end

  it "signs me in" do
    visit '/sessions/new'
    within("#session") do
      fill_in 'Login', :with => 'user@example.com'
      fill_in 'Password', :with => 'password'
    end
    click_link 'Sign in'
    page.should have_content 'Success'
  end
end

Cucumber is a tool to write human-readable tests that are mapped into code. With it, you can rewrite the above example like this:

Scenario: Signup process

Given a user exists with email "user@example.com" and password "caplin"
When I try to login with "user@example.com" and "caplin"
Then I should be logged in successfully

The almost plain-text interpretation is useful to pass around non-developers but also need some code mapped into it to actually work (the step definitions).

Usually you will use Capybara if you're testing a website and use Cucumber if you need to share those tests with non-developers. These two conditions are independent so you can use one without the other or both or none.

PS: in the code snippet there is some RSpec as well. This is needed because Cucumber or Capybara by themselves cannot test something. They rely on RSpec, Test::Unit or minitest to do the actual "Pass or Fail" work.

Kostas
  • 8,356
  • 11
  • 47
  • 63
7

cucumber is a BDD tool that expresses testing scenarios in a business-readable, domain-specific language.

capybara is an automated testing tool (often used) for ROR applications.

On the capybara github page, there's an example on using capybara with cucumber.

orde
  • 5,233
  • 6
  • 31
  • 33
5

Cucumber is a general-purpose BDD tool. It knows nothing about web apps. So Cucumber step definitions call Capybara in order to test web apps.

Andy Waite
  • 10,785
  • 4
  • 33
  • 47
0
|-------------------------------|-------------------------------|
|      Cucumber                 |     Capybara                  |
|-------------------------------|-------------------------------|
| Test cases are more readable  | Test cases are not readable;  |
| and written in plain english  | Capybara also wraps RSpec and |
| text as a DSL                 | you follow the same pattern to|
|                               | write test cases              |
|-------------------------------|-------------------------------|
| Easy to iterate data using    | Not easy to iterate data      |
| tables                        |                               |
|-------------------------------|-------------------------------|
| Cucumber has its own runner   | Uses RSpec runner to execute  |
|                               | tests                         |
|-------------------------------|-------------------------------|
| Default HTML reporter         | No default HTML reporter      |
|-------------------------------|-------------------------------|
| Need to learn cucumber regex  | No such concept               |
| pattern to write step-        |                               |
| definition which is the middle|                               |
| man for test case and script. |                               |
| Btw, the regex pattern is not |                               |
| essential for all cases.      |                               |
|-------------------------------|-------------------------------|
| You can use the native        | (Wrapper)/Built on top of     |
| selenium-webdriver methods    | selenium-webdriver ruby       |
| while using Cucumber; Cucumber| library when used with        |
| is just an additional         | selenium; it can also be used |
| framework to your generic     | with two other drivers.       |
| automation framework          |                               |
|-------------------------------|-------------------------------|
| Pure BDD framework (In theory | Traditional functional test   |
| BDD sounds great. In practice,| model for end-to-end testing  |
| product owners and developers |                               |
| rarely continue to use BDD)   |                               |
|-------------------------------|-------------------------------|
Prashanth Sams
  • 19,677
  • 20
  • 102
  • 125
  • its a pretty answer, but most of it is just wrong I'm afraid. For example cucumber does not use native selenium components, capybara can be used without selenium, capybara has nothing to do with test cases, or page objects, you don't need to learn regex's to write step definitions etc. etc.. With respect I'd suggest you withdraw this answer. – diabolist Sep 03 '19 at 09:15
  • @diabolist thank you for the valuable feedback; I agree few points and adjusted the answer; also, added extra points that was actually missed. – Prashanth Sams Sep 03 '19 at 10:46
  • `Capybara is itself a wrapper around selenium-webdriver` https://stackoverflow.com/questions/45893060/upgrade-old-project-to-capybara. – Prashanth Sams Sep 03 '19 at 11:04
  • Capybara has something to do with RSpec (test cases); those are the two points I don't agree with – Prashanth Sams Sep 03 '19 at 11:13
  • Hope you know that there are few regex patterns that you wanted to learn to glue cucumber test case with the script. By default you can generate it but it is also important to learn it https://www.coveros.com/regular-expressions-a-setup-for-cucumber-glue-code/#targetText=A%20regular%20expression%2C%20or%20regex,the%20literal%20text%20%E2%80%9Ccucumber%E2%80%9D.&targetText=The%20most%20basic%20regular%20expression%20consists%20of%20a%20single%20literal%20character%3B%20e.g. – Prashanth Sams Sep 03 '19 at 11:23
  • Capybara can use selenium but it doesn't have too, For example it can use the rack driver instead – diabolist Sep 04 '19 at 12:22
  • You can cuke effectively without using regex's. I've been Cuking for longer than the entire lifespan of Cucumber (started with Rspec Plain Text stories, the pre-cursor to Cucumber). I haven't used a regex in my step defs for the last 5 or so years. – diabolist Sep 04 '19 at 12:24
  • Capybara it a tool created to be used by test tools to support driving web browsers. Its basically a DSL that wraps lower level methods that interact with a browser instance that is provided by a test tool via a test case – diabolist Sep 04 '19 at 12:27
  • Thomas's answer needs to be taken in context of the question. Yes capybara when used with selenium acts as a wrapper around selenium, but Capybara does not have to be used with selenium. His answer is correct in that context, your understanding of that answer is incorrect – diabolist Sep 04 '19 at 12:32
  • All the stuff about site prism again has no place in an answer about comparing Cucumber and Capybara. You are implying that site prism is a part of these tools when in fact you can use both tools without ever touching site prism or page objects for that matter. – diabolist Sep 04 '19 at 12:35
  • this will be my last comment on this. This answer is misleading, and just plain wrong, and the author should just remove it. – diabolist Sep 04 '19 at 12:40
  • @diabolist cool; I agree and respect when you've mentioned. Removed the site-prism statements and updated the other point. – Prashanth Sams Sep 04 '19 at 13:05