Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Refactoring page objects The Screenplay Pattern

2,800 views

Published on

As seen at BDD Exchange 2016 and Selenium Conf 2016
The Screenplay Pattern, first created by Antony Marcano, is an alternative model to PageObjects. Today, it is growing in popularity with increasing tool support in popular testing frameworks.

PageObjects provide an easy-to-follow, simple structure that avoids early maintenance issues. They were introduced to help test-developers avoid mistaking flaky tests for problems with Selenium. But, PageObjects break some key OO design rules, making maintenance more difficult over time. They are a useful first step, but why do we stop there?

In this session you’ll learn about the SOLID design principles that PageObjects disregard. You’ll see why this leads to problems. You’ll see how and why PageObjects benefit from refactoring to SOLID design principles. Finally, you’ll meet the Screenplay Pattern – an alternative model based on SOLID principles that saves you the trouble.

Published in: Technology

Refactoring page objects The Screenplay Pattern

  1. 1. The Screenplay Pattern A SOLID alternative
  2. 2. @AntonyMarcano riverglide.com Contributions, quotations & references in: 2
  3. 3. All code is… Seriously Hindered In Testing? Inspired by Simon Stewart’s opening keynote at Selenium Conf 2016…
  4. 4. Knowledge of programming
  5. 5. All code is… Seriously Hindered In Translation? Inspired by Simon Stewart’s opening keynote at Selenium Conf 2016…
  6. 6. Expressing intent
  7. 7. All code is… Seriously Hindered In Transformation? Inspired by Simon Stewart’s opening keynote at Selenium Conf 2016…
  8. 8. Ease of refactoring
  9. 9. Just enough to give confidence that the product hangs together for 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
  10. 10. 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…
  11. 11. http://bit.ly/rg-todomvc
  12. 12. Support code using Page Objects Todo List Page newTodo todoItems todosRemaining toggleAllButton clearCompletedButton filterTodos … addATodoItem() addTodoItems() toggleAllCompleted() filterItems() … Responsibilities How to find page elements How a user completes given tasks
  13. 13. Let’s go back in time…
  14. 14. “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!”
  15. 15. http://bit.ly/rg-pageobject-example
  16. 16. What’s wrong with this?
  17. 17. “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
  18. 18. Code Smell: “Large Class”
  19. 19. What can help us refactor to a better design?
  20. 20. SOLID Single Responsibility Principle Open Closed Principle Liskov Substitution Principle Interface Segregation Principle Dependency Inversion Principle
  21. 21. Let’s focus on… Single Responsibility Principle Open Closed Principle
  22. 22. 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
  23. 23. Open Closed Principle The 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.
  24. 24. http://bit.ly/rg-todomvc
  25. 25. Todo List Page newTodo todoItems todosRemaining toggleAllButton clearCompletedButton filterTodos … addATodoItem() addTodoItems() toggleAllCompleted() filterItems() … Responsibilities How to find page elements How a user completes given tasks
  26. 26. Todo List Page newTodo todoItems todosRemaining toggleAllButton clearCompletedButton filterTodos … addATodoItem() addTodoItems() toggleAllCompleted() filterItems() … TodoListPage newTodo todoItems todosRemaining toggleAllButton clearCompletedButton filterTodos … TodoListTasks addATodoItem() addTodoItems() toggleAllCompleted() filterItems() … Extract Class Extract Class
  27. 27. Todo List Page newTodo todoItems todosRemaining toggleAllButton clearCompletedButton filterTodos … addATodoItem() addTodoItems() toggleAllCompleted() filterItems() … AddNewTodo newTodo addTodoItem () addTodoItems() MaintainTodos toggleAllButton clearCompletedButton toggleAllCompleted() clearAllCompletedTodos() Extract Class Extract Class
  28. 28. Todo List Page newTodo todoItems todosRemaining toggleAllButton clearCompletedButton filterTodos … addATodoItem() addTodoItems() toggleAllCompleted() filterItems() … TodoListPage newTodo todoItems todosRemaining toggleAllButton clearCompletedButton filterTodos … AddATodoItem perform() Extract Class Replace Method with Method Object AddTodoItems perform() ToggleAllCompleted perform()
  29. 29. What are we modeling?
  30. 30. Modelling the problem Not the solution
  31. 31. The Screenplay Pattern
  32. 32. There are actors. Actors have abilities.
  33. 33. Actors perform tasks…
  34. 34. Tasks involve ‘actions’
  35. 35. Things we interact with are just data
  36. 36. 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.
  37. 37. You don’t have to use cucumber for this either… Example using Screenplay implementation in Serenity-BDD
  38. 38. 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…
  39. 39. The Screenplay Pattern
  40. 40. The Screenplay Pattern
  41. 41. Before you begin Don’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.
  42. 42. In Summary
  43. 43. • 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
  44. 44. Links Full article: bit.ly/rg-screenplay Examples on Github: bit.ly/rg-serenity-eg using Serenity Acknowledgements Andy Palmer John Ferguson Smart & Jan Molak
  45. 45. Contact: antony@riverglide.com http://antonymarcano.com/blog @AntonyMarcano Materials created with the time, love and attention of Antony Marcano and Andy Palmer

×