Cucumber vs Capybara - testing

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?

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.

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.

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.

|-------------------------------|-------------------------------|
| 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) | |
|-------------------------------|-------------------------------|

Related

How to execute tests (multiple feature files) across environments in parallel in Karate DSL

How to execute tests(feature files) across multiple environments in parallel using Karate DSL
I have two feature files which I need to execute in parallel across the different environments.
Use a second feature file, and call the feature you want in 2 scenarios, but passing different arguments.
You can even use a Scenario Outline, for an example see this:
Scenario Outline:
* call read('some.feature') <config>
Examples:
| config |
| { type: 'chrome' } |
| { type: 'chromedriver' } |
| { type: 'geckodriver' } |

Karate UI cross browser examples

Does someone have any examples of how to run cross browser UI tests in karate? I know from Peter's twitter feed that its possible but anyone having some examples?
Thanks
Look at this example: test-01.feature
It is a Scenario Outline and each row changes the value of * configure driver = config, see the Examples at the end:
Examples:
| config |
| { type: 'chrome' } |
| { type: 'chromedriver' } |
| { type: 'geckodriver' } |
Another strategy could be to re-run the test with different values of karate.env or use call and loop over a second feature file.

Karate Cucumber Report | Why cucumber report displays rows for all tags?

I'm using Karate DSL's parallel runner to run my API tests. It is using cucumber reporting library to generate test results.I have question about 'Tags' section of cucumber report.
Here is my sample feature file.
#tag1 #tag2
Feature: Test
Background:
Given url testUrl
Scenario Outline: Test scenario
Given path 'test'
And param parameter = <parameter>
When method GET
Then status 200
Examples:
| parameter |
| 123 |
| 456 |
Issue:
If I run test with only tag #tag1, I'm expecting to see only #tag1 in 'Tags' section of the report, but It's displaying rows for both #tag1 and #tag2.
Questions:
1. Is it expected behavior in cucumber report ?
2. Is there a way to customize report to only include specified tag ?
You may have some mistake when you ran the tests.
Refer this other answer: https://stackoverflow.com/a/51839405/143475
If that doesn't help please follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue
EDIT: have responded and closed the issue you raised.

Database Cleaner Not Cleaning One Cucumber Scenario

So my team is driving out our rails app (a survey and lite electronic record system) using Cucumber, Rspec and all the gems necessary to use these testing frameworks. I am in the process of setting up a Jenkins CI server and wanted to standardize our databases across our testing, development, and staging environments (we ended up choosing MySQL).
Upon switching the testing environment from SQlite to MySQL we discovered a couple of caching-related testing bugs that we resolved by using relative ids instead of hard-coded ones. example:
describe "#instance_method" do
before(:each) do
#survey = FactoryGirl.create(:survey)
#question_1 = FactoryGirl.create(:question)
#question_2 = FactoryGirl.create(:question)
....
end
context "question has no requirements" do
it "should match" do
# below breaks under MySQL (and postgres), but not SQlite
expect { #survey.present_question?(2) }.to be_true
# always works
expect { #survey.present_question?(#question_2.id) }.to be_true
end
end
end
After resolving this one failing spec, I addressed some unrelated ajax issues that have forever plaguing our test suite. Thinking that I finally masted the art of testing, I confidently ran rake. I was met with this unfriendly sight:
Now of course cucumber features/presenting_default_questions.feature rains green when ran in isolation.
Failing Scenario:
#javascript
Feature: Dynamic Presentation of Questions
In order to only answer questions that are relevant considering previously answered questions
As a patient
I want to not be presented questions that illogical given my previously answered question on the survey
Scenario: Answering a question that does not depend on any other questions
Given I have the following questions:
| prompt | datatype | options | parent_id | requirement |
| Do you like cars? | bool | | | |
| Do you like fruit? | bool | | | |
When I visit the patient sign in page
And I fill out the form with the name "Jim Dog", date of birth "1978-03-30", and gender "male"
And I accept the waiver
Then I should see the question "Do you like cars"
When I respond to the boolean question with "Yes"
Then I should see the question "Do you like fruit?"
When I respond to the boolean question with "No"
Given I wait for the ajax request to finish
Then I should be on the results page
Relevant step:
Then(/^I should be on the results page$/) do
# fails under ``rake`` passes when run in isolation
current_path.should == results_survey_path(1)
end
Then(/^I should be on the results page$/) do
# passes in isolation and under ``rake``
current_path.should == results_survey_path(Survey.last.id)
end
Besides from using Capybara.javascript_driver = :webkit, the cucumber / database cleaner config is unmodified from rails g cucumber:install.
It seems like both the failing rspec and cucumber tests suffer from the same sort of indexing problem. While the solution proposed above works, it's super janky and begs the question as to why a simple absolute index doesn't work (after all the database is cleaned between each scenario and feature). Is there something up with my tests? With database_cleaner?
Please let me know if more code would be helpful!
Relavent gem versions:
activemodel (3.2.14)
cucumber (1.3.6)
cucumber-rails (1.3.1)
database_cleaner (1.0.1)
capybara (2.1.0)
capybara-webkit (1.0.0)
mysql2 (0.3.13)
rspec (2.13.0)
The problem seems to be that you are missing the database_cleaner code for your Cucumber scenarios.
You mentioned that you have the database_cleaner code for your RSpec scenarios, but it seems that you're missing something like this for Cucumber's env.rb:
begin
require 'database_cleaner'
require 'database_cleaner/cucumber'
DatabaseCleaner.strategy = :truncation
rescue NameError
raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end
Around do |scenario, block|
DatabaseCleaner.cleaning(&block)
end
Do you have this code in there?

factory_girl_rails & Cucumber::undefined

Having a problem with making this work. Seems it has been a problem for others and I think I have followed all of the advice.
I've set up a stripped down rails 3 .0.14 app to just include cucumber-rails & factory_girl_rails but still no go. I expect I am doing something silly!
Running the cuc test below produces the following:
Scenario: test factory-girl # features/users.feature:3
Given the following user exists: # features/users.feature:4
| name | email |
| Brandon | brandon#example.com |
Undefined step: "the following user exists:" (Cucumber::Undefined)
The user factory has been created, of which I am sure, with a bit of 'pp' output.
Would really appreciate any help to get this sorted.
Ross
Set up
env.rb: snippet
require 'pp'
require 'cucumber/rails'
require 'factory_girl_rails'
require 'factory_girl/step_definitions'
features/support/factories.rb:
FactoryGirl.define do
factory :user do
name 'Adam Advertiser'
email 'a#b.com'still
end
end
pp FactoryGirl.create(:user)
Cucumber features/user.feature:
Feature: a
Scenario: test factory-girl
Given the following user exists:
| name | email |
| Brandon | brandon#example.com |
The problem here is that you have to get your factory required before requiring 'factory_girl/step_definitions', because there is meta-programming in step_definitions which needs to know about your factory. You could explicitly require the factories.rb in the env.rb, but that will end up producing a duplicate definition error, as cucumber will re-require factories.rb.
You need to remove the requiring of step_definitions from the env.rb - that will make it happen too early - and put it at the bottom of factories.rb, or else create a wrapper which requires first the factories (which will need to reside somewhere that cucumber doesn't automatically require) and then the step_definitions.