4. Amir Barylko - Intro to Capybara
Why Projects Fail?
• Delivering late or over budget
• Delivering the wrong thing
• Unstable in production
• Costly to maintain
5. Amir Barylko - Intro to Capybara
Improve Quality
• Unit Testing
• Integration Testing
• Acceptance Testing
6. Amir Barylko - Intro to Capybara
BDD
• Implementing an application
• by describing its behavior
• from the perspective of the stakeholder
7. Amir Barylko - Intro to Capybara
Outside In Approach
BDD TDD
8. Amir Barylko - Intro to Capybara
Test First
• Write a test before any line of code
• Write minimum amount of code to make
the test pass
• Refactor code to eliminate “smells”
10. Amir Barylko - Intro to Capybara
Gherkin DSL
• Business readable DSL
• Flush out requirements
• Documentation
• Automated testing
• Used by Cucumber,
SpecFlow, jBehave
11. Amir Barylko - Intro to Capybara
Gherkin Keywords
• Feature • Then
• Scenario • And
• Given • But
• When
12. Amir Barylko - Intro to Capybara
Features
Feature: Listing projects
As a user Free text!
I Want to see the list of projects
So I can choose one to see the details
Scenario: List all projects
(steps here to implement scenario)
Scenario: No projects are available
(steps here to implement scenario)
13. Amir Barylko - Intro to Capybara
Scenario
Scenario: List all projects
Given I'm logged in Step 1
And I have some projects stored Step 2
When I browse the projects Step 3
Then I should see all of them listed Step 4
14. Amir Barylko - Intro to Capybara
Running Features
• Parse the feature
• Parse the scenario
• For each scenario
• Find a step implementation
• Execute the code
15. Amir Barylko - Intro to Capybara
Matching Step
• Matching regular expression
Given I have some projects stored Feature File
Given /^I have some projects stored$/
Step Def File
16. Amir Barylko - Intro to Capybara
Step
Given /^I have some projects stored$/ do
projects = 10.times { random_valid_project }
fake_response = create_response(projects)
FakeWeb.register_uri(....)
end
Plain
Ruby!
18. Amir Barylko - Intro to Capybara
The biggest rodent?
• Gem to simplify integration tests
• Inspired by Webrat
• Used to implement Cucumber steps
• Or with your favorite testing framework
19. Amir Barylko - Intro to Capybara
Why?
• Support for different drivers (browsers)
• Even a headless driver (capybara-webkit)
• Supports Rack applications
• But can be used with any web app!
20. Amir Barylko - Intro to Capybara
Forms
• click_link • check
• click_button • uncheck
• click_link_or_button • attach_file
• choose • select_option
• fill_in(“FirstName”, :with => “John”)
21. Amir Barylko - Intro to Capybara
Finders
• all(:css, ‘#user’)
• all(:xpath, ‘//[@id=user]’)
• find (throws exception if not found)
• find_field
• find_link
• find_button
• find_by_id
22. Amir Barylko - Intro to Capybara
Matchers
• has_selector?
• has_no_selector?
• has_content?
• has_no_content
23. Amir Barylko - Intro to Capybara
Javascript
• execute_script(....)
• Only selenium
page.driver.browser.switch_to.alert.accept
page.driver.browser.switch_to.alert.dismiss
page.driver.browser.switch_to.alert.text
26. Amir Barylko - Intro to Capybara
Next steps
• Learn about using capybara DSL
• Read and follow the Cucumber book
• Learn about page objects pattern
• Start with a simple project
27. Amir Barylko - Intro to Capybara
Resources
• Email: amir@barylko.com,
• Twitter: @abarylko
• Blog: http://orthocoders.com
• Website: http://maventhought.com