SlideShare a Scribd company logo
Embracing
Capybara
Who Has Used
Capybara?
Who Has Been
Bitten By Capybara?
TimMoore
tmoore
http://git.io/capybara

What’s a
Capybara?

World’s
Largest Rodent!

“Makes it easy to
simulate how a
user interacts with
your application”
Matchers/

Selectors

Browsing

DSL

Browser
Engines

Capybara Anatomy
visit '/sessions/new'
within("#session") do
fill_in 'Login',

with: 'user@example.com'
fill_in 'Password',

with: 'password'
end
click_link 'Sign in'
expect(page).to have_content 'Success'
Understanding
Capybara
Capybara is Patient
visit '/sessions/new'
within("#session") do

fill_in 'Login',

with: 'user@example.com'
fill_in 'Password',

with: 'password'

end
click_link 'Sign in'
expect(page).

to have_content 'Success'

Waits for page to load
Capybara is Patient
visit '/sessions/new'
within("#session") do

fill_in 'Login',

with: 'user@example.com'
fill_in 'Password',

with: 'password'

end
click_link 'Sign in'
Capybara.using_wait_time(60) do

expect(page).

to have_content 'Success'

end

Waits for page to load
Capybara is Patient
visit '/sessions/new'
within("#session") do

fill_in 'Login',

with: 'user@example.com'
fill_in 'Password',

with: 'password'

end
click_link 'Sign in'
expect(page).

to have_content 'Success',

wait: 60

Waits for page to load
Methods that Wait
•
•
•
•

find(selector), find_field, find_link, etc.
within(selector) (scoping)
has_selector?/has_no_selector? & assertions
form & link actions

• click_link/button
• fill_in
• check/uncheck, select, choose, etc.
Methods that Don’t
•
•
•
•
•
•

visit(path)
current_path
all(selector)
first(selector) (counter-intuitively)
execute_script/evaluate_script
simple accessors: text, value, title, etc.
Don’t Do This
click_link('Search')
expect(all('.search-result').count).

to eq 2
Does not wait

Returns 0
Good
click_link('Search')
expect(page).

to have_css '.search-result',

count: 2
Don’t Do This
click_button('Save')

Does not wait

expect(find('.dirty-warning').text).

not_to contain('Unsaved changes')
Good
click_button('Save')
expect(find('.dirty-warning')).

to have_no_text('Unsaved changes')
Also Good
click_button('Save')
expect(find('.dirty-warning')).

not_to have_text('Unsaved changes')
Training Capybara
Don’t Do This
Scenario: Searching for cats
Given I am on "/search"
When I fill in "input.search_text"
with "cats"
And I press "Search"
Then I should see "grumpy cat"
within "div.search-results"
Don’t Do This

“web_steps.rb is a terrible, terrible idea”
- Boring scenarios cats
Scenario: Searching for
- Brittle scenarios
Given I am - Where is my workflow?
on "/search"

When I fill in "input.search_text"
with "cats"
And I

“A step description
press "Search" contain
should never
regexen, CSS or
should XPath selectors” cat"
see "grumpy

Then I
within "div.search-results"
Better
Scenario: Searching for cats
Given I am on the search page
When I fill in "Search" with "cats"
And I press "Search"
Then I should see "grumpy cat"
within the search results
Best
Scenario: Searching for cats
When I search for "cats"
Then I should see "grumpy cat" within
the search results
Don’t Do This
When /^I search for "([^"]*)"$/

do |search_text|
visit "/search"
fill_in "input.search_text",

with: search_text
click_button "Search"
end
Better
When /^I search for "([^"]*)"$/

do |search_text|
search_page =

my_app.search_page
search_page.search_input.

fill_in search_text
search_page.search_button.click
end
Better
class MyAppTester
def initialize
@session = Capybara::Session.new(:selenium)
end
!

def search_page
MyAppTester::SearchPage.new(@session)
end
end
Better
class MyAppTester::SearchPage
def initialize(session)

session.visit "/search"

@session = session

end
def search_input

@session.find "input.search_text"

end
def search_button

@session.find "button.search"

end
end
Better
When /^I search for "([^"]*)"$/

do |search_text|
search_page =

my_app.search_page
search_page.search_input.

fill_in search_text
search_page.search_button.click
end
Best
When /^I search for "([^"]*)"$/

do |search_text|
my_app.search_for search_text
end
Don’t Do This
find(:css, "div.main div.sidebar
div.search div.advanced div.actions
button.submit").click
Failure/Error: find(:css, "div.main div.sidebar
div.search div.advanced div.actions button.submit").click
Capybara::ElementNotFound:
Unable to find css "div.main div.sidebar
div.search div.advanced div.actions button.submit"
Disciplining
Capybara
Setting Breakpoints
When /^I debug$/ do
debugger
end
Taking Screenshots
After do |scenario|
filename =

scenario.name.gsub(/W/, '_')
page.save_screenshot(

"target/reports/#{filename}.png")
end
!

http://git.io/capybarascreenshot
Firefox
• focusmanager.testmode = true
• Update selenium-webdriver gem
• Disable auto-update in CI
• Beware platform differences
• http://git.io/capybarafirebug
Getting Help
•

http://groups.google.com/group/
ruby-capybara

•
•

Freenode (IRC): #capybara
Stack Overflow
Summary
•
•
•

Understand Capybara synchronisation

•
•

Keep selectors simple

Be explicit with Capybara
Use Page Objects for readable,
flexible tests
If you get bitten, seek help!
Fix Flaky Tests
Thanks!
Any Questions?

More Related Content

What's hot

What is sex ? Sex Sex Sex Sex Sex Sex
 What is sex ? Sex Sex Sex Sex Sex Sex What is sex ? Sex Sex Sex Sex Sex Sex
What is sex ? Sex Sex Sex Sex Sex Sex
Patrick Kasper Kasper
 
Which font should I use?
Which font should I use?Which font should I use?
Which font should I use?
Alexei Kapterev
 
How to Pick the Price for Your Product by Amazon Product Manager
How to Pick the Price for Your Product by Amazon Product ManagerHow to Pick the Price for Your Product by Amazon Product Manager
How to Pick the Price for Your Product by Amazon Product Manager
Product School
 
Presentation Design Secrets
Presentation Design SecretsPresentation Design Secrets
Presentation Design SecretsAkash Karia
 
10 Ways Your Boss Kills Employee Motivation
10 Ways Your Boss Kills Employee Motivation10 Ways Your Boss Kills Employee Motivation
10 Ways Your Boss Kills Employee Motivation
Officevibe
 
Getting Data into Marketo
Getting Data into Marketo Getting Data into Marketo
Getting Data into Marketo Murtza Manzur
 
12 celebs with naughty piercings
12 celebs with naughty piercings12 celebs with naughty piercings
12 celebs with naughty piercings
Mr. Yogi One & Only
 
How To Make an Executive Presentation 2011
How To Make an Executive Presentation 2011How To Make an Executive Presentation 2011
10 tips on writing by David Ogilvy
10 tips on writing by David Ogilvy10 tips on writing by David Ogilvy
10 tips on writing by David Ogilvy
Prezly
 
The Secret -The Law of the Attraction
The Secret -The Law of the AttractionThe Secret -The Law of the Attraction
The Secret -The Law of the Attraction
Diramar Costa
 
Discover The Top 10 Types Of Colleagues Around You
Discover The Top 10 Types Of Colleagues Around YouDiscover The Top 10 Types Of Colleagues Around You
Discover The Top 10 Types Of Colleagues Around You
Ankur Tandon
 

What's hot (12)

What is sex ? Sex Sex Sex Sex Sex Sex
 What is sex ? Sex Sex Sex Sex Sex Sex What is sex ? Sex Sex Sex Sex Sex Sex
What is sex ? Sex Sex Sex Sex Sex Sex
 
Which font should I use?
Which font should I use?Which font should I use?
Which font should I use?
 
How to Pick the Price for Your Product by Amazon Product Manager
How to Pick the Price for Your Product by Amazon Product ManagerHow to Pick the Price for Your Product by Amazon Product Manager
How to Pick the Price for Your Product by Amazon Product Manager
 
Presentation Design Secrets
Presentation Design SecretsPresentation Design Secrets
Presentation Design Secrets
 
10 Ways Your Boss Kills Employee Motivation
10 Ways Your Boss Kills Employee Motivation10 Ways Your Boss Kills Employee Motivation
10 Ways Your Boss Kills Employee Motivation
 
Getting Data into Marketo
Getting Data into Marketo Getting Data into Marketo
Getting Data into Marketo
 
12 celebs with naughty piercings
12 celebs with naughty piercings12 celebs with naughty piercings
12 celebs with naughty piercings
 
How To Make an Executive Presentation 2011
How To Make an Executive Presentation 2011How To Make an Executive Presentation 2011
How To Make an Executive Presentation 2011
 
10 tips on writing by David Ogilvy
10 tips on writing by David Ogilvy10 tips on writing by David Ogilvy
10 tips on writing by David Ogilvy
 
The Secret -The Law of the Attraction
The Secret -The Law of the AttractionThe Secret -The Law of the Attraction
The Secret -The Law of the Attraction
 
What happens online every 60 seconds
What happens online every 60 seconds What happens online every 60 seconds
What happens online every 60 seconds
 
Discover The Top 10 Types Of Colleagues Around You
Discover The Top 10 Types Of Colleagues Around YouDiscover The Top 10 Types Of Colleagues Around You
Discover The Top 10 Types Of Colleagues Around You
 

Viewers also liked

Capybara + RSpec - lightweight acceptance testing
Capybara + RSpec - lightweight acceptance testingCapybara + RSpec - lightweight acceptance testing
Capybara + RSpec - lightweight acceptance testing
patricksroberts
 
User Acceptance Testing Driven by Humans telling Stories (with RSpec)
User Acceptance Testing Driven by Humans telling Stories (with RSpec)User Acceptance Testing Driven by Humans telling Stories (with RSpec)
User Acceptance Testing Driven by Humans telling Stories (with RSpec)
Jean-Michel Garnier
 
Capybara with Rspec
Capybara with RspecCapybara with Rspec
Capybara with Rspec
Omnia Helmi
 
Automated Testing with Ruby
Automated Testing with RubyAutomated Testing with Ruby
Automated Testing with Ruby
Keith Pitty
 
Capybara testing
Capybara testingCapybara testing
Capybara testing
Futureworkz
 
Rspec and Capybara Intro Tutorial at RailsConf 2013
Rspec and Capybara Intro Tutorial at RailsConf 2013Rspec and Capybara Intro Tutorial at RailsConf 2013
Rspec and Capybara Intro Tutorial at RailsConf 2013
Brian Sam-Bodden
 

Viewers also liked (6)

Capybara + RSpec - lightweight acceptance testing
Capybara + RSpec - lightweight acceptance testingCapybara + RSpec - lightweight acceptance testing
Capybara + RSpec - lightweight acceptance testing
 
User Acceptance Testing Driven by Humans telling Stories (with RSpec)
User Acceptance Testing Driven by Humans telling Stories (with RSpec)User Acceptance Testing Driven by Humans telling Stories (with RSpec)
User Acceptance Testing Driven by Humans telling Stories (with RSpec)
 
Capybara with Rspec
Capybara with RspecCapybara with Rspec
Capybara with Rspec
 
Automated Testing with Ruby
Automated Testing with RubyAutomated Testing with Ruby
Automated Testing with Ruby
 
Capybara testing
Capybara testingCapybara testing
Capybara testing
 
Rspec and Capybara Intro Tutorial at RailsConf 2013
Rspec and Capybara Intro Tutorial at RailsConf 2013Rspec and Capybara Intro Tutorial at RailsConf 2013
Rspec and Capybara Intro Tutorial at RailsConf 2013
 

Similar to Embracing Capybara

Quick ref capybara
Quick ref capybaraQuick ref capybara
Quick ref capybara
fatec
 
Quick ref capybara
Quick ref capybaraQuick ref capybara
Quick ref capybara
fatec
 
Introduce cucumber
Introduce cucumberIntroduce cucumber
Introduce cucumber
Bachue Zhou
 
Enabling agile devliery through enabling BDD in PHP projects
Enabling agile devliery through enabling BDD in PHP projectsEnabling agile devliery through enabling BDD in PHP projects
Enabling agile devliery through enabling BDD in PHP projects
Konstantin Kudryashov
 
Cucumber
CucumberCucumber
Cucumber
markjturner
 
Cheap frontend tricks
Cheap frontend tricksCheap frontend tricks
Cheap frontend tricks
ambiescent
 
Cool bonsai cool - an introduction to ElasticSearch
Cool bonsai cool - an introduction to ElasticSearchCool bonsai cool - an introduction to ElasticSearch
Cool bonsai cool - an introduction to ElasticSearch
clintongormley
 
A Universal Automation Framework based on BDD Cucumber and Ruby on Rails - Ph...
A Universal Automation Framework based on BDD Cucumber and Ruby on Rails - Ph...A Universal Automation Framework based on BDD Cucumber and Ruby on Rails - Ph...
A Universal Automation Framework based on BDD Cucumber and Ruby on Rails - Ph...
Ho Chi Minh City Software Testing Club
 
Solving the Riddle of Search: Using Sphinx with Rails
Solving the Riddle of Search: Using Sphinx with RailsSolving the Riddle of Search: Using Sphinx with Rails
Solving the Riddle of Search: Using Sphinx with Rails
freelancing_god
 
The Best (and Worst) of Django
The Best (and Worst) of DjangoThe Best (and Worst) of Django
The Best (and Worst) of DjangoJacob Kaplan-Moss
 
Zero to Sixty: AWS Elastic Beanstalk (DMG204) | AWS re:Invent 2013
Zero to Sixty: AWS Elastic Beanstalk (DMG204) | AWS re:Invent 2013Zero to Sixty: AWS Elastic Beanstalk (DMG204) | AWS re:Invent 2013
Zero to Sixty: AWS Elastic Beanstalk (DMG204) | AWS re:Invent 2013
Amazon Web Services
 
Acceptance Testing with Webrat
Acceptance Testing with WebratAcceptance Testing with Webrat
Acceptance Testing with Webrat
Luismi Cavallé
 
Wynn Netherland: Accelerating Titanium Development with CoffeeScript, Compass...
Wynn Netherland: Accelerating Titanium Development with CoffeeScript, Compass...Wynn Netherland: Accelerating Titanium Development with CoffeeScript, Compass...
Wynn Netherland: Accelerating Titanium Development with CoffeeScript, Compass...
Axway Appcelerator
 
Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel
Engine Yard
 
Unobtrusive javascript with jQuery
Unobtrusive javascript with jQueryUnobtrusive javascript with jQuery
Unobtrusive javascript with jQueryAngel Ruiz
 
RubyBarCamp “Полезные gems и plugins”
RubyBarCamp “Полезные gems и plugins”RubyBarCamp “Полезные gems и plugins”
RubyBarCamp “Полезные gems и plugins”
apostlion
 
[PHP 也有 Day] 垃圾留言守城記 - 用 Laravel 阻擋 SPAM 留言的奮鬥史
[PHP 也有 Day] 垃圾留言守城記 - 用 Laravel 阻擋 SPAM 留言的奮鬥史[PHP 也有 Day] 垃圾留言守城記 - 用 Laravel 阻擋 SPAM 留言的奮鬥史
[PHP 也有 Day] 垃圾留言守城記 - 用 Laravel 阻擋 SPAM 留言的奮鬥史
Shengyou Fan
 
Jquery presentation
Jquery presentationJquery presentation
Jquery presentationguest5d87aa6
 
Writing Software not Code with Cucumber
Writing Software not Code with CucumberWriting Software not Code with Cucumber
Writing Software not Code with Cucumber
Ben Mabey
 
SMX West 2020 - Leveraging Structured Data for Maximum Effect
SMX West  2020 - Leveraging Structured Data for Maximum EffectSMX West  2020 - Leveraging Structured Data for Maximum Effect
SMX West 2020 - Leveraging Structured Data for Maximum Effect
Abby Hamilton
 

Similar to Embracing Capybara (20)

Quick ref capybara
Quick ref capybaraQuick ref capybara
Quick ref capybara
 
Quick ref capybara
Quick ref capybaraQuick ref capybara
Quick ref capybara
 
Introduce cucumber
Introduce cucumberIntroduce cucumber
Introduce cucumber
 
Enabling agile devliery through enabling BDD in PHP projects
Enabling agile devliery through enabling BDD in PHP projectsEnabling agile devliery through enabling BDD in PHP projects
Enabling agile devliery through enabling BDD in PHP projects
 
Cucumber
CucumberCucumber
Cucumber
 
Cheap frontend tricks
Cheap frontend tricksCheap frontend tricks
Cheap frontend tricks
 
Cool bonsai cool - an introduction to ElasticSearch
Cool bonsai cool - an introduction to ElasticSearchCool bonsai cool - an introduction to ElasticSearch
Cool bonsai cool - an introduction to ElasticSearch
 
A Universal Automation Framework based on BDD Cucumber and Ruby on Rails - Ph...
A Universal Automation Framework based on BDD Cucumber and Ruby on Rails - Ph...A Universal Automation Framework based on BDD Cucumber and Ruby on Rails - Ph...
A Universal Automation Framework based on BDD Cucumber and Ruby on Rails - Ph...
 
Solving the Riddle of Search: Using Sphinx with Rails
Solving the Riddle of Search: Using Sphinx with RailsSolving the Riddle of Search: Using Sphinx with Rails
Solving the Riddle of Search: Using Sphinx with Rails
 
The Best (and Worst) of Django
The Best (and Worst) of DjangoThe Best (and Worst) of Django
The Best (and Worst) of Django
 
Zero to Sixty: AWS Elastic Beanstalk (DMG204) | AWS re:Invent 2013
Zero to Sixty: AWS Elastic Beanstalk (DMG204) | AWS re:Invent 2013Zero to Sixty: AWS Elastic Beanstalk (DMG204) | AWS re:Invent 2013
Zero to Sixty: AWS Elastic Beanstalk (DMG204) | AWS re:Invent 2013
 
Acceptance Testing with Webrat
Acceptance Testing with WebratAcceptance Testing with Webrat
Acceptance Testing with Webrat
 
Wynn Netherland: Accelerating Titanium Development with CoffeeScript, Compass...
Wynn Netherland: Accelerating Titanium Development with CoffeeScript, Compass...Wynn Netherland: Accelerating Titanium Development with CoffeeScript, Compass...
Wynn Netherland: Accelerating Titanium Development with CoffeeScript, Compass...
 
Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel
 
Unobtrusive javascript with jQuery
Unobtrusive javascript with jQueryUnobtrusive javascript with jQuery
Unobtrusive javascript with jQuery
 
RubyBarCamp “Полезные gems и plugins”
RubyBarCamp “Полезные gems и plugins”RubyBarCamp “Полезные gems и plugins”
RubyBarCamp “Полезные gems и plugins”
 
[PHP 也有 Day] 垃圾留言守城記 - 用 Laravel 阻擋 SPAM 留言的奮鬥史
[PHP 也有 Day] 垃圾留言守城記 - 用 Laravel 阻擋 SPAM 留言的奮鬥史[PHP 也有 Day] 垃圾留言守城記 - 用 Laravel 阻擋 SPAM 留言的奮鬥史
[PHP 也有 Day] 垃圾留言守城記 - 用 Laravel 阻擋 SPAM 留言的奮鬥史
 
Jquery presentation
Jquery presentationJquery presentation
Jquery presentation
 
Writing Software not Code with Cucumber
Writing Software not Code with CucumberWriting Software not Code with Cucumber
Writing Software not Code with Cucumber
 
SMX West 2020 - Leveraging Structured Data for Maximum Effect
SMX West  2020 - Leveraging Structured Data for Maximum EffectSMX West  2020 - Leveraging Structured Data for Maximum Effect
SMX West 2020 - Leveraging Structured Data for Maximum Effect
 

Recently uploaded

PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 

Recently uploaded (20)

PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 

Embracing Capybara

Editor's Notes

  1. I’m going to talk about Embracing Capybara: how to understand Capybara, train it to do new tricks, and discipline it when it misbehaves. First, show of hands…
  2. OK keep them up…
  3. Sometimes it just doesn’t do what you want it to do.
  4. I work at Lonely Planet, on a fairly complex single-page app, and we use Capybara for our tests.
  5. In fact, we use a lot of Capybaras.
  6. Like, a whole herd of Capybaras running together in parallel.
  7. We, too, have been bitten by our Capybaras. We got through it, learned a lot, and now we can’t imagine living without our Capybara.
  8. Capybaras are the largest rodents in the world. They are related to guinea pigs, but are the size of a big dog or small pig. They live in South America, and they’re pretty damn cute. Capybara is also the name of a Ruby gem written by this guy, Jonas Nicklas. It helps you write integration tests for web apps by pretending to be a user with a web browser. It takes a black box approach to testing, letting you go to URLs, click links and buttons, fill in forms, and check that the rendered page contains what you think it should have in it. It doesn’t replace test runners like RSpec, Cucumber and Minitest, it works with them. It has built-in integration with the most popular testing frameworks, but is test-framework agnostic.
  9. Three parts: Front end is a Ruby-based DSL for browsing the web: actions like visiting URLs, clicking links and buttons, interacting with forms, and querying for contents of the page. Powering this is a flexible selector engine: supports CSS selectors, XPath, finding by content or form input labels. On the back end, Capybara has pluggable drivers for various browsing engines. Out of the box, it supports RackTest and Selenium WebDriver. RackTest is a headless, pure Ruby browser simulator. Very fast, no setup required, works everywhere, but doesn’t support JavaScript execution. Selenium drives a real web browser: Firefox, Chrome, IE, etc. Executes JavaScript. Great for compatibility testing and JavaScript-heavy apps, but slow to run, and asynchronous execution can be difficult to understand. Other options via gems: e.g., Poltergeist integrates with PhantomJS, a headless WebKit implementation. Most people use Selenium WebDriver. Why not code directly against the WebDriver API? Capybara is more Ruby-like, more flexible, and makes it easier to handle asynchrony.
  10. Part of what makes Capybara so appealing is that it feels like magic. This is also what can make it frustrating. To work effectively with Capybara, you need to learn to think like a Capybara.
  11. Capybara automatically waits for asynchronous operations to complete. When you try to find an element that isn't on the page, it waits and retries until it is there, or a timeout duration elapses. Default is 2 seconds. That's pretty short, so if the sign-in is slow, you might get timeout errors.
  12. You can wrap a block in 'using_wait_time' to give it a longer timeout duration.
  13. In Capybara 2.1, you can also pass a 'wait' option to individual methods.
  14. Methods that unambiguously identify a fixed number of elements. This isn't fully comprehensive, but most methods fall into one of these categories.
  15. Anything where Capybara can't guess how many elements you’re expecting, or what they are.
  16. This test may fail when it should pass.
  17. We can tell Capybara how many results we're expecting, and it will wait for it to become true, or time out.
  18. When something is supposed to be removed from a page after an action, Capybara needs to wait for that, too.
  19. If we tell Capybara that we're expecting the text not to be there, it will wait for that to be true.
  20. Capybara's RSpec matchers are smart, so this works too. It knows whether you said expect/to (or should) or expect/not_to (or should_not).
  21. We’ve seen examples of simple Capybara code written in an imperative, script-like style, but in big apps, it can get unwieldy. Luckily, Capybara speaks Ruby, so we can use all of the tools of the language—classes, inheritance, delegation, blocks—to teach Capybara abstractions and encapsulate the details of our application.
  22. This Cucumber scenario is very tightly coupled to the page it is testing. It’s ugly enough here, but in a big app this style of Cucumber is much worse, with URL paths and CSS selectors duplicated everywhere. You see this a lot in apps that used an older version of the cucumber-rails gem. It used to generate a file of default Cucumber step definitions that looked just like this.
  23. Jonas, the author of Capybara, wrote a blog post called “You’re Cuking it Wrong” explaining why this is bad practice. Aslak Hellesøy, the author of Cucumber, has since disavowed web_steps.rb, and it is no longer created by default.
  24. Here, the underlying URL and page markup details have been encapsulated within step definitions. This is much more readable for non-technical stakeholders. Still, it is very coupled to specific UI implementation details.
  25. Now the Cucumber test concisely describes the business requirements.
  26. You may be tempted to simply move the tightly-coupled test code into the step definition, but this is hardly better. It is still boring and brittle.
  27. We can refactor to page objects that model the UI of the application, and create an object-oriented testing API that can be reused across step definitions.
  28. The application tester object can be added to the Cucumber world. It encapsulates access to the Capybara session, and provides methods that represent pages in the app, or services it provides.
  29. Each page encapsulates markup details by providing logical methods for the elements on the page. These return Capybara Node objects, which support methods such as click, fill_in, finding descendent elements, etc. The entire Capybara DSL is available, scoped within that node. For more complex elements, you can encapsulate them in their own page objects that provide higher-level APIs. For example, a date picker could be wrapped in an object that lets you set a date or choose “Today”.
  30. Let’s see how that’s used again.
  31. Long selectors like this are extremely brittle. There's no shame in adding CSS IDs and classes to your markup for your tests. Difficulty writing tests can be a code smell: clean, semantic markup makes for easier testing. Try (A)TDD. Page Objects help here, too. Navigating through a chain of page elements gives useful errors and stack traces.
  32. Sometimes, despite your best efforts, Capybara misbehaves. Here are some tips you can use for getting it back in line.
  33. Add this to your Cucumber step definitions. If a step is failing, throw “And I debug” right before it and watch what happens in the browser. If it looks like it’s working, continue in the debugger. If it passes, you probably have a synchronisation issue, where the step isn’t waiting for the operation to complete.
  34. Capybara can save screenshots. This is a simple way to save a screenshot after each scenario. You can get more clever with this, such as only saving after failures. Our code is pretty complex, but there is also a gem that makes it automatic.
  35. One annoying thing with Firefox: sometimes tests fail when it doesn’t have focus. Firefox doesn’t fire certain events when it’s in the background. focusmanager.testmode is a hidden Firefox configuration that disables this. Google for it and you’ll find examples of how to set it. New versions of Firefox often break WebDriver, so keep that gem up to date using “bundle update selenium-webdriver” often. Also, disable Firefox’s auto-update in CI. Linux handles events differently than Mac OS and Windows. Keep that in mind when diagnosing failures in CI. Make sure your xvnc or xvfb window is large enough to display the page properly. We had to configure the default geometry in Jenkins. You’ll get a clean profile in Firefox with no add-ons installed by default. Capybara Firebug is a gem that automatically installs and activates Firebug into Firefox when Capybara runs. Really useful, but slows down your tests. Use a Cucumber profile to activate it.
  36. The usual resources apply.
  37. The most important thing to do: prioritise fixing flaky tests as a team. Flaky tests are ones that sometimes pass, sometimes fail, seemingly at random. It’s all too easy to get into a situation where one flaky test becomes many, your build is always red, and you start to ignore failures. Your tests lose their meaning, and then one day you ship a bug that the tests would have caught. That really stinks.