refactoring page objects the screenplay pattern

48
The Screenplay Pattern A SOLID alternative Antony Marcan

Upload: riverglide

Post on 25-Jan-2017

63 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Refactoring page objects   The Screenplay Pattern

The Screenplay PatternA SOLID alternative

to Page ObjectsAntony Marcano

Page 2: Refactoring page objects   The Screenplay Pattern

@AntonyMarcanoriverglide.com

Contributions, quotations & references in:

2

Page 3: Refactoring page objects   The Screenplay Pattern

All code is…

SeriouslyHinderedInTesting?

Inspired by Simon Stewart’s opening keynote at Selenium Conf 2016…

Page 4: Refactoring page objects   The Screenplay Pattern

Knowledge of programming

Page 5: Refactoring page objects   The Screenplay Pattern

All code is…

SeriouslyHinderedInTranslation?

Inspired by Simon Stewart’s opening keynote at Selenium Conf 2016…

Page 6: Refactoring page objects   The Screenplay Pattern

Expressing intent

Page 7: Refactoring page objects   The Screenplay Pattern

All code is…

SeriouslyHinderedInTransformation?

Inspired by Simon Stewart’s opening keynote at Selenium Conf 2016…

Page 8: Refactoring page objects   The Screenplay Pattern

Ease of refactoring

Page 9: Refactoring page objects   The Screenplay Pattern

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

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

http://bit.ly/rg-todomvc

Page 12: Refactoring page objects   The Screenplay Pattern

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

Let’s go back in time…

Page 14: Refactoring page objects   The Screenplay Pattern

“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

http://bit.ly/rg-pageobject-example

Page 16: Refactoring page objects   The Screenplay Pattern
Page 17: Refactoring page objects   The Screenplay Pattern

What’s wrong with this?

Page 18: Refactoring page objects   The Screenplay Pattern

“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

Code Smell: “Large Class”

Page 20: Refactoring page objects   The Screenplay Pattern

What can help us refactorto a better design?

Page 21: Refactoring page objects   The Screenplay Pattern

SOLIDSingle Responsibility PrincipleOpen Closed PrincipleLiskov Substitution PrincipleInterface Segregation PrincipleDependency Inversion Principle

Page 22: Refactoring page objects   The Screenplay Pattern

Let’s focus on…Single Responsibility PrincipleOpen Closed PrincipleLiskov Substitution PrincipleInterface Segregation PrincipleDependency Inversion Principle

Page 23: Refactoring page objects   The Screenplay Pattern

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

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

http://bit.ly/rg-todomvc

Page 26: Refactoring page objects   The Screenplay Pattern

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

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

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

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

What are we modeling?

Page 31: Refactoring page objects   The Screenplay Pattern
Page 32: Refactoring page objects   The Screenplay Pattern

Modelling the problem

Not the solution

Page 33: Refactoring page objects   The Screenplay Pattern

The Screenplay Pattern

Page 34: Refactoring page objects   The Screenplay Pattern
Page 35: Refactoring page objects   The Screenplay Pattern

There are actors. Actors have abilities.

Page 36: Refactoring page objects   The Screenplay Pattern

Actors perform tasks…

Page 37: Refactoring page objects   The Screenplay Pattern

Tasks involve ‘actions’

Page 38: Refactoring page objects   The Screenplay Pattern

Things we interact with are just data

Page 39: Refactoring page objects   The Screenplay Pattern

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

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

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

The Screenplay Pattern

Page 43: Refactoring page objects   The Screenplay Pattern

The Screenplay Pattern

Page 44: Refactoring page objects   The Screenplay Pattern

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

In Summary

Page 46: Refactoring page objects   The Screenplay Pattern

• 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

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

Contact: [email protected] http://antonymarcano.com/blog @AntonyMarcano

Materials created with the time, love and attention of Antony Marcano and Andy Palmer