SlideShare a Scribd company logo
Capybara


by:- Omnia Gamal El-Deen
email :- omnia.gamal1988@gmail.com
twitter @Omnia_G
Capybara


           Go away
           I'm busy
           NOW
Why Capybara
Setup

Install :
   sudo gem install capybara

Or Gemfile
   gem "capybara", :group => :test
Using Capybara with RSpec

● spec/spec_helper.rb
  require 'capybara/rails'
  require 'capybara/rspec'


● create test directory /spec/requests
● add capybara dsl to RSpec.configure in
  spec_helper.rb
  RSpec.configure do |config|
     config.include Capybara::DSL, :type => :request
Test

test_file.rb
  require 'spec_helper'
  describe "home page" do
      before :each do
          # ......
      end

     it "displays greeting" do
          # .........
     end
  end
The DSL

●   navigating (visit)
●   matcher (page.should)
●   clicks (click_link - click_button)
●   action (fill_in)
●   finders (find)
●   scope (within)
●   Scripting (page.execute_script)
● debugger (page.htm - save_and_open_page)
  ○ NOTE : You'll need to install launchy (gem 'launchy', :
     group => :test)and make sure it's available to open
     pages with save_and_open_page
Example
Drivers

● Driver agnostic
● Support :
    ○ RackTest
    ○ Selenium
    ○ Capybara-webkit
●   Default:
       rack_test driver
RackTest

● It's pure Ruby, So it interacts directly with Rack
    interfaces
●   It does not require a server to be started
But
● Not used to test a remote application, or to access
    remote URLs (e.g., redirects to external sites, external
    APIs, or OAuth services)
●   Dumb
●   It's not have any support for executing JavaScript
Selenium

● Runs tests in a real browser
● Supports any Javascript your browser supports, just like
  real users
● Easy to set up with capybara
But
● Slow
● GUI browser adds a lot of cruft you don’t want
● Unfriendly
   ○ No console.log output
   ○ invisible javascript errors
CAPYBARA-WEBKIT

●   Fast
●   No browser UI
●   Using webkit engine
●   console.log output
●   Errors in standard output
Setup capybara-webkit:

Gemfile
  gem "capybara-webkit", :group => :test
   ○ You didn't need capybara itself anymore
   ○ You will need database_cleaner because database transactions aren’
     t compatible with rspec drivers besides Rack::Test
  gem 'database_cleaner', :group => :test
Spec_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
require 'capybara-webkit'
Capybara.javascript_driver = :webkit

#...
DatabaseCleaner configuration

RSpec.configure do |config|
   #...
   config.use_transactional_fixtures = false
    config.before(:suite) do
        DatabaseCleaner.strategy = :truncation
   end
    config.before(:each) do
        DatabaseCleaner.start
   end
    config.after(:each) do
        DatabaseCleaner.clean
   end
end
Reference :
●   Example@github OmniaGM/learn-capybara
●   Browser testing
●   Capybara
●   Capybara Cheat Sheet

More Related Content

What's hot

Lunch and learn: Cucumber and Capybara
Lunch and learn: Cucumber and CapybaraLunch and learn: Cucumber and Capybara
Lunch and learn: Cucumber and Capybara
Marc Seeger
 
Put a Button on It: Removing Barriers to Going Fast
Put a Button on It: Removing Barriers to Going FastPut a Button on It: Removing Barriers to Going Fast
Put a Button on It: Removing Barriers to Going Fast
OSCON Byrum
 

What's hot (20)

Lunch and learn: Cucumber and Capybara
Lunch and learn: Cucumber and CapybaraLunch and learn: Cucumber and Capybara
Lunch and learn: Cucumber and Capybara
 
Put a Button on It: Removing Barriers to Going Fast
Put a Button on It: Removing Barriers to Going FastPut a Button on It: Removing Barriers to Going Fast
Put a Button on It: Removing Barriers to Going Fast
 
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...Write an API for Almost Anything: The Amazing Power and Flexibility of Django...
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...
 
Alexander Mostovenko "'Devide at impera' with GraphQL and SSR"
Alexander Mostovenko "'Devide at impera' with GraphQL and SSR"Alexander Mostovenko "'Devide at impera' with GraphQL and SSR"
Alexander Mostovenko "'Devide at impera' with GraphQL and SSR"
 
Introduction to ansible galaxy
Introduction to ansible galaxyIntroduction to ansible galaxy
Introduction to ansible galaxy
 
Django rest framework tips and tricks
Django rest framework   tips and tricksDjango rest framework   tips and tricks
Django rest framework tips and tricks
 
Building an API with Django and Django REST Framework
Building an API with Django and Django REST FrameworkBuilding an API with Django and Django REST Framework
Building an API with Django and Django REST Framework
 
Ansible - Hands on Training
Ansible - Hands on TrainingAnsible - Hands on Training
Ansible - Hands on Training
 
Server Side Rendering, caching and performance
Server Side Rendering, caching and performanceServer Side Rendering, caching and performance
Server Side Rendering, caching and performance
 
Testing with Rspec 3
Testing with Rspec 3Testing with Rspec 3
Testing with Rspec 3
 
Javascript Test Automation Workshop (21.08.2014)
Javascript Test Automation Workshop (21.08.2014)Javascript Test Automation Workshop (21.08.2014)
Javascript Test Automation Workshop (21.08.2014)
 
JHipster Conf 2018 : Connect your JHipster apps to the world of APIs with Ope...
JHipster Conf 2018 : Connect your JHipster apps to the world of APIs with Ope...JHipster Conf 2018 : Connect your JHipster apps to the world of APIs with Ope...
JHipster Conf 2018 : Connect your JHipster apps to the world of APIs with Ope...
 
Parsing and Rewriting Ruby Templates
Parsing and Rewriting Ruby TemplatesParsing and Rewriting Ruby Templates
Parsing and Rewriting Ruby Templates
 
Automate All the Things with Grunt
Automate All the Things with GruntAutomate All the Things with Grunt
Automate All the Things with Grunt
 
Paperclip
PaperclipPaperclip
Paperclip
 
Rails Concept
Rails ConceptRails Concept
Rails Concept
 
Django rest framework
Django rest frameworkDjango rest framework
Django rest framework
 
Basic RSpec 2
Basic RSpec 2Basic RSpec 2
Basic RSpec 2
 
Ansible 101 - Presentation at Ansible STL Meetup
Ansible 101 - Presentation at Ansible STL MeetupAnsible 101 - Presentation at Ansible STL Meetup
Ansible 101 - Presentation at Ansible STL Meetup
 
Test automation with Cucumber-JVM
Test automation with Cucumber-JVMTest automation with Cucumber-JVM
Test automation with Cucumber-JVM
 

Similar to Capybara with Rspec

Deployments with rails
Deployments with railsDeployments with rails
Deployments with rails
Gourav Tiwari
 
NLIT 2011: Chef & Capistrano
NLIT 2011: Chef & CapistranoNLIT 2011: Chef & Capistrano
NLIT 2011: Chef & Capistrano
nickblah
 

Similar to Capybara with Rspec (20)

Deployments with rails
Deployments with railsDeployments with rails
Deployments with rails
 
Autotesting rails app
Autotesting rails appAutotesting rails app
Autotesting rails app
 
Automating Complex Setups with Puppet
Automating Complex Setups with PuppetAutomating Complex Setups with Puppet
Automating Complex Setups with Puppet
 
The Automation Factory
The Automation FactoryThe Automation Factory
The Automation Factory
 
Capybara and cucumber with DSL using ruby
Capybara and cucumber with DSL using rubyCapybara and cucumber with DSL using ruby
Capybara and cucumber with DSL using ruby
 
Toolbox of a Ruby Team
Toolbox of a Ruby TeamToolbox of a Ruby Team
Toolbox of a Ruby Team
 
Beyond Puppet
Beyond PuppetBeyond Puppet
Beyond Puppet
 
Deploy like a pro!
Deploy like a pro!Deploy like a pro!
Deploy like a pro!
 
Automating complex infrastructures with Puppet
Automating complex infrastructures with PuppetAutomating complex infrastructures with Puppet
Automating complex infrastructures with Puppet
 
pppr - 解決 JavaScript 無法被搜尋引擎正確索引的問題
pppr - 解決 JavaScript 無法被搜尋引擎正確索引的問題pppr - 解決 JavaScript 無法被搜尋引擎正確索引的問題
pppr - 解決 JavaScript 無法被搜尋引擎正確索引的問題
 
Capistrano
CapistranoCapistrano
Capistrano
 
Sinatra for REST services
Sinatra for REST servicesSinatra for REST services
Sinatra for REST services
 
Practical Chef and Capistrano for Your Rails App
Practical Chef and Capistrano for Your Rails AppPractical Chef and Capistrano for Your Rails App
Practical Chef and Capistrano for Your Rails App
 
Monitoring your VM's at Scale
Monitoring your VM's at ScaleMonitoring your VM's at Scale
Monitoring your VM's at Scale
 
NLIT 2011: Chef & Capistrano
NLIT 2011: Chef & CapistranoNLIT 2011: Chef & Capistrano
NLIT 2011: Chef & Capistrano
 
How I hack on puppet modules
How I hack on puppet modulesHow I hack on puppet modules
How I hack on puppet modules
 
Get your teeth into Plack
Get your teeth into PlackGet your teeth into Plack
Get your teeth into Plack
 
Optimizing AngularJS Application
Optimizing AngularJS ApplicationOptimizing AngularJS Application
Optimizing AngularJS Application
 
Chef training - Day2
Chef training - Day2Chef training - Day2
Chef training - Day2
 
Merb Slices
Merb SlicesMerb Slices
Merb Slices
 

Recently uploaded

Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 

Recently uploaded (20)

Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and Planning
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
Optimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityOptimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through Observability
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
 
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
 
UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
 

Capybara with Rspec

  • 1. Capybara by:- Omnia Gamal El-Deen email :- omnia.gamal1988@gmail.com twitter @Omnia_G
  • 2. Capybara Go away I'm busy NOW
  • 3.
  • 5. Setup Install : sudo gem install capybara Or Gemfile gem "capybara", :group => :test
  • 6. Using Capybara with RSpec ● spec/spec_helper.rb require 'capybara/rails' require 'capybara/rspec' ● create test directory /spec/requests ● add capybara dsl to RSpec.configure in spec_helper.rb RSpec.configure do |config| config.include Capybara::DSL, :type => :request
  • 7. Test test_file.rb require 'spec_helper' describe "home page" do before :each do # ...... end it "displays greeting" do # ......... end end
  • 8. The DSL ● navigating (visit) ● matcher (page.should) ● clicks (click_link - click_button) ● action (fill_in) ● finders (find) ● scope (within) ● Scripting (page.execute_script) ● debugger (page.htm - save_and_open_page) ○ NOTE : You'll need to install launchy (gem 'launchy', : group => :test)and make sure it's available to open pages with save_and_open_page
  • 10. Drivers ● Driver agnostic ● Support : ○ RackTest ○ Selenium ○ Capybara-webkit ● Default: rack_test driver
  • 11. RackTest ● It's pure Ruby, So it interacts directly with Rack interfaces ● It does not require a server to be started But ● Not used to test a remote application, or to access remote URLs (e.g., redirects to external sites, external APIs, or OAuth services) ● Dumb ● It's not have any support for executing JavaScript
  • 12. Selenium ● Runs tests in a real browser ● Supports any Javascript your browser supports, just like real users ● Easy to set up with capybara But ● Slow ● GUI browser adds a lot of cruft you don’t want ● Unfriendly ○ No console.log output ○ invisible javascript errors
  • 13. CAPYBARA-WEBKIT ● Fast ● No browser UI ● Using webkit engine ● console.log output ● Errors in standard output
  • 14. Setup capybara-webkit: Gemfile gem "capybara-webkit", :group => :test ○ You didn't need capybara itself anymore ○ You will need database_cleaner because database transactions aren’ t compatible with rspec drivers besides Rack::Test gem 'database_cleaner', :group => :test
  • 15. Spec_helper.rb ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' require 'capybara/rspec' require 'capybara-webkit' Capybara.javascript_driver = :webkit #...
  • 16. DatabaseCleaner configuration RSpec.configure do |config| #... config.use_transactional_fixtures = false config.before(:suite) do DatabaseCleaner.strategy = :truncation end config.before(:each) do DatabaseCleaner.start end config.after(:each) do DatabaseCleaner.clean end end
  • 17. Reference : ● Example@github OmniaGM/learn-capybara ● Browser testing ● Capybara ● Capybara Cheat Sheet