SlideShare a Scribd company logo
Selenium
Design Patterns
About me
 Test automation expert
 Extensive experience in BDD, TDD, SpecFlow and Selenium.
 QA and Automation leader at BurlingtonEnglish,
 Blogger at SeleniumPro.net
Clean Code
“Any fool can write code that a computer
can understand. Good programmers write
code that humans can understand.”
- Martin Fowler
Clean Code
“Good code isn't just readable by a
machine, but by humans as well”
Martin Fowler
What is Clean Code
 A reader-focused development style that produces
software that’s easy to write, read, and maintain
 Clean code is code that is easy to understand and easy to
change
Why Clean Code Matters
 Each line of code is likely to be read many times by
humans during its lifetime
 Maintenance: Writing code is relatively easy, reading is
hard, by creating code that is maintainable, you are
optimizing the majority of your time and the cost of
maintaining code
Simplicity:
By keeping your code simple, you can produce higher
quality code, solve problems faster ,and work better in
groups
When your code is indented, it becomes more readable
and easier to find what you’re looking for
Cost Savings:
By doing clean code, you gain all those advantages listed
above, and all of them lead to cost savings.
Recommended Books:
Code Smells
 Code Smell is a term coined by Kent Beck and introduced in
Martin Fowler's book, Refactoring.
 Code Smells are patterns of code that suggest there might be a
problem
 There might be a better way of writing the code or that more
design perhaps should go into it
 Anti Patterns describes the same kinds of phenomena
Examples for Code Smells
 Alternative Classes with Different Interfaces (should probably be refactored to
have similar interfaces)
 Comments (code should be written so that the comments are not necessary)
 Duplicate Code - If you see the same code structure in more than one place, you
can be sure that your program will be better if you find a way to unify them
 See Don't Repeat Yourself
 Large Class - Single responsibility principle - every module or class should have
responsibility over a single part of the functionality provided by the software
 Long Method - The longer a function is, the more difficult it is to understand.
 Long Parameter List - More than three arguments to a function is generally an
issue
Common bad patterns of Selenium
code
 Writing Selenium code inside tests
 Sleep
 Not making tests independent of each other
 Name your tests wisely - Naming convention
 Reuse components. Don’t Copy/paste
 Testing more that 1 thing in a single test: Single purpose tests are easier
to create and maintain
 Not following AAA pattern (Arrange => Act => Assert)
 Not using PageObjects / ScreenPlay patterns
More bad practices related to
Selenium tests
 PageObject with public WebElements
 Static driver (singleton)
 Unstable locators
 Long PageObject class (Code Smell
)
Page Objects
 Page Object is a design pattern for building robust,
reusable and maintainable automated GUI tests, Page
Object is very popular, especially with Selenium tests
 This pattern helps you to organize your code, makes your
code much more readable, reusable and maintainable
Before
After
More info
Why Page Objects Pattern
 Avoid code duplication
 Improve readability
 Organize code for web pages interaction
 Saves a lot of time on tests maintenance and
makes the UI automation pipeline faster =>
reduces costs
Page Objects can become anti-pattern
called “Large Class”
 This happens when you have too many elements on some of the pages
 so the number of functions on an object that represents the page might
become really huge
 This goes against one of the most important design principles in object-
oriented programming called SOLID
 Single Responsibility Principle
 Open Closed Principle
 Liskov Substitution Principle
 Interface Substitution Principle
 Dependency Inversion Principle
Single Responsibility Principle
 Each class should have a single functionality
 A class should have only one reason to change. -
Robert C. Martin
Solution: Design OOP Page Objects
Example 1:
Email Inbox
Instead of:
Selenium Design Patterns
Example 2: Todos Application
Page Object
classes for
Todos:
Task
Row:
And the usage:
Usage of Todos page:
Project:
https://github.com/LirazShay/Pa
geObjectsDemo
The Screenplay Pattern
 Screenplay is the design pattern for acceptance tests
 Allows you to easily follow SOLID principles
Selenium Design Patterns
And the classes:
For more information:
https://dzone.com/articles/page-objects-
refactored-solid-steps-to-the-screenp
Conclusions:
 By modeling the page components into objects we will prevent
ourselves from large classes,
 It seems that by using ScreenPlay pattern there will be tons of
classes
 ScreenPlay is preferred over large page objects
 Small PageObject classes are much better than ScreenPlay
design
Flaky tests
 We define a "flaky" test result as a test that exhibits both a passing and a
failing result with the same code
 Non-deterministic tests have two problems:
 Firstly - they are useless
 Secondly - they are a virulent infection that can completely ruin your
entire test suite.
 As a result they need to be dealt with as soon as you can, before your
entire deployment pipeline is compromised.
Reasons for flaky tests and
How to prevent them
 Tests’ Initial State Should Always be Consistent – See Oren Rubin great
article here
 Avoid non deterministic input – A test should run exactly the same with
given parameters.
 Test should be deterministic – with the same conditions, avoid ‘if’ in the
tests
 Test with many ‘if’ conditions or other non deterministic conditions - will
be very hard to know the exact workflow that happened when the test was
failed
More reasons for flaky tests in
Selenium tests:
 Not following Page Object pattern principle of verifying in the
constructor of a PageObject class that the page was loaded, this or
other similar issues can cause that sometime the operation
happened to early so it failed.
 Use Assert methods with waiting logic, because sometimes the
same assertion will pass after waiting a little bit
 Flaky third party code
Tips for parallel tests architecture
 Use different data as possible (users, etc.)
 Each test should use his own objects
 Try to minimum the usage of static resources
 If need static / singleton – write thread safe code, with thread
safe collections, lock if needed, etc.
The End
Questions?

More Related Content

What's hot (20)

ODP
Introduction to BDD
Knoldus Inc.
 
PPSX
JIRA
ANKUR-BA
 
PPTX
Test case management using jira
QMetry
 
PPSX
Selenium WebDriver
Rajathi-QA
 
PPTX
Selenium WebDriver training
Vijay Krishnan Ramaswamy
 
PDF
Automation testing introduction for FujiNet
Hai Tran Son
 
PPTX
A Top Down Approach to End-to-End Testing
SmartBear
 
PPT
Selenium Concepts
Swati Bansal
 
PPTX
Automation Framework Presentation
Ben Ngo
 
PPTX
Introduction to Automation Testing
Archana Krushnan
 
PDF
JUnit 5
Scott Leberknight
 
PDF
What is JUnit? | Edureka
Edureka!
 
PDF
Katalon Studio - Successful Test Automation for both Testers and Developers
Katalon Studio
 
PDF
Domain-Driven Design with ASP.NET MVC
Steven Smith
 
PPTX
Test automation proposal
Mihai-Cristian Fratila
 
PDF
Angular 4 Tutorial | What's New In Angular 4 | Angular Training | Edureka
Edureka!
 
PDF
Functional Tests Automation with Robot Framework
laurent bristiel
 
PPTX
PATTERNS03 - Behavioural Design Patterns
Michael Heron
 
PPTX
SoapUI ile Rest Api Testi
Tuncer Ergin
 
PPT
Hybrid Automation Framework Development introduction
Ganuka Yashantha
 
Introduction to BDD
Knoldus Inc.
 
JIRA
ANKUR-BA
 
Test case management using jira
QMetry
 
Selenium WebDriver
Rajathi-QA
 
Selenium WebDriver training
Vijay Krishnan Ramaswamy
 
Automation testing introduction for FujiNet
Hai Tran Son
 
A Top Down Approach to End-to-End Testing
SmartBear
 
Selenium Concepts
Swati Bansal
 
Automation Framework Presentation
Ben Ngo
 
Introduction to Automation Testing
Archana Krushnan
 
What is JUnit? | Edureka
Edureka!
 
Katalon Studio - Successful Test Automation for both Testers and Developers
Katalon Studio
 
Domain-Driven Design with ASP.NET MVC
Steven Smith
 
Test automation proposal
Mihai-Cristian Fratila
 
Angular 4 Tutorial | What's New In Angular 4 | Angular Training | Edureka
Edureka!
 
Functional Tests Automation with Robot Framework
laurent bristiel
 
PATTERNS03 - Behavioural Design Patterns
Michael Heron
 
SoapUI ile Rest Api Testi
Tuncer Ergin
 
Hybrid Automation Framework Development introduction
Ganuka Yashantha
 

Similar to Selenium Design Patterns (20)

PDF
How To Tidy Up Your Test Code
Rock Interview
 
PDF
Testing and TDD - KoJUG
lburdz
 
PPT
Unit Testing, TDD and the Walking Skeleton
Seb Rose
 
PPTX
Implementing TDD in for .net Core applications
Ahmad Kazemi
 
PDF
Building Maintainable Android Apps (DroidCon NYC 2014)
Kevin Schultz
 
PPT
Introduction to test programming
openfinanceDev
 
PPTX
Unit Testing Full@
Alex Borsuk
 
DOCX
Software Testing Tools Training
QEdge Tech
 
PPS
Unit Testing
Anuj Arora
 
PDF
Interview Question & Answers for Selenium Freshers | LearningSlot
Learning Slot
 
PPTX
Selenium online training in India
united global soft
 
PPTX
Selenium online training in india
united global soft
 
PPTX
Selenium online training
Divya Shree
 
KEY
Products are a mess
Suman Mukherjee
 
ODP
The Professional Programmer
Dave Cross
 
PDF
7 Effective Practices to Follow for Selenium Automation Testing
TestingXperts
 
PPS
Why Unit Testingl
priya_trivedi
 
PPS
Why Unit Testingl
priya_trivedi
 
PPS
Why unit testingl
Priya Sharma
 
PPTX
Selenium Online Training in India
united global soft
 
How To Tidy Up Your Test Code
Rock Interview
 
Testing and TDD - KoJUG
lburdz
 
Unit Testing, TDD and the Walking Skeleton
Seb Rose
 
Implementing TDD in for .net Core applications
Ahmad Kazemi
 
Building Maintainable Android Apps (DroidCon NYC 2014)
Kevin Schultz
 
Introduction to test programming
openfinanceDev
 
Unit Testing Full@
Alex Borsuk
 
Software Testing Tools Training
QEdge Tech
 
Unit Testing
Anuj Arora
 
Interview Question & Answers for Selenium Freshers | LearningSlot
Learning Slot
 
Selenium online training in India
united global soft
 
Selenium online training in india
united global soft
 
Selenium online training
Divya Shree
 
Products are a mess
Suman Mukherjee
 
The Professional Programmer
Dave Cross
 
7 Effective Practices to Follow for Selenium Automation Testing
TestingXperts
 
Why Unit Testingl
priya_trivedi
 
Why Unit Testingl
priya_trivedi
 
Why unit testingl
Priya Sharma
 
Selenium Online Training in India
united global soft
 
Ad

Recently uploaded (20)

PPTX
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
PDF
GridView,Recycler view, API, SQLITE& NetworkRequest.pdf
Nabin Dhakal
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PPTX
Platform for Enterprise Solution - Java EE5
abhishekoza1981
 
PPTX
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PDF
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PDF
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
PDF
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
PPTX
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PPTX
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
Letasoft Sound Booster 1.12.0.538 Crack Download+ Product Key [Latest]
HyperPc soft
 
PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
PDF
Continouous failure - Why do we make our lives hard?
Papp KrisztiĂĄn
 
PPTX
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
GridView,Recycler view, API, SQLITE& NetworkRequest.pdf
Nabin Dhakal
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
Platform for Enterprise Solution - Java EE5
abhishekoza1981
 
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
Human Resources Information System (HRIS)
Amity University, Patna
 
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Letasoft Sound Booster 1.12.0.538 Crack Download+ Product Key [Latest]
HyperPc soft
 
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
Continouous failure - Why do we make our lives hard?
Papp KrisztiĂĄn
 
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
Ad

Selenium Design Patterns

  • 2. About me  Test automation expert  Extensive experience in BDD, TDD, SpecFlow and Selenium.  QA and Automation leader at BurlingtonEnglish,  Blogger at SeleniumPro.net
  • 3. Clean Code “Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” - Martin Fowler
  • 4. Clean Code “Good code isn't just readable by a machine, but by humans as well” Martin Fowler
  • 5. What is Clean Code  A reader-focused development style that produces software that’s easy to write, read, and maintain  Clean code is code that is easy to understand and easy to change
  • 6. Why Clean Code Matters  Each line of code is likely to be read many times by humans during its lifetime  Maintenance: Writing code is relatively easy, reading is hard, by creating code that is maintainable, you are optimizing the majority of your time and the cost of maintaining code
  • 7. Simplicity: By keeping your code simple, you can produce higher quality code, solve problems faster ,and work better in groups When your code is indented, it becomes more readable and easier to find what you’re looking for Cost Savings: By doing clean code, you gain all those advantages listed above, and all of them lead to cost savings.
  • 9. Code Smells  Code Smell is a term coined by Kent Beck and introduced in Martin Fowler's book, Refactoring.  Code Smells are patterns of code that suggest there might be a problem  There might be a better way of writing the code or that more design perhaps should go into it  Anti Patterns describes the same kinds of phenomena
  • 10. Examples for Code Smells  Alternative Classes with Different Interfaces (should probably be refactored to have similar interfaces)  Comments (code should be written so that the comments are not necessary)  Duplicate Code - If you see the same code structure in more than one place, you can be sure that your program will be better if you find a way to unify them  See Don't Repeat Yourself  Large Class - Single responsibility principle - every module or class should have responsibility over a single part of the functionality provided by the software  Long Method - The longer a function is, the more difficult it is to understand.  Long Parameter List - More than three arguments to a function is generally an issue
  • 11. Common bad patterns of Selenium code  Writing Selenium code inside tests  Sleep  Not making tests independent of each other  Name your tests wisely - Naming convention  Reuse components. Don’t Copy/paste  Testing more that 1 thing in a single test: Single purpose tests are easier to create and maintain  Not following AAA pattern (Arrange => Act => Assert)  Not using PageObjects / ScreenPlay patterns
  • 12. More bad practices related to Selenium tests  PageObject with public WebElements  Static driver (singleton)  Unstable locators  Long PageObject class (Code Smell
)
  • 13. Page Objects  Page Object is a design pattern for building robust, reusable and maintainable automated GUI tests, Page Object is very popular, especially with Selenium tests  This pattern helps you to organize your code, makes your code much more readable, reusable and maintainable
  • 15. Why Page Objects Pattern  Avoid code duplication  Improve readability  Organize code for web pages interaction  Saves a lot of time on tests maintenance and makes the UI automation pipeline faster => reduces costs
  • 16. Page Objects can become anti-pattern called “Large Class”  This happens when you have too many elements on some of the pages  so the number of functions on an object that represents the page might become really huge  This goes against one of the most important design principles in object- oriented programming called SOLID  Single Responsibility Principle  Open Closed Principle  Liskov Substitution Principle  Interface Substitution Principle  Dependency Inversion Principle
  • 17. Single Responsibility Principle  Each class should have a single functionality  A class should have only one reason to change. - Robert C. Martin
  • 18. Solution: Design OOP Page Objects Example 1: Email Inbox Instead of:
  • 20. Example 2: Todos Application
  • 24. Usage of Todos page:
  • 26. The Screenplay Pattern  Screenplay is the design pattern for acceptance tests  Allows you to easily follow SOLID principles
  • 28. And the classes: For more information: https://dzone.com/articles/page-objects- refactored-solid-steps-to-the-screenp
  • 29. Conclusions:  By modeling the page components into objects we will prevent ourselves from large classes,  It seems that by using ScreenPlay pattern there will be tons of classes  ScreenPlay is preferred over large page objects  Small PageObject classes are much better than ScreenPlay design
  • 30. Flaky tests  We define a "flaky" test result as a test that exhibits both a passing and a failing result with the same code  Non-deterministic tests have two problems:  Firstly - they are useless  Secondly - they are a virulent infection that can completely ruin your entire test suite.  As a result they need to be dealt with as soon as you can, before your entire deployment pipeline is compromised.
  • 31. Reasons for flaky tests and How to prevent them  Tests’ Initial State Should Always be Consistent – See Oren Rubin great article here  Avoid non deterministic input – A test should run exactly the same with given parameters.  Test should be deterministic – with the same conditions, avoid ‘if’ in the tests  Test with many ‘if’ conditions or other non deterministic conditions - will be very hard to know the exact workflow that happened when the test was failed
  • 32. More reasons for flaky tests in Selenium tests:  Not following Page Object pattern principle of verifying in the constructor of a PageObject class that the page was loaded, this or other similar issues can cause that sometime the operation happened to early so it failed.  Use Assert methods with waiting logic, because sometimes the same assertion will pass after waiting a little bit  Flaky third party code
  • 33. Tips for parallel tests architecture  Use different data as possible (users, etc.)  Each test should use his own objects  Try to minimum the usage of static resources  If need static / singleton – write thread safe code, with thread safe collections, lock if needed, etc.