cucumber
DESCRIPTION
Introduce you Cucumber architecture and how to use CucumberTRANSCRIPT
![Page 1: Cucumber](https://reader035.vdocuments.site/reader035/viewer/2022062703/5553ab19b4c905d9448b483f/html5/thumbnails/1.jpg)
![Page 2: Cucumber](https://reader035.vdocuments.site/reader035/viewer/2022062703/5553ab19b4c905d9448b483f/html5/thumbnails/2.jpg)
What is Cucumber?Cucumber is a tool that executes plain-text functional descriptions as automated tests. Here is an example:
BDD: Behavior Driven Development
![Page 3: Cucumber](https://reader035.vdocuments.site/reader035/viewer/2022062703/5553ab19b4c905d9448b483f/html5/thumbnails/3.jpg)
Running test cases
![Page 4: Cucumber](https://reader035.vdocuments.site/reader035/viewer/2022062703/5553ab19b4c905d9448b483f/html5/thumbnails/4.jpg)
How Cucumber works: Gherkin
• Gherkin is the language that Cucumber understands. • It is a Business Readable, Domain Specific Language
that lets you describe software’s behaviour without detailing how that behaviour is implemented.
• So everyone can write Gherkin.• Gherkin serves two purposes – documentation and
automated tests. The third is a bonus feature – when it yells in red it’s talking to you, telling you what code you should write.
![Page 5: Cucumber](https://reader035.vdocuments.site/reader035/viewer/2022062703/5553ab19b4c905d9448b483f/html5/thumbnails/5.jpg)
Gherkin Example 1: Feature: Some terse yet descriptive text of what is desired 2: In order to realize a named business value 3: As an explicit system actor 4: I want to gain some beneficial outcome which furthers the goal 5: 6: Scenario: Some determinable business situation 7: Given some precondition 8: And some other precondition 9: When some action by the actor10: And some other action11: And yet another action12: Then some testable outcome is achieved13: And something else we can check happens too14: 15: Scenario: A different situation16: ...
![Page 6: Cucumber](https://reader035.vdocuments.site/reader035/viewer/2022062703/5553ab19b4c905d9448b483f/html5/thumbnails/6.jpg)
Gherkin Documents
![Page 7: Cucumber](https://reader035.vdocuments.site/reader035/viewer/2022062703/5553ab19b4c905d9448b483f/html5/thumbnails/7.jpg)
How Cucumber works: Backgrounder
• Language: Ruby, etc.
Gherkin:Scenario: Users can enter an invoice item . . . Then I enter a product quantity of 5
Step definition:When /enter a product quantity of (\d+)/ do |quantity| pending "TODO: Do we need to have a product code passed as well?"End
![Page 8: Cucumber](https://reader035.vdocuments.site/reader035/viewer/2022062703/5553ab19b4c905d9448b483f/html5/thumbnails/8.jpg)
How Cucumber works: Capybara
• Capybara helps you test Rails and Rack applications by simulating how a real user would interact with your app.
• It is agnostic about the driver running your tests and comes with Rack::Test and Selenium support built in.
![Page 9: Cucumber](https://reader035.vdocuments.site/reader035/viewer/2022062703/5553ab19b4c905d9448b483f/html5/thumbnails/9.jpg)
Capybara Example
When /^I sign in$/ do within("#session") do fill_in 'Login’, with => '[email protected]' fill_in 'Password’, with => 'password' end click_link 'Sign in'end
![Page 10: Cucumber](https://reader035.vdocuments.site/reader035/viewer/2022062703/5553ab19b4c905d9448b483f/html5/thumbnails/10.jpg)
Capybara DSL
• Navigation: visit
• Interact: attach_file, text, check, choose, click_link_or_button, click_button,
click_link, field_labeled, fill_in, select, unselect, uncheck, click_on
• Find: all, first, find, find_button, find_by_id, find_field, find_link
• Query: has_content?, has_css?, has_no_content?, has_no_css?,
has_no_xpath?, has_xpath?, has_link?, has_no_link?, has_button,
has_no_button?, has_field?, has_no_field?, has_checked_field?,
has_unchecked_field?, has_no_table?, has_table?, has_select?,
has_no_select?, has_selector?, has_no_selector?, has_no_checked_field?,
has_no_unchecked_field?
• Debugging: save_page, save_and_open_page
![Page 11: Cucumber](https://reader035.vdocuments.site/reader035/viewer/2022062703/5553ab19b4c905d9448b483f/html5/thumbnails/11.jpg)
Capybara Drivers
• RackTest– Default driver– Doesn’t support Javascript
• Webkit– Uses QtWebKit to start a rendering engine
process.– Execute Javascript
• Selenium– Start a Firefox to test
![Page 12: Cucumber](https://reader035.vdocuments.site/reader035/viewer/2022062703/5553ab19b4c905d9448b483f/html5/thumbnails/12.jpg)
Cucumber Architecture
Cucumber
Gherkin
Given When Then
Backgrounder
Ruby code Capybara
DSL
Navigating Interaction Querying Finding Scoping Debugging
Drivers
RackTest Webkit Selenium
Firefox …
![Page 13: Cucumber](https://reader035.vdocuments.site/reader035/viewer/2022062703/5553ab19b4c905d9448b483f/html5/thumbnails/13.jpg)
Install cucumber in Rails 2.3gem "capybara", "1.1.1"gem "cucumber", "1.1.0"gem "cucumber-rails", "0.3.2”
$ script/generate cucumber --capybaracreate config/cucumber.yml #cucumber command paramscreate config/environments/cucumber.rb #cucumber rails envcreate script/cucumbercreate features/step_definitions #step definitionscreate features/step_definitions/web_steps.rb #generalstep definitionscreate features/support #cucumber configure filescreate features/support/paths.rb #cucumber path configurecreate features/support/env.rb #cucumber env configurecreate lib/tasks #cucumber rake taskscreate lib/tasks/cucumber.rake
![Page 14: Cucumber](https://reader035.vdocuments.site/reader035/viewer/2022062703/5553ab19b4c905d9448b483f/html5/thumbnails/14.jpg)
Write your first cucumber test caseIt’s really, really recommended that you write your features by hand – in collaboration with your customer / business analyst / domain expert / interaction designer. However, to get you started you can use the feature generator to generate the first few features:
$ script/generate feature Frooble name:string color:string description:text exists features/step_definitions create features/manage_froobles.feature #gherkin, user stories create features/step_definitions/frooble_steps.rb #ruby, step definitions
![Page 15: Cucumber](https://reader035.vdocuments.site/reader035/viewer/2022062703/5553ab19b4c905d9448b483f/html5/thumbnails/15.jpg)
How to use Cucumber?
1. Describe behaviour in plain text2. Write a step definition in Ruby3. Run and watch it fail4. Write code to make the step pass5. Run again and see the step pass6. Repeat 2-5 until green like a cuke7. Repeat 1-6 until the money runs out
![Page 16: Cucumber](https://reader035.vdocuments.site/reader035/viewer/2022062703/5553ab19b4c905d9448b483f/html5/thumbnails/16.jpg)
More info?