SlideShare a Scribd company logo
1 of 34
Using the Page Object
                       Pattern to Improve
                  Functional Test Maintainability
                            Dante Briones
                                Principal Consultant
                              Electronic Ingenuity LLC
                           http://electronicingenuity.com




Friday, 07 August, 2009
The Page Object Pattern

                     • Technique for structuring test code that:
                      • Promotes reuse and reduces duplication
                      • Makes tests readable
                      • Makes tests more robust (less brittle)
                      • Improves maintainability, particularly if
                          the application is rapidly evolving


Friday, 07 August, 2009
The Page Object Pattern

                     • Demos will be coded in Java
                     • Everything we discuss is applicable to other
                          strongly-typed languages (e.g. C#)
                     • Most things are applicable to dynamically-
                          typed languages (e.g. Ruby, Python, Perl)



Friday, 07 August, 2009
Selenium IDE
                                     Quick Poll


                     • How many using Se IDE?
                     • How many create tests primarily through
                          record & playback?




Friday, 07 August, 2009
Selenium IDE
                               It’s Great Because:

                     • It’s Free
                     • It’s Free
                     • Requires little or no programming
                          experience




Friday, 07 August, 2009
Selenium IDE
                           It falls short because:

                     • Selenese is not a programming language
                      • No support for loops/iteration
                      • No support for conditionals (if...else)
                      • No way to eliminate duplicated code!

Friday, 07 August, 2009
Selenium IDE
                            Why is duplication bad?

                     • Has this ever happened to you?
                      • A tiny change to the app causes several
                            test failures?
                          • Let’s see an example


Friday, 07 August, 2009
Demo



Friday, 07 August, 2009
Selenium IDE
                           Why is duplication bad?


                     • Fixing the tests will require more work
                          than the change that caused the failure
                     • Duplication makes code less maintainable


Friday, 07 August, 2009
Selenium IDE
                                       Summary


                          • Great productivity for the first few tests
                          • Maintainability decreases as the test suite
                            grows
                          • How can we do better?

Friday, 07 August, 2009
Start Coding Your Tests
                                             a.k.a.
                          “Write tests in a programming language”




Friday, 07 August, 2009
Coding Your Tests
                                 Quick Poll



                     • How many are using Selenium RC?



Friday, 07 August, 2009
Coding Your Tests
                              How does this help?


                     • We can eliminate duplication by building
                          abstractions
                     • Let’s see an example of this


Friday, 07 August, 2009
Demo



Friday, 07 August, 2009
Coding Your Tests
                              What happens next?

                     • All commonly used methods get pulled into
                          the base class of all tests
                     • As the test suite grows, so does the base
                          class
                     • Uh oh...the base class starts getting too big

Friday, 07 August, 2009
Coding Your Tests
                              How Big is Too Big?

                     • You can’t tell someone else has already
                          written the code you’re about to write
                     • Frequent version control conflicts
                     • Maintenance is an issue again


Friday, 07 August, 2009
Coding Your Tests
                 How to rein in your base class?
                     • Observation: For any given page, most of
                          the behavior does not apply
                     • Example: you can’t change your password
                          on the “view cart” page
                     • What if we moved the page-specific
                          behavior into a corresponding class?
                     • Voila! The Page Object is born
Friday, 07 August, 2009
The Page Object



Friday, 07 August, 2009
The Page Object
                                What does it do?


                     • Consolidates the code for interacting with
                          any given UI element
                     • Allows you to model the UI in your tests

Friday, 07 August, 2009
The Page Object
                                 What does it do?
                     • Exposes methods that reflect the things a
                          user can see and do on that page, e.g.
                          • addItemToCart(), getPrice()
                          • getEntryTitle(), saveDraft(), publishEntry()
                     • Hides the details of telling the browser
                          how to do those things


Friday, 07 August, 2009
Demo



Friday, 07 August, 2009
The Page Object
                                     Results

                     • Test code is very readable
                     • No duplication of Selenium API calls
                     • Interactive documentation via Ctrl-Space
                     • Eminently reusable code

Friday, 07 August, 2009
The Page Object
                                        Results

                     • This technique is framework-agnostic
                      • Has been used with Selenium, WebDriver
                            (Selenium 2.0), Watir, and others
                          • I’ve even used it to drive desktop apps


Friday, 07 August, 2009
The Page Object
                                        Results

                     • Because it encapsulates the details of the
                          test framework API:
                          • You can use this technique to simplify the
                            process of swapping frameworks
                          • In theory, you only need to reimplement
                            the Page base class.



Friday, 07 August, 2009
But wait, there’s more!
                                   It gets better!

                     • Page Objects improve maintainability
                     • They also provide an opportunity to make
                          test creation extremely quick (and maybe
                          even fun!)
                     • How? Page Objects + Method Chaining

Friday, 07 August, 2009
Method Chaining
                          What’s method chaining?


                     • Change the signature of user behavior
                          methods so that they return the page
                          object itself
                     • Allows multiple behavior methods to be
                          called in sequence



Friday, 07 August, 2009
Method Chaining
                            What will it do for me?


                          • Removes for “code clutter”
                          • Tests become even more readable


Friday, 07 August, 2009
Method Chaining
                    How does it work in practice?
                     • All Page Object methods return a
                          reference to a Page Object
                          • If the “user action” moves the focus to a
                            different page, the method should return
                            that page object
                          • Otherwise, return the same page object
                     • Time for an example
Friday, 07 August, 2009
Demo



Friday, 07 August, 2009
Method Chaining
                                      Results


                     • Test code is even more readable
                     • Very quick to write
                     • (Actually kinda fun. Don’t tell anyone!)


Friday, 07 August, 2009
Method Chaining
                                       Caveats


                     • Dependent on tool support
                     • Lots of options: IntelliJ IDEA, Eclipse,
                          NetBeans, VisualStudio




Friday, 07 August, 2009
Method Chaining
                                       Caveats
                     • Tool support depends on language
                      • Excellent support for strongly typed
                            languages like Java and C#
                          • Support is “up and coming” for
                            dynamically typed languages (Ruby,
                            Python, and Perl)



Friday, 07 August, 2009
The Page Object Pattern
                     • Technique for organizing test code that:
                      • Promotes reuse and reduces duplication
                      • Makes tests readable
                      • Makes tests more robust (less brittle)
                      • Improves maintainability, particularly if
                            the application is rapidly evolving
                          • Is just as quick record & playback!
Friday, 07 August, 2009
Q &A



Friday, 07 August, 2009

More Related Content

What's hot

[Webinar] Continuous Testing Done Right: Test Automation at the World's Leadi...
[Webinar] Continuous Testing Done Right: Test Automation at the World's Leadi...[Webinar] Continuous Testing Done Right: Test Automation at the World's Leadi...
[Webinar] Continuous Testing Done Right: Test Automation at the World's Leadi...
Applitools
 
UI Testing Automation
UI Testing AutomationUI Testing Automation
UI Testing Automation
AgileEngine
 

What's hot (20)

Test Driven Development in CQ5/AEM
Test Driven Development in CQ5/AEMTest Driven Development in CQ5/AEM
Test Driven Development in CQ5/AEM
 
DSL, Page Object and WebDriver – the path to reliable functional tests.pptx
DSL, Page Object and WebDriver – the path to reliable functional tests.pptxDSL, Page Object and WebDriver – the path to reliable functional tests.pptx
DSL, Page Object and WebDriver – the path to reliable functional tests.pptx
 
Automated Testing in Angular Slides
Automated Testing in Angular SlidesAutomated Testing in Angular Slides
Automated Testing in Angular Slides
 
Cucumber ppt
Cucumber pptCucumber ppt
Cucumber ppt
 
[Webinar] Continuous Testing Done Right: Test Automation at the World's Leadi...
[Webinar] Continuous Testing Done Right: Test Automation at the World's Leadi...[Webinar] Continuous Testing Done Right: Test Automation at the World's Leadi...
[Webinar] Continuous Testing Done Right: Test Automation at the World's Leadi...
 
Better Page Object Handling with Loadable Component Pattern
Better Page Object Handling with Loadable Component PatternBetter Page Object Handling with Loadable Component Pattern
Better Page Object Handling with Loadable Component Pattern
 
Selenium WebDriver - Test automation for web applications
Selenium WebDriver - Test automation for web applicationsSelenium WebDriver - Test automation for web applications
Selenium WebDriver - Test automation for web applications
 
Patterns of a “good” test automation framework
Patterns of a “good” test automation frameworkPatterns of a “good” test automation framework
Patterns of a “good” test automation framework
 
Introduction To Web Application Testing
Introduction To Web Application TestingIntroduction To Web Application Testing
Introduction To Web Application Testing
 
Selenium Tips & Tricks
Selenium Tips & TricksSelenium Tips & Tricks
Selenium Tips & Tricks
 
UI Testing Automation
UI Testing AutomationUI Testing Automation
UI Testing Automation
 
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
 
Angular Unit Testing
Angular Unit TestingAngular Unit Testing
Angular Unit Testing
 
An easy way to automate complex UI
An easy way to automate complex UIAn easy way to automate complex UI
An easy way to automate complex UI
 
Test Automation With Cucumber JVM, Selenium, and Mocha
Test Automation With Cucumber JVM, Selenium, and MochaTest Automation With Cucumber JVM, Selenium, and Mocha
Test Automation With Cucumber JVM, Selenium, and Mocha
 
Visual Automation Framework via Screenshot Comparison
Visual Automation Framework via Screenshot ComparisonVisual Automation Framework via Screenshot Comparison
Visual Automation Framework via Screenshot Comparison
 
Protractor end-to-end testing framework for angular js
Protractor   end-to-end testing framework for angular jsProtractor   end-to-end testing framework for angular js
Protractor end-to-end testing framework for angular js
 
Design patterns in test automation
Design patterns in test automationDesign patterns in test automation
Design patterns in test automation
 
Automation Frame works Instruction Sheet
Automation Frame works Instruction SheetAutomation Frame works Instruction Sheet
Automation Frame works Instruction Sheet
 
Colorful world-of-visual-automation-testing-latest
Colorful world-of-visual-automation-testing-latestColorful world-of-visual-automation-testing-latest
Colorful world-of-visual-automation-testing-latest
 

Viewers also liked

Automation testing strategy, approach & planning
Automation testing  strategy, approach & planningAutomation testing  strategy, approach & planning
Automation testing strategy, approach & planning
SivaprasanthRentala1975
 

Viewers also liked (20)

Beyond Page Objects
Beyond Page ObjectsBeyond Page Objects
Beyond Page Objects
 
Page Objects Done Right - selenium conference 2014
Page Objects Done Right - selenium conference 2014Page Objects Done Right - selenium conference 2014
Page Objects Done Right - selenium conference 2014
 
Automation Abstraction Layers: Page Objects and Beyond
Automation Abstraction Layers: Page Objects and BeyondAutomation Abstraction Layers: Page Objects and Beyond
Automation Abstraction Layers: Page Objects and Beyond
 
A journey beyond the page object pattern
A journey beyond the page object patternA journey beyond the page object pattern
A journey beyond the page object pattern
 
DSL, Page Object and Selenium – a way to reliable functional tests
DSL, Page Object and Selenium – a way to reliable functional testsDSL, Page Object and Selenium – a way to reliable functional tests
DSL, Page Object and Selenium – a way to reliable functional tests
 
Introduction to Selenium Web Driver
Introduction to Selenium Web DriverIntroduction to Selenium Web Driver
Introduction to Selenium Web Driver
 
Test Automation Framework Designs
Test Automation Framework DesignsTest Automation Framework Designs
Test Automation Framework Designs
 
Automation Testing by Selenium Web Driver
Automation Testing by Selenium Web DriverAutomation Testing by Selenium Web Driver
Automation Testing by Selenium Web Driver
 
Automation Testing using Selenium
Automation Testing using SeleniumAutomation Testing using Selenium
Automation Testing using Selenium
 
Hands on Exploration of Page Objects and Abstraction Layers with Selenium Web...
Hands on Exploration of Page Objects and Abstraction Layers with Selenium Web...Hands on Exploration of Page Objects and Abstraction Layers with Selenium Web...
Hands on Exploration of Page Objects and Abstraction Layers with Selenium Web...
 
Software Estimation Techniques
Software Estimation TechniquesSoftware Estimation Techniques
Software Estimation Techniques
 
Selenium ppt
Selenium pptSelenium ppt
Selenium ppt
 
Selenium - The page object pattern
Selenium - The page object patternSelenium - The page object pattern
Selenium - The page object pattern
 
Web Test Automation with Selenium
Web Test Automation with SeleniumWeb Test Automation with Selenium
Web Test Automation with Selenium
 
Software cost estimation
Software cost estimationSoftware cost estimation
Software cost estimation
 
Automation testing strategy, approach & planning
Automation testing  strategy, approach & planningAutomation testing  strategy, approach & planning
Automation testing strategy, approach & planning
 
Refactoring page objects The Screenplay Pattern
Refactoring page objects   The Screenplay Pattern Refactoring page objects   The Screenplay Pattern
Refactoring page objects The Screenplay Pattern
 
Advanced Selenium Workshop
Advanced Selenium WorkshopAdvanced Selenium Workshop
Advanced Selenium Workshop
 
Nodejs - A quick tour (v6)
Nodejs - A quick tour (v6)Nodejs - A quick tour (v6)
Nodejs - A quick tour (v6)
 
Specifications For Enterprise Testing
Specifications For Enterprise TestingSpecifications For Enterprise Testing
Specifications For Enterprise Testing
 

Similar to Using The Page Object Pattern

PHPUnit & Continuous Integration: An Introduction
PHPUnit & Continuous Integration: An IntroductionPHPUnit & Continuous Integration: An Introduction
PHPUnit & Continuous Integration: An Introduction
alexmace
 
2011 JavaOne Apache TomEE Java EE 6 Web Profile
2011 JavaOne Apache TomEE Java EE 6 Web Profile2011 JavaOne Apache TomEE Java EE 6 Web Profile
2011 JavaOne Apache TomEE Java EE 6 Web Profile
David Blevins
 
DjangoCon 2013 - How to Write Fast and Efficient Unit Tests in Django
DjangoCon 2013 - How to Write Fast and Efficient Unit Tests in DjangoDjangoCon 2013 - How to Write Fast and Efficient Unit Tests in Django
DjangoCon 2013 - How to Write Fast and Efficient Unit Tests in Django
Casey Kinsey
 
Test Driven Sysadmin
Test Driven SysadminTest Driven Sysadmin
Test Driven Sysadmin
johanku
 
Why Your Selenium Tests are so Dang Brittle, and What to Do About It
Why Your Selenium Tests are so Dang Brittle, and What to Do About ItWhy Your Selenium Tests are so Dang Brittle, and What to Do About It
Why Your Selenium Tests are so Dang Brittle, and What to Do About It
Jay Aho
 

Similar to Using The Page Object Pattern (20)

PHPUnit & Continuous Integration: An Introduction
PHPUnit & Continuous Integration: An IntroductionPHPUnit & Continuous Integration: An Introduction
PHPUnit & Continuous Integration: An Introduction
 
Web Application Testing with Selenium
Web Application Testing with Selenium Web Application Testing with Selenium
Web Application Testing with Selenium
 
TLC2018 Shyam Sunder: Legoizing Testing
TLC2018 Shyam Sunder: Legoizing TestingTLC2018 Shyam Sunder: Legoizing Testing
TLC2018 Shyam Sunder: Legoizing Testing
 
How To Make Your Testing More Groovy
How To Make Your Testing More GroovyHow To Make Your Testing More Groovy
How To Make Your Testing More Groovy
 
Generalization in Auto-Testing. How we put what we had into new Technological...
Generalization in Auto-Testing. How we put what we had into new Technological...Generalization in Auto-Testing. How we put what we had into new Technological...
Generalization in Auto-Testing. How we put what we had into new Technological...
 
2011 JavaOne Apache TomEE Java EE 6 Web Profile
2011 JavaOne Apache TomEE Java EE 6 Web Profile2011 JavaOne Apache TomEE Java EE 6 Web Profile
2011 JavaOne Apache TomEE Java EE 6 Web Profile
 
Plone Testing Tools And Techniques
Plone Testing Tools And TechniquesPlone Testing Tools And Techniques
Plone Testing Tools And Techniques
 
Enterprise Strength Mobile JavaScript
Enterprise Strength Mobile JavaScriptEnterprise Strength Mobile JavaScript
Enterprise Strength Mobile JavaScript
 
Useful automation
Useful automationUseful automation
Useful automation
 
FreshAir2008
FreshAir2008FreshAir2008
FreshAir2008
 
FreshAir2008
FreshAir2008FreshAir2008
FreshAir2008
 
SVCC 2011 - 0 - 60: QA Automation @ Box
SVCC 2011 - 0 - 60: QA Automation @ BoxSVCC 2011 - 0 - 60: QA Automation @ Box
SVCC 2011 - 0 - 60: QA Automation @ Box
 
DjangoCon 2013 - How to Write Fast and Efficient Unit Tests in Django
DjangoCon 2013 - How to Write Fast and Efficient Unit Tests in DjangoDjangoCon 2013 - How to Write Fast and Efficient Unit Tests in Django
DjangoCon 2013 - How to Write Fast and Efficient Unit Tests in Django
 
Sap inside track Munich 2017
Sap inside track Munich 2017Sap inside track Munich 2017
Sap inside track Munich 2017
 
Opticon 2017 How Developers Can Take Experimentation
Opticon 2017 How Developers Can Take ExperimentationOpticon 2017 How Developers Can Take Experimentation
Opticon 2017 How Developers Can Take Experimentation
 
Growing Trends of Open Source UI Frameworks
Growing Trends of Open Source UI FrameworksGrowing Trends of Open Source UI Frameworks
Growing Trends of Open Source UI Frameworks
 
Test Driven Sysadmin
Test Driven SysadminTest Driven Sysadmin
Test Driven Sysadmin
 
Web Test Automation Framework - IndicThreads Conference
Web Test Automation Framework  - IndicThreads ConferenceWeb Test Automation Framework  - IndicThreads Conference
Web Test Automation Framework - IndicThreads Conference
 
Why Your Selenium Tests are so Dang Brittle, and What to Do About It
Why Your Selenium Tests are so Dang Brittle, and What to Do About ItWhy Your Selenium Tests are so Dang Brittle, and What to Do About It
Why Your Selenium Tests are so Dang Brittle, and What to Do About It
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Recently uploaded (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 

Using The Page Object Pattern

  • 1. Using the Page Object Pattern to Improve Functional Test Maintainability Dante Briones Principal Consultant Electronic Ingenuity LLC http://electronicingenuity.com Friday, 07 August, 2009
  • 2. The Page Object Pattern • Technique for structuring test code that: • Promotes reuse and reduces duplication • Makes tests readable • Makes tests more robust (less brittle) • Improves maintainability, particularly if the application is rapidly evolving Friday, 07 August, 2009
  • 3. The Page Object Pattern • Demos will be coded in Java • Everything we discuss is applicable to other strongly-typed languages (e.g. C#) • Most things are applicable to dynamically- typed languages (e.g. Ruby, Python, Perl) Friday, 07 August, 2009
  • 4. Selenium IDE Quick Poll • How many using Se IDE? • How many create tests primarily through record & playback? Friday, 07 August, 2009
  • 5. Selenium IDE It’s Great Because: • It’s Free • It’s Free • Requires little or no programming experience Friday, 07 August, 2009
  • 6. Selenium IDE It falls short because: • Selenese is not a programming language • No support for loops/iteration • No support for conditionals (if...else) • No way to eliminate duplicated code! Friday, 07 August, 2009
  • 7. Selenium IDE Why is duplication bad? • Has this ever happened to you? • A tiny change to the app causes several test failures? • Let’s see an example Friday, 07 August, 2009
  • 9. Selenium IDE Why is duplication bad? • Fixing the tests will require more work than the change that caused the failure • Duplication makes code less maintainable Friday, 07 August, 2009
  • 10. Selenium IDE Summary • Great productivity for the first few tests • Maintainability decreases as the test suite grows • How can we do better? Friday, 07 August, 2009
  • 11. Start Coding Your Tests a.k.a. “Write tests in a programming language” Friday, 07 August, 2009
  • 12. Coding Your Tests Quick Poll • How many are using Selenium RC? Friday, 07 August, 2009
  • 13. Coding Your Tests How does this help? • We can eliminate duplication by building abstractions • Let’s see an example of this Friday, 07 August, 2009
  • 15. Coding Your Tests What happens next? • All commonly used methods get pulled into the base class of all tests • As the test suite grows, so does the base class • Uh oh...the base class starts getting too big Friday, 07 August, 2009
  • 16. Coding Your Tests How Big is Too Big? • You can’t tell someone else has already written the code you’re about to write • Frequent version control conflicts • Maintenance is an issue again Friday, 07 August, 2009
  • 17. Coding Your Tests How to rein in your base class? • Observation: For any given page, most of the behavior does not apply • Example: you can’t change your password on the “view cart” page • What if we moved the page-specific behavior into a corresponding class? • Voila! The Page Object is born Friday, 07 August, 2009
  • 18. The Page Object Friday, 07 August, 2009
  • 19. The Page Object What does it do? • Consolidates the code for interacting with any given UI element • Allows you to model the UI in your tests Friday, 07 August, 2009
  • 20. The Page Object What does it do? • Exposes methods that reflect the things a user can see and do on that page, e.g. • addItemToCart(), getPrice() • getEntryTitle(), saveDraft(), publishEntry() • Hides the details of telling the browser how to do those things Friday, 07 August, 2009
  • 22. The Page Object Results • Test code is very readable • No duplication of Selenium API calls • Interactive documentation via Ctrl-Space • Eminently reusable code Friday, 07 August, 2009
  • 23. The Page Object Results • This technique is framework-agnostic • Has been used with Selenium, WebDriver (Selenium 2.0), Watir, and others • I’ve even used it to drive desktop apps Friday, 07 August, 2009
  • 24. The Page Object Results • Because it encapsulates the details of the test framework API: • You can use this technique to simplify the process of swapping frameworks • In theory, you only need to reimplement the Page base class. Friday, 07 August, 2009
  • 25. But wait, there’s more! It gets better! • Page Objects improve maintainability • They also provide an opportunity to make test creation extremely quick (and maybe even fun!) • How? Page Objects + Method Chaining Friday, 07 August, 2009
  • 26. Method Chaining What’s method chaining? • Change the signature of user behavior methods so that they return the page object itself • Allows multiple behavior methods to be called in sequence Friday, 07 August, 2009
  • 27. Method Chaining What will it do for me? • Removes for “code clutter” • Tests become even more readable Friday, 07 August, 2009
  • 28. Method Chaining How does it work in practice? • All Page Object methods return a reference to a Page Object • If the “user action” moves the focus to a different page, the method should return that page object • Otherwise, return the same page object • Time for an example Friday, 07 August, 2009
  • 30. Method Chaining Results • Test code is even more readable • Very quick to write • (Actually kinda fun. Don’t tell anyone!) Friday, 07 August, 2009
  • 31. Method Chaining Caveats • Dependent on tool support • Lots of options: IntelliJ IDEA, Eclipse, NetBeans, VisualStudio Friday, 07 August, 2009
  • 32. Method Chaining Caveats • Tool support depends on language • Excellent support for strongly typed languages like Java and C# • Support is “up and coming” for dynamically typed languages (Ruby, Python, and Perl) Friday, 07 August, 2009
  • 33. The Page Object Pattern • Technique for organizing test code that: • Promotes reuse and reduces duplication • Makes tests readable • Makes tests more robust (less brittle) • Improves maintainability, particularly if the application is rapidly evolving • Is just as quick record & playback! Friday, 07 August, 2009
  • 34. Q &A Friday, 07 August, 2009