SlideShare ist ein Scribd-Unternehmen logo
1 von 37
Downloaden Sie, um offline zu lesen
Efficient Rails Test-Driven
 Development — Week 6

     Wolfram Arnold
    www.rubyfocus.biz

    In collaboration with:
Sarah Allen, BlazingCloud.net
       marakana.com
Integration Frameworks

Cucumber, Webrat, Capybara,...
  lightweight browser without JS
  fast
  easy to set up
  great for API testing
  useful for exploratory application testing
Integration Frameworks

Selenium
  drives a browser (Firefox, IE, Safari, Chrome...)
  slow
  many moving pieces
  many technology choices
  great if client/server testing is a must
  useful for exploratory application testing
Selenium
Console Experiments

irb
> require 'support/boot'
> Page.start_new_browser_session
> ...
> Homepage.visit
> Page.selenium_driver.try_something_here
> ...
> Page.close_current_browser_session
Procedural Selenium Test

page.open
page.type “username”, “joe”
page.type “password”, “secret password”
page.click “submit”, :wait_for => :page
page.text?(“My HomePage”).should be_true
page.click “link=New Messages”
...
Problems with this

●   Very low level
    –   hard to see what's being checked
    –   hard to reuse
    –   prone to copy & paste
●   Next step
    –   pull out common methods
    –   but common file quickly gets cluttered
    –   most tests only use a small fraction of common
        methods
Page Objects
Page Objects

Each Page represented by an Object
Methods are
  –   things you can do
  –   things you can see
  –   apply to this page only
Each method returns a page object
  –   itself
  –   another page object
Advantages

●   Reuse is easy
●   Documentation
●   Maintainability
    –   Changes to the UI or page require only one change
        of the test code, in only one place
●   Method Chaining
    –   Development of new tests is easy and fast
    –   Easy reuse of existing “library of things to do” on a
        page
How to build Page Objects?

Model the UI in Page Objects
  entire pages
  parts of a page
Locators
Locators

Selenium Locators
  “link=Sign in”
  “username”
CSS Locators
  “css=ul.global_nav_list li”
  “css=input[name=username]”
XPath Locators
  “xpath=//ul[@class='global_nav_list']/li”
  “xpath=//input[@name='username']”
Selenium Locators—Default
“identifier” matches:
                                     identifier keyword
id attribute                         is optional
  element(“identifier=navigation”)
                                     This is the default
  finds: <div id=”navigation”>       locator

name attribute
  element(“identifier=username”)
  finds: <input name=”username”>


Note: matches id first, if not found, looks for
 name attribute
Other Selenium Loctors

Explicit “id” locator:
   matches id attribute
   element(“id=navigation”)
   finds: <div id=”navigation”>


Explicit “name” locator:
   matches name attribute
   element(“name=username”)
   finds: <input name=”username”>
More on name locator

The “name” locator may be followed by filters:


  element(“name=usename value=Joe”)


  Supported filters:
  value=value pattern
  index=element index, starting at 0
Link Locators

The “link” locator is used for links:


  element(“link=Sign in”)


  matches the text (pattern) of an a tag:


  <a href=”...”>Sign in</a>
Select from Drop-Down

select(selectLocator, optionLocator)


optionLocator matches on label by default


Example:
select(“name=language”,”French”)
Text Verification

glob:pattern
  Similar to filename matching no command line:
     * matches any sequence of characters
     ? matches any single character
regexp:pattern
  match against a regular expression
regexpi:pattern
  match against a regular expression, case-insensitive
exact:string
  exact match
String matching examples

“Welcome John Smith”
  glob:Welcome*
  regexp:Welcome.*
  regexpi:welcome.*
  exact:Welcome John Smith


If nothing is specified, glob is the default.
CSS Locators

css=a
  selects <a> tag
css=a.some_class
  selects <a class=”some_class”>
css=a#some_id
  selects <a id=”some_id”>
css=a.some_class#some_id
  selects <a id=”some_id” class=”some_class”>
CSS Locators: Attributes

css=input[name=username]
  selects <input name=”username”>
css=img[src*=”btn_img”]
  selects <img src=”http://example.com/btn_img?1234>


*= partial match
^= match beginning
$= match end
CSS Locators Chained

tag1 tag2                   tag1+tag2
  decendents                  siblings
  css=ul.navlist li a         css=input+a

  <ul class=”navlist”>        <input>
    <li>
         <a>Some link</a>     <a></a>
    </li>
  <ul>
Waiting Rules
When to Wait?

An Event Trigger
  Click
  Mouse over/out
  Form Submit


Waiting for:
  Page load
  AJAX load
  JavaScript action (e.g. hide/show)
Waiting for Page

open “/”
  will automatically wait for page
clicking on a link
  wait_for :page
Wait for Element

Used for: Ajax, JavaScript


click “locator”,
  :wait_for => :element,
  :element => “locator”
Wait for Visibility

Something appearing or disappearing
 dynamically


click “locator”,
  :wait_for => :visible,
  :element => “locator”


Note: The element must be present (see wait for
 element) or visibility check will fail with a “no
 element found error”
RSpec Custom Matchers
[1,2,3] == [2,3,1]


([1,2,3] – [2,3,1]).should be_empty


[1,2,3].should be_commutative_with([2,3,1])


http://wiki.github.com/dchelimsky/rspec/custom-
  matchers
http://railscasts.com/episodes/157-rspec-
  matchers-macros
Testing for
Access Control
Access Control

Controller access
  Disallowed action should be blocked via before_filters
  Most important!


View access
  Disallowed pages/actions should not be linked to
  Purely cosmetic
Devise

http://github.com/plataformatec/devise


Latest in Authorization Plugins
Comes with controllers and views
  password recovery feature, etc.
Rack-based
Test Driven
Development
Test-Driven Development

Benefit #1: Better Design
  Testing as-you-go makes code better structured.
  Testing emphasizes decoupling.
  Fat model, skinny controller becomes easier.


Benefit #2: Focus & Project Management
  If you don't know what to test for, how can you know
     what to code for?
  Knowing when you're done.
Test-Driven Development

Benefit #3: Documentation & Collaboration
  Tests are documentation that's always current.
  Tests illustrate how code is meant to be used.


Benefit #4: Creation of Tests
  Testing happens in-situ, not after the fact
  When you're done coding, you've got a full test suite
  No tedious chores and guilt afterwards
Inside-Out vs. Outside-In

Start with what you understand
  can be model, view or controller
When you discover you need something else...
  make failing tests pending
  implement what you're missing
  resume
Discover the application inside out.

Weitere ähnliche Inhalte

Was ist angesagt?

jQuery Proven Performance Tips & Tricks
jQuery Proven Performance Tips & TricksjQuery Proven Performance Tips & Tricks
jQuery Proven Performance Tips & TricksAddy Osmani
 
Working with the django admin
Working with the django admin Working with the django admin
Working with the django admin flywindy
 
MVC & SQL_In_1_Hour
MVC & SQL_In_1_HourMVC & SQL_In_1_Hour
MVC & SQL_In_1_HourDilip Patel
 
Handlebars and Require.js
Handlebars and Require.jsHandlebars and Require.js
Handlebars and Require.jsIvano Malavolta
 
Filtering data with D2W
Filtering data with D2W Filtering data with D2W
Filtering data with D2W WO Community
 
In memory OLAP engine
In memory OLAP engineIn memory OLAP engine
In memory OLAP engineWO Community
 
Two scoops of django 1.6 - Ch7, Ch8
Two scoops of django 1.6  - Ch7, Ch8Two scoops of django 1.6  - Ch7, Ch8
Two scoops of django 1.6 - Ch7, Ch8flywindy
 
jQuery Performance Tips and Tricks (2011)
jQuery Performance Tips and Tricks (2011)jQuery Performance Tips and Tricks (2011)
jQuery Performance Tips and Tricks (2011)Addy Osmani
 
Introduction to Sightly
Introduction to SightlyIntroduction to Sightly
Introduction to SightlyAnkit Gubrani
 
Coding with style: The Scalastyle style checker
Coding with style: The Scalastyle style checkerCoding with style: The Scalastyle style checker
Coding with style: The Scalastyle style checkerMatthew Farwell
 
Testing mit Codeception: Full-stack testing PHP framework
Testing mit Codeception: Full-stack testing PHP frameworkTesting mit Codeception: Full-stack testing PHP framework
Testing mit Codeception: Full-stack testing PHP frameworkSusannSgorzaly
 
Automated Testing With Watir
Automated Testing With WatirAutomated Testing With Watir
Automated Testing With WatirTimothy Fisher
 
Jasig Rubyon Rails
Jasig Rubyon RailsJasig Rubyon Rails
Jasig Rubyon RailsPaul Pajo
 
An Introduction to Ruby on Rails
An Introduction to Ruby on RailsAn Introduction to Ruby on Rails
An Introduction to Ruby on RailsJoe Fiorini
 
ERRest - The Next Steps
ERRest - The Next StepsERRest - The Next Steps
ERRest - The Next StepsWO Community
 

Was ist angesagt? (20)

jQuery Proven Performance Tips & Tricks
jQuery Proven Performance Tips & TricksjQuery Proven Performance Tips & Tricks
jQuery Proven Performance Tips & Tricks
 
Working with the django admin
Working with the django admin Working with the django admin
Working with the django admin
 
MVC & SQL_In_1_Hour
MVC & SQL_In_1_HourMVC & SQL_In_1_Hour
MVC & SQL_In_1_Hour
 
Handlebars and Require.js
Handlebars and Require.jsHandlebars and Require.js
Handlebars and Require.js
 
KAAccessControl
KAAccessControlKAAccessControl
KAAccessControl
 
Selenium bootcamp slides
Selenium bootcamp slides   Selenium bootcamp slides
Selenium bootcamp slides
 
Filtering data with D2W
Filtering data with D2W Filtering data with D2W
Filtering data with D2W
 
In memory OLAP engine
In memory OLAP engineIn memory OLAP engine
In memory OLAP engine
 
Two scoops of django 1.6 - Ch7, Ch8
Two scoops of django 1.6  - Ch7, Ch8Two scoops of django 1.6  - Ch7, Ch8
Two scoops of django 1.6 - Ch7, Ch8
 
jQuery Performance Tips and Tricks (2011)
jQuery Performance Tips and Tricks (2011)jQuery Performance Tips and Tricks (2011)
jQuery Performance Tips and Tricks (2011)
 
Introduction to Sightly
Introduction to SightlyIntroduction to Sightly
Introduction to Sightly
 
Wt unit 5 client &amp; server side framework
Wt unit 5 client &amp; server side frameworkWt unit 5 client &amp; server side framework
Wt unit 5 client &amp; server side framework
 
Coding with style: The Scalastyle style checker
Coding with style: The Scalastyle style checkerCoding with style: The Scalastyle style checker
Coding with style: The Scalastyle style checker
 
Testing mit Codeception: Full-stack testing PHP framework
Testing mit Codeception: Full-stack testing PHP frameworkTesting mit Codeception: Full-stack testing PHP framework
Testing mit Codeception: Full-stack testing PHP framework
 
Automated Testing With Watir
Automated Testing With WatirAutomated Testing With Watir
Automated Testing With Watir
 
Jasig Rubyon Rails
Jasig Rubyon RailsJasig Rubyon Rails
Jasig Rubyon Rails
 
Flask restless
Flask restlessFlask restless
Flask restless
 
An Introduction to Ruby on Rails
An Introduction to Ruby on RailsAn Introduction to Ruby on Rails
An Introduction to Ruby on Rails
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
ERRest - The Next Steps
ERRest - The Next StepsERRest - The Next Steps
ERRest - The Next Steps
 

Ähnlich wie 2010 07-18.wa.rails tdd-6

Test automation with selenide
Test automation with selenideTest automation with selenide
Test automation with selenideIsuru Madanayaka
 
Selenium testing
Selenium testingSelenium testing
Selenium testingJason Myers
 
Selenium WebDriver with Java
Selenium WebDriver with JavaSelenium WebDriver with Java
Selenium WebDriver with JavaFayis-QA
 
Automation with Selenium Presented by Quontra Solutions
Automation with Selenium Presented by Quontra SolutionsAutomation with Selenium Presented by Quontra Solutions
Automation with Selenium Presented by Quontra SolutionsQuontra Solutions
 
Testing ASP.NET - Progressive.NET
Testing ASP.NET - Progressive.NETTesting ASP.NET - Progressive.NET
Testing ASP.NET - Progressive.NETBen Hall
 
jQuery Performance Tips and Tricks
jQuery Performance Tips and TricksjQuery Performance Tips and Tricks
jQuery Performance Tips and TricksValerii Iatsko
 
Comprehensive Browser Automation Solution using Groovy, WebDriver & Obect Model
Comprehensive Browser Automation Solution using Groovy, WebDriver & Obect ModelComprehensive Browser Automation Solution using Groovy, WebDriver & Obect Model
Comprehensive Browser Automation Solution using Groovy, WebDriver & Obect ModelvodQA
 
Automation - web testing with selenium
Automation - web testing with seleniumAutomation - web testing with selenium
Automation - web testing with seleniumTzirla Rozental
 
Breaking the limits_of_page_objects
Breaking the limits_of_page_objectsBreaking the limits_of_page_objects
Breaking the limits_of_page_objectsRobert Bossek
 
Javascript unit testing, yes we can e big
Javascript unit testing, yes we can   e bigJavascript unit testing, yes we can   e big
Javascript unit testing, yes we can e bigAndy Peterson
 
SharePoint Cincy 2012 - jQuery essentials
SharePoint Cincy 2012 - jQuery essentialsSharePoint Cincy 2012 - jQuery essentials
SharePoint Cincy 2012 - jQuery essentialsMark Rackley
 
SharePointfest Denver - A jQuery Primer for SharePoint
SharePointfest Denver -  A jQuery Primer for SharePointSharePointfest Denver -  A jQuery Primer for SharePoint
SharePointfest Denver - A jQuery Primer for SharePointMarc D Anderson
 
J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012ghnash
 
jQuery Rescue Adventure
jQuery Rescue AdventurejQuery Rescue Adventure
jQuery Rescue AdventureAllegient
 
Testing in AngularJS
Testing in AngularJSTesting in AngularJS
Testing in AngularJSPeter Drinnan
 

Ähnlich wie 2010 07-18.wa.rails tdd-6 (20)

Test automation with selenide
Test automation with selenideTest automation with selenide
Test automation with selenide
 
Selenium testing
Selenium testingSelenium testing
Selenium testing
 
Selenium WebDriver with Java
Selenium WebDriver with JavaSelenium WebDriver with Java
Selenium WebDriver with Java
 
Automation with Selenium Presented by Quontra Solutions
Automation with Selenium Presented by Quontra SolutionsAutomation with Selenium Presented by Quontra Solutions
Automation with Selenium Presented by Quontra Solutions
 
Testing ASP.NET - Progressive.NET
Testing ASP.NET - Progressive.NETTesting ASP.NET - Progressive.NET
Testing ASP.NET - Progressive.NET
 
JQuery
JQueryJQuery
JQuery
 
JQuery
JQueryJQuery
JQuery
 
jQuery Performance Tips and Tricks
jQuery Performance Tips and TricksjQuery Performance Tips and Tricks
jQuery Performance Tips and Tricks
 
DirectToWeb 2.0
DirectToWeb 2.0DirectToWeb 2.0
DirectToWeb 2.0
 
Comprehensive Browser Automation Solution using Groovy, WebDriver & Obect Model
Comprehensive Browser Automation Solution using Groovy, WebDriver & Obect ModelComprehensive Browser Automation Solution using Groovy, WebDriver & Obect Model
Comprehensive Browser Automation Solution using Groovy, WebDriver & Obect Model
 
Automation - web testing with selenium
Automation - web testing with seleniumAutomation - web testing with selenium
Automation - web testing with selenium
 
Breaking the limits_of_page_objects
Breaking the limits_of_page_objectsBreaking the limits_of_page_objects
Breaking the limits_of_page_objects
 
Browser testing with nightwatch.js
Browser testing with nightwatch.jsBrowser testing with nightwatch.js
Browser testing with nightwatch.js
 
Javascript unit testing, yes we can e big
Javascript unit testing, yes we can   e bigJavascript unit testing, yes we can   e big
Javascript unit testing, yes we can e big
 
SharePoint Cincy 2012 - jQuery essentials
SharePoint Cincy 2012 - jQuery essentialsSharePoint Cincy 2012 - jQuery essentials
SharePoint Cincy 2012 - jQuery essentials
 
SharePointfest Denver - A jQuery Primer for SharePoint
SharePointfest Denver -  A jQuery Primer for SharePointSharePointfest Denver -  A jQuery Primer for SharePoint
SharePointfest Denver - A jQuery Primer for SharePoint
 
J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012
 
jQuery Rescue Adventure
jQuery Rescue AdventurejQuery Rescue Adventure
jQuery Rescue Adventure
 
Web driver training
Web driver trainingWeb driver training
Web driver training
 
Testing in AngularJS
Testing in AngularJSTesting in AngularJS
Testing in AngularJS
 

Mehr von Marakana Inc.

Android Services Black Magic by Aleksandar Gargenta
Android Services Black Magic by Aleksandar GargentaAndroid Services Black Magic by Aleksandar Gargenta
Android Services Black Magic by Aleksandar GargentaMarakana Inc.
 
Behavior Driven Development
Behavior Driven DevelopmentBehavior Driven Development
Behavior Driven DevelopmentMarakana Inc.
 
Martin Odersky: What's next for Scala
Martin Odersky: What's next for ScalaMartin Odersky: What's next for Scala
Martin Odersky: What's next for ScalaMarakana Inc.
 
Why Java Needs Hierarchical Data
Why Java Needs Hierarchical DataWhy Java Needs Hierarchical Data
Why Java Needs Hierarchical DataMarakana Inc.
 
Deep Dive Into Android Security
Deep Dive Into Android SecurityDeep Dive Into Android Security
Deep Dive Into Android SecurityMarakana Inc.
 
Pictures from "Learn about RenderScript" meetup at SF Android User Group
Pictures from "Learn about RenderScript" meetup at SF Android User GroupPictures from "Learn about RenderScript" meetup at SF Android User Group
Pictures from "Learn about RenderScript" meetup at SF Android User GroupMarakana Inc.
 
Android UI Tips, Tricks and Techniques
Android UI Tips, Tricks and TechniquesAndroid UI Tips, Tricks and Techniques
Android UI Tips, Tricks and TechniquesMarakana Inc.
 
Graphicsand animations devoxx2010 (1)
Graphicsand animations devoxx2010 (1)Graphicsand animations devoxx2010 (1)
Graphicsand animations devoxx2010 (1)Marakana Inc.
 
What's this jQuery? Where it came from, and how it will drive innovation
What's this jQuery? Where it came from, and how it will drive innovationWhat's this jQuery? Where it came from, and how it will drive innovation
What's this jQuery? Where it came from, and how it will drive innovationMarakana Inc.
 
jQuery State of the Union - Yehuda Katz
jQuery State of the Union - Yehuda KatzjQuery State of the Union - Yehuda Katz
jQuery State of the Union - Yehuda KatzMarakana Inc.
 
Pics from: "James Gosling on Apple, Apache, Google, Oracle and the Future of ...
Pics from: "James Gosling on Apple, Apache, Google, Oracle and the Future of ...Pics from: "James Gosling on Apple, Apache, Google, Oracle and the Future of ...
Pics from: "James Gosling on Apple, Apache, Google, Oracle and the Future of ...Marakana Inc.
 
Learn about JRuby Internals from one of the JRuby Lead Developers, Thomas Enebo
Learn about JRuby Internals from one of the JRuby Lead Developers, Thomas EneboLearn about JRuby Internals from one of the JRuby Lead Developers, Thomas Enebo
Learn about JRuby Internals from one of the JRuby Lead Developers, Thomas EneboMarakana Inc.
 
Replacing Java Incrementally
Replacing Java IncrementallyReplacing Java Incrementally
Replacing Java IncrementallyMarakana Inc.
 
Learn to Build like you Code with Apache Buildr
Learn to Build like you Code with Apache BuildrLearn to Build like you Code with Apache Buildr
Learn to Build like you Code with Apache BuildrMarakana Inc.
 
Learn How to Unit Test Your Android Application (with Robolectric)
Learn How to Unit Test Your Android Application (with Robolectric)Learn How to Unit Test Your Android Application (with Robolectric)
Learn How to Unit Test Your Android Application (with Robolectric)Marakana Inc.
 
Learn Learn how to build your mobile back-end with MongoDB
Learn Learn how to build your mobile back-end with MongoDBLearn Learn how to build your mobile back-end with MongoDB
Learn Learn how to build your mobile back-end with MongoDBMarakana Inc.
 
A hands on overview of the semantic web
A hands on overview of the semantic webA hands on overview of the semantic web
A hands on overview of the semantic webMarakana Inc.
 

Mehr von Marakana Inc. (20)

Android Services Black Magic by Aleksandar Gargenta
Android Services Black Magic by Aleksandar GargentaAndroid Services Black Magic by Aleksandar Gargenta
Android Services Black Magic by Aleksandar Gargenta
 
JRuby at Square
JRuby at SquareJRuby at Square
JRuby at Square
 
Behavior Driven Development
Behavior Driven DevelopmentBehavior Driven Development
Behavior Driven Development
 
Martin Odersky: What's next for Scala
Martin Odersky: What's next for ScalaMartin Odersky: What's next for Scala
Martin Odersky: What's next for Scala
 
Why Java Needs Hierarchical Data
Why Java Needs Hierarchical DataWhy Java Needs Hierarchical Data
Why Java Needs Hierarchical Data
 
Deep Dive Into Android Security
Deep Dive Into Android SecurityDeep Dive Into Android Security
Deep Dive Into Android Security
 
Securing Android
Securing AndroidSecuring Android
Securing Android
 
Pictures from "Learn about RenderScript" meetup at SF Android User Group
Pictures from "Learn about RenderScript" meetup at SF Android User GroupPictures from "Learn about RenderScript" meetup at SF Android User Group
Pictures from "Learn about RenderScript" meetup at SF Android User Group
 
Android UI Tips, Tricks and Techniques
Android UI Tips, Tricks and TechniquesAndroid UI Tips, Tricks and Techniques
Android UI Tips, Tricks and Techniques
 
Graphicsand animations devoxx2010 (1)
Graphicsand animations devoxx2010 (1)Graphicsand animations devoxx2010 (1)
Graphicsand animations devoxx2010 (1)
 
What's this jQuery? Where it came from, and how it will drive innovation
What's this jQuery? Where it came from, and how it will drive innovationWhat's this jQuery? Where it came from, and how it will drive innovation
What's this jQuery? Where it came from, and how it will drive innovation
 
jQuery State of the Union - Yehuda Katz
jQuery State of the Union - Yehuda KatzjQuery State of the Union - Yehuda Katz
jQuery State of the Union - Yehuda Katz
 
Pics from: "James Gosling on Apple, Apache, Google, Oracle and the Future of ...
Pics from: "James Gosling on Apple, Apache, Google, Oracle and the Future of ...Pics from: "James Gosling on Apple, Apache, Google, Oracle and the Future of ...
Pics from: "James Gosling on Apple, Apache, Google, Oracle and the Future of ...
 
Learn about JRuby Internals from one of the JRuby Lead Developers, Thomas Enebo
Learn about JRuby Internals from one of the JRuby Lead Developers, Thomas EneboLearn about JRuby Internals from one of the JRuby Lead Developers, Thomas Enebo
Learn about JRuby Internals from one of the JRuby Lead Developers, Thomas Enebo
 
Replacing Java Incrementally
Replacing Java IncrementallyReplacing Java Incrementally
Replacing Java Incrementally
 
Learn to Build like you Code with Apache Buildr
Learn to Build like you Code with Apache BuildrLearn to Build like you Code with Apache Buildr
Learn to Build like you Code with Apache Buildr
 
Learn How to Unit Test Your Android Application (with Robolectric)
Learn How to Unit Test Your Android Application (with Robolectric)Learn How to Unit Test Your Android Application (with Robolectric)
Learn How to Unit Test Your Android Application (with Robolectric)
 
Learn Learn how to build your mobile back-end with MongoDB
Learn Learn how to build your mobile back-end with MongoDBLearn Learn how to build your mobile back-end with MongoDB
Learn Learn how to build your mobile back-end with MongoDB
 
A hands on overview of the semantic web
A hands on overview of the semantic webA hands on overview of the semantic web
A hands on overview of the semantic web
 
Jena framework
Jena frameworkJena framework
Jena framework
 

2010 07-18.wa.rails tdd-6

  • 1. Efficient Rails Test-Driven Development — Week 6 Wolfram Arnold www.rubyfocus.biz In collaboration with: Sarah Allen, BlazingCloud.net marakana.com
  • 2. Integration Frameworks Cucumber, Webrat, Capybara,... lightweight browser without JS fast easy to set up great for API testing useful for exploratory application testing
  • 3. Integration Frameworks Selenium drives a browser (Firefox, IE, Safari, Chrome...) slow many moving pieces many technology choices great if client/server testing is a must useful for exploratory application testing
  • 5. Console Experiments irb > require 'support/boot' > Page.start_new_browser_session > ... > Homepage.visit > Page.selenium_driver.try_something_here > ... > Page.close_current_browser_session
  • 6. Procedural Selenium Test page.open page.type “username”, “joe” page.type “password”, “secret password” page.click “submit”, :wait_for => :page page.text?(“My HomePage”).should be_true page.click “link=New Messages” ...
  • 7. Problems with this ● Very low level – hard to see what's being checked – hard to reuse – prone to copy & paste ● Next step – pull out common methods – but common file quickly gets cluttered – most tests only use a small fraction of common methods
  • 9. Page Objects Each Page represented by an Object Methods are – things you can do – things you can see – apply to this page only Each method returns a page object – itself – another page object
  • 10. Advantages ● Reuse is easy ● Documentation ● Maintainability – Changes to the UI or page require only one change of the test code, in only one place ● Method Chaining – Development of new tests is easy and fast – Easy reuse of existing “library of things to do” on a page
  • 11. How to build Page Objects? Model the UI in Page Objects entire pages parts of a page
  • 13. Locators Selenium Locators “link=Sign in” “username” CSS Locators “css=ul.global_nav_list li” “css=input[name=username]” XPath Locators “xpath=//ul[@class='global_nav_list']/li” “xpath=//input[@name='username']”
  • 14. Selenium Locators—Default “identifier” matches: identifier keyword id attribute is optional element(“identifier=navigation”) This is the default finds: <div id=”navigation”> locator name attribute element(“identifier=username”) finds: <input name=”username”> Note: matches id first, if not found, looks for name attribute
  • 15. Other Selenium Loctors Explicit “id” locator: matches id attribute element(“id=navigation”) finds: <div id=”navigation”> Explicit “name” locator: matches name attribute element(“name=username”) finds: <input name=”username”>
  • 16. More on name locator The “name” locator may be followed by filters: element(“name=usename value=Joe”) Supported filters: value=value pattern index=element index, starting at 0
  • 17. Link Locators The “link” locator is used for links: element(“link=Sign in”) matches the text (pattern) of an a tag: <a href=”...”>Sign in</a>
  • 18. Select from Drop-Down select(selectLocator, optionLocator) optionLocator matches on label by default Example: select(“name=language”,”French”)
  • 19. Text Verification glob:pattern Similar to filename matching no command line: * matches any sequence of characters ? matches any single character regexp:pattern match against a regular expression regexpi:pattern match against a regular expression, case-insensitive exact:string exact match
  • 20. String matching examples “Welcome John Smith” glob:Welcome* regexp:Welcome.* regexpi:welcome.* exact:Welcome John Smith If nothing is specified, glob is the default.
  • 21. CSS Locators css=a selects <a> tag css=a.some_class selects <a class=”some_class”> css=a#some_id selects <a id=”some_id”> css=a.some_class#some_id selects <a id=”some_id” class=”some_class”>
  • 22. CSS Locators: Attributes css=input[name=username] selects <input name=”username”> css=img[src*=”btn_img”] selects <img src=”http://example.com/btn_img?1234> *= partial match ^= match beginning $= match end
  • 23. CSS Locators Chained tag1 tag2 tag1+tag2 decendents siblings css=ul.navlist li a css=input+a <ul class=”navlist”> <input> <li> <a>Some link</a> <a></a> </li> <ul>
  • 25. When to Wait? An Event Trigger Click Mouse over/out Form Submit Waiting for: Page load AJAX load JavaScript action (e.g. hide/show)
  • 26. Waiting for Page open “/” will automatically wait for page clicking on a link wait_for :page
  • 27. Wait for Element Used for: Ajax, JavaScript click “locator”, :wait_for => :element, :element => “locator”
  • 28. Wait for Visibility Something appearing or disappearing dynamically click “locator”, :wait_for => :visible, :element => “locator” Note: The element must be present (see wait for element) or visibility check will fail with a “no element found error”
  • 30. [1,2,3] == [2,3,1] ([1,2,3] – [2,3,1]).should be_empty [1,2,3].should be_commutative_with([2,3,1]) http://wiki.github.com/dchelimsky/rspec/custom- matchers http://railscasts.com/episodes/157-rspec- matchers-macros
  • 32. Access Control Controller access Disallowed action should be blocked via before_filters Most important! View access Disallowed pages/actions should not be linked to Purely cosmetic
  • 33. Devise http://github.com/plataformatec/devise Latest in Authorization Plugins Comes with controllers and views password recovery feature, etc. Rack-based
  • 35. Test-Driven Development Benefit #1: Better Design Testing as-you-go makes code better structured. Testing emphasizes decoupling. Fat model, skinny controller becomes easier. Benefit #2: Focus & Project Management If you don't know what to test for, how can you know what to code for? Knowing when you're done.
  • 36. Test-Driven Development Benefit #3: Documentation & Collaboration Tests are documentation that's always current. Tests illustrate how code is meant to be used. Benefit #4: Creation of Tests Testing happens in-situ, not after the fact When you're done coding, you've got a full test suite No tedious chores and guilt afterwards
  • 37. Inside-Out vs. Outside-In Start with what you understand can be model, view or controller When you discover you need something else... make failing tests pending implement what you're missing resume Discover the application inside out.