KEMBAR78
Design Patterns in Automation | PDF
@BagmarAnand
Design Patterns in
Test Automation
Anand Bagmar
Software Quality Evangelist
@BagmarAnand
BagmarAnand
https://www.linkedin.com/in/anandbagmar/
About Me
Design Principles
@BagmarAnand
@BagmarAnand
SOLID Design Principles
https://devopedia.org/solid-design-principles
@BagmarAnand
Design Principles
• Principle of Least Knowledge – Talk only to your immediate friends
• Loose Coupled Principle – Loosely coupled design between objects that interact
What is a Pattern?
@BagmarAnand
@BagmarAnand
https://en.wikipedia.org/wiki/Software_design_pattern
@BagmarAnand
Have you heard-of,
or, used any
Pattern(s) in Test Automation?
@BagmarAnand
@BagmarAnand
Common patterns used in test automation
• Page-Object
• Singleton
• Composition
• Factory Method
• Abstract Factory
• Builder
• Decorator
• Observer
• Command
• Adapter
• Facade
Functional Test
Automation
@BagmarAnand
Code sample - #1
@BagmarAnand
Applying structure
to Functional Test
Automation
@BagmarAnand
Page-Object Pattern
@BagmarAnand
@BagmarAnand
Page Object Pattern
Model pages
in code
Simulates user actions Reduces code duplication
One place change
Code sample - #2
@BagmarAnand
@BagmarAnand
Page Object Pattern
Model pages
in code
Simulates user actions
One place change
Reduces code duplication
Snippets
of page
Code sample - #3
@BagmarAnand
@BagmarAnand
Automation framework with Page Objects
@BagmarAnand
Limitations of Page-Object Pattern
• Test intent gets polluted
• Duplication of Test intent & implementation
• Intent becomes Imperative
• Maintenance challenges
• Scaling challenges
Business-Layer
Page-Object Pattern
@BagmarAnand
@BagmarAnand
Automation framework with Page Objects
@BagmarAnand
Business-layer Page-Object Pattern
Code sample - #3
@BagmarAnand
@BagmarAnand
Advantages of
Business-layer Page-object Pattern
• Validate what is important - Business requirements
• Test Pyramid remains sane
• Abstraction layers allow separation-of-concerns
• Changes are isolated
• Maintenance & Scaling becomes ‘easier’
Test Data Patterns
@BagmarAnand
Why do we need to
think differently
about Test Data?
@BagmarAnand
@BagmarAnand
Criteria for Test Data
• Data is complex
• Needs to mimic “real” data
• Needs to be unique
• Data can be nested
• Though specified as static, may need to be Dynamic
• Data can be shared and reused
@BagmarAnand
Different ways to specify Test Data
• In Test implementation
• In Test specification / intent
• In code … separate data structures / classes / etc.
• External files
@BagmarAnand
Test Data Specification Examples
• Excel
• CSV
• Property
• XML
• YAML
• Database
• Json
Code sample - #4
@BagmarAnand
@BagmarAnand
Criteria for selection
• Easy to specify
• Easy to read and consume (by test framework)
• Ability to override specified data, easily
• Usable
@BagmarAnand
Tips for implementation
• Consistent way to specify test data
• Read the data – as Business Entities
• Override as appropriate
• Create DSL to give meaning to data
• Use in test implementation
• Implement Test Data Entity Utilities – Build, Equals, Copy, Find, etc.
Locators’ Patterns
@BagmarAnand
@BagmarAnand
Different ways to specify element locators
• In Page-Objects
• In separate files, per Page-Object
• In external files / locator files
Advantages of
using Patterns for
Test Automation
@BagmarAnand
@BagmarAnand
Advantages of Design Patterns
• Well known, well understood
• Tried & tested solutions for common problems
• Reduces complexity
• Language neutral
• Aid in communication
@BagmarAnand
Advantages of Design Patterns in Test Automation
Saves time & effort
(eventually)
Single ownership
Test Automation Code is of Production Quality!
Single point of change
Easy to –
- Implement
- Maintain
- Debug
- Scale
@BagmarAnand
@BagmarAnand
Which is the “best” pattern to use ?
https://cdn.pixabay.com/photo/2016/02/16/02/17/hands-1202488_1280.png
It DEPENDS!
on the
Context!
@BagmarAnand
@BagmarAnand
References
• SOLID Design Principles
• https://www.monterail.com/hubfs/PDF%20content/SOLID_cheatsheet.pdf
• https://web.archive.org/web/20150906155800/http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf
• https://devopedia.org/solid-design-principles
• Page Objects – Google
• https://code.google.com/p/selenium/wiki/PageObjects
• Page Objects – Martin Fowler
• http://martinfowler.com/bliki/PageObject.html
• Perils of Page-Object Pattern – Anand Bagmar
• http://essenceoftesting.blogspot.in/2014/09/perils-of-page-object-pattern.html
• Test Design Consideration
• http://docs.seleniumhq.org/docs/06_test_design_considerations.jsp
• Sample Test Framework implementation
• https://github.com/znsio/tesiz
@BagmarAnand
BagmarAnand
https://www.linkedin.com/in/anandbagmar/
Thank you

Design Patterns in Automation