Refactoring page objects The Screenplay Pattern
-
Upload
riverglide -
Category
Technology
-
view
56 -
download
0
Transcript of Refactoring page objects The Screenplay Pattern
![Page 1: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/1.jpg)
The Screenplay PatternA SOLID alternative
to Page ObjectsAntony Marcano
![Page 2: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/2.jpg)
@AntonyMarcanoriverglide.com
Contributions, quotations & references in:
2
![Page 3: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/3.jpg)
All code is…
SeriouslyHinderedInTesting?
Inspired by Simon Stewart’s opening keynote at Selenium Conf 2016…
![Page 4: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/4.jpg)
Knowledge of programming
![Page 5: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/5.jpg)
All code is…
SeriouslyHinderedInTranslation?
Inspired by Simon Stewart’s opening keynote at Selenium Conf 2016…
![Page 6: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/6.jpg)
Expressing intent
![Page 7: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/7.jpg)
All code is…
SeriouslyHinderedInTransformation?
Inspired by Simon Stewart’s opening keynote at Selenium Conf 2016…
![Page 8: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/8.jpg)
Ease of refactoring
![Page 9: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/9.jpg)
Just enough to give confidencethat the product hangs togetherfor the key usage examples
DON’T try to prove that absolutely everything works together in absolutely every way that the whole product is supposed to
![Page 10: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/10.jpg)
The Screenplay pattern is as a way of separating any kind of interactions with your product, including RESTful APIs, Web or any form of interaction.
For the purposes of this talk, we’re going to focus on page objects…
![Page 11: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/11.jpg)
http://bit.ly/rg-todomvc
![Page 12: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/12.jpg)
Support code using Page Objects
Todo List Page
newTodotodoItemstodosRemainingtoggleAllButtonclearCompletedButtonfilterTodos…
addATodoItem()addTodoItems()toggleAllCompleted()filterItems()…
Responsibilities
How to find page elements
How a user completes given tasks
![Page 13: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/13.jpg)
Let’s go back in time…
![Page 14: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/14.jpg)
“Selenium is not unstable, and your tests don’t need to be flaky. […] When your tests are flaky, do some root cause analysis to understand why they’re flaky. It’s very seldom because you’ve uncovered a bug in the test framework.”-Simon Stewart, Google Testing Blog 2009, “My Selenium Tests Aren't Stable!”
![Page 15: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/15.jpg)
http://bit.ly/rg-pageobject-example
![Page 16: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/16.jpg)
![Page 17: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/17.jpg)
What’s wrong with this?
![Page 18: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/18.jpg)
“A code smell is a surface indication that usually corresponds to a deeper problem in the system. The term was first coined by Kent Beck while helping me with my Refactoring book.” -Martin Fowler
![Page 19: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/19.jpg)
Code Smell: “Large Class”
![Page 20: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/20.jpg)
What can help us refactorto a better design?
![Page 21: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/21.jpg)
SOLIDSingle Responsibility PrincipleOpen Closed PrincipleLiskov Substitution PrincipleInterface Segregation PrincipleDependency Inversion Principle
![Page 22: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/22.jpg)
Let’s focus on…Single Responsibility PrincipleOpen Closed PrincipleLiskov Substitution PrincipleInterface Segregation PrincipleDependency Inversion Principle
![Page 23: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/23.jpg)
Single Responsibility Principle
“If a class has more than one responsibility, then the responsibilities become coupled. Changes to one responsibility may impair or inhibit the class’ ability to meet the others. This kind of coupling leads to fragile designs that break in unexpected ways when changed.” -Robert Martin, Agile Principles, Patterns & Practices
![Page 24: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/24.jpg)
Open Closed PrincipleThe Open Closed Principle (coined by Bertrand Meyer in Object-Oriented Software Construction) states that a class should be open for extension, but closed for modification. This means that it should be possible to extend behaviour by writing a new class without changing existing, working code.
![Page 25: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/25.jpg)
http://bit.ly/rg-todomvc
![Page 26: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/26.jpg)
Todo List Page
newTodotodoItemstodosRemainingtoggleAllButtonclearCompletedButtonfilterTodos…
addATodoItem()addTodoItems()toggleAllCompleted()filterItems()…
Responsibilities
How to find page elements
How a user completes given tasks
![Page 27: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/27.jpg)
Todo List Page
newTodotodoItemstodosRemainingtoggleAllButtonclearCompletedButtonfilterTodos…
addATodoItem()addTodoItems()toggleAllCompleted()filterItems()…
TodoListPagenewTodotodoItemstodosRemainingtoggleAllButtonclearCompletedButtonfilterTodos…
TodoListTasksaddATodoItem()addTodoItems()toggleAllCompleted()filterItems()…
Extract Class
Extract Class
![Page 28: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/28.jpg)
Todo List Page
newTodotodoItemstodosRemainingtoggleAllButtonclearCompletedButtonfilterTodos…
addATodoItem()addTodoItems()toggleAllCompleted()filterItems()…
AddNewTodo
newTodo
addTodoItem ()addTodoItems()
MaintainTodos
toggleAllButtonclearCompletedButton
toggleAllCompleted()clearAllCompletedTodos()
Extract Class
Extract Class
![Page 29: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/29.jpg)
Todo List Page
newTodotodoItemstodosRemainingtoggleAllButtonclearCompletedButtonfilterTodos…
addATodoItem()addTodoItems()toggleAllCompleted()filterItems()…
TodoListPagenewTodotodoItemstodosRemainingtoggleAllButtonclearCompletedButtonfilterTodos…
AddATodoItem
perform()
Extract Class
Replace Method with Method Object
AddTodoItems
perform()
ToggleAllCompleted
perform()
![Page 30: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/30.jpg)
What are we modeling?
![Page 31: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/31.jpg)
![Page 32: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/32.jpg)
Modelling the problem
Not the solution
![Page 33: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/33.jpg)
The Screenplay Pattern
![Page 34: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/34.jpg)
![Page 35: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/35.jpg)
There are actors. Actors have abilities.
![Page 36: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/36.jpg)
Actors perform tasks…
![Page 37: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/37.jpg)
Tasks involve ‘actions’
![Page 38: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/38.jpg)
Things we interact with are just data
![Page 39: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/39.jpg)
Instead of a few large classes,you have more smaller classes.When you identify a new task an actor must perform, just add a new task class.
![Page 40: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/40.jpg)
You don’t have to use cucumber for this either…
Example using Screenplay implementation in Serenity-BDD
![Page 41: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/41.jpg)
Your scenario is a narrative for a cast of actors, each playing a different role. Each actor has the task of performing action to the best of their ability…
![Page 42: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/42.jpg)
The Screenplay Pattern
![Page 43: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/43.jpg)
The Screenplay Pattern
![Page 44: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/44.jpg)
Before you beginDon’t aim to refactor all of your support code.Try an experiment alongside what you have.
See how it feels and if you like it, look at how to use the approach with new scenarios.
![Page 45: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/45.jpg)
In Summary
![Page 46: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/46.jpg)
• As any support code grows, ask first “are we testing too much here?”
• Big classes with interaction methods (like PageObjects) are useful as training wheels
• As we try to go faster, training wheels make us less stable
• The Screenplay Pattern allows us to ride more safely, at speed, from the start
![Page 47: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/47.jpg)
LinksFull article: bit.ly/rg-screenplayExamples on Github: bit.ly/rg-serenity-eg
using Serenity
AcknowledgementsAndy Palmer
John Ferguson Smart & Jan Molak
![Page 48: Refactoring page objects The Screenplay Pattern](https://reader033.fdocuments.in/reader033/viewer/2022042611/588794c81a28ab5b1a8b5ab5/html5/thumbnails/48.jpg)
Contact: [email protected] http://antonymarcano.com/blog @AntonyMarcano
Materials created with the time, love and attention of Antony Marcano and Andy Palmer