Test Automation Screenplay pattern: A different approach for Test Automation.

Test Automation Screenplay pattern: A different approach for Test Automation.

Before talking about the Screenplay pattern, I would like to start defining a test automation patter for those not familiar with this concept, "a test automation pattern is a way of solving an issue or problems in test automation that has worked in practice for many people." Just keep in mind that patter is not a step by step procedure; in other words, a patter can adapt and implement it in our context, helping to solve some of our issues.

A test automation pattern is an expert knowledge proven by repeated experience.

Let's explore the current situation in Test Automation; if you are working in a Test Automation project or building a test automation framework from scratch, your first option is Page Object pattern. Still, the Page Object pattern is not always the best option for your current project.

The main idea about this article is to know better about the Screenplay pattern and how we can adapt to our current projects. We need to decide which model fits into our current necessities and solving our problems.

Get familiar with the Screenplay pattern.

The Screenplay Pattern is about writing high-quality automated tests based on software engineering principles like the Single Responsibility Principle and the Open-Closed Principle. It prefers composition over inheritance and employs reasoning from Domain Driven Design to reflect the domain of performing tests, steering you towards the effective use of layers of abstraction.

Screenplay promotes good testing habits and well-organized test suites that are easy to follow, easy to maintain, and easy to extend, enabling teams to write more robust and more reliable automated tests. All good patterns must have all those good testing habits, but in reality, we should keep an eye on implementing correctly.

"The Screenplay pattern has been around since 2007; it is brilliantly described by Antony Marcano, Andy Palmer, Jan Molak, and John Ferguson Smart in their article Page Objects Refactored: SOLID Steps to the Screenplay/Journey Pattern. They have an excellent explanation of this pattern's concept and what common problems it intends to address. The main issue is that the commonly used Page Object pattern can introduce a high maintenance cost if the test automation solution grows."

In the Screenplay pattern, tests describe how a user interacts with the application to achieve a goal.

A user interacting with the system is called an "Actor." Actors are the central part of the Screenplay pattern. As a Screenplay, the actors have one or more "Abilities," such as the ability to browse the web or to query a web service, the actors can also perform "Tasks," such as adding an item or removing items.

Actor centric model describe below.

To interact with the application, such as entering values into the fields or clicking on buttons, the actors need to interact with the applications; these interactions are "Actions." Actors ask "Questions" about the state of the system, e.g., reading the value of a field on the screen or querying a web service, this is the way to test using the Screenplay pattern.

Screenplay pattern Implementation

For this example of the Screenplay pattern, we will use Python and one specific module called ScreenPy, ScreenPy is a library that provides the base for an automated test suite using Screenplay Pattern.

First, we need to install ScreenPy:

pip install screenpy

To quickly set up a Screenplay Pattern test suite using ScreenPy, as we mentioned above, we need to run the following command into the folder for our test suite:

python -m screenpy-quickstart.py
No alt text provided for this image

this command will create a File hierarchy for us:

\test_suite_folder
  \features #this is where the actual test files will live
    + feature1.py
    + ...
    
  
    \user_interface # files containing locators and/or URLs for each page
    +page1.py
    + ...

You can have other folders, e.g., actions, tasks, or abilities, depending on whether your Actors need additional capabilities.

As mentioned above, Screenplay Pattern focuses entirely on what your final users want to do on your site; in that case, your test cases will focus on what the actors do, such as gaining abilities, performing actions, and asking questions.

from screenpy.actor import Actor, AnActor
from selenium.webdriver import Firefox
from screenpy.abilities import BrowseTheWeb


Anton = AnActor.named("Anton").who_can(BrowseTheWeb.using(Firefox()))


As you can see above, Anton, our actor is ready to provide exceptional performance with his new ability to BrowseTheWeb. Now we need to perform some actions with his unique ability.

from screenpy import Target
from screenpy.actions import Click

#the string to use as a locator for the element can be a CSS selector or an xpath string
GOOGLE_LINK = Target.the("https://www.google.com").located_by("css select")
Anton.attempts_to(Click.the(GOOGLE_LINK))

Anton is ready to provide a superb performance (In this case a test), remember tests are performed with Questions:

from screenpy.questions import Text
from screenpy.resolutions import ReadsExactly

GOOGLE_MESSAGE = Target.the("google_message").located_by("div:nth-of-type(1) > .rc .st")
Anton.should_see_the((Text.of(GOOGLE_MESSAGE), ReadsExactly("Test"))

You can find a test example with Test structure:

/feature/test_iframe.py

import unittest


from screenpy import AnActor, given, then, when
from screenpy.abilities import BrowseTheWeb
from screenpy.actions import Open, SwitchTo
from screenpy.pacing import act, scene
from screenpy.questions import Text
from screenpy.resolutions import ReadsExactly
from tasks import start


from user_interface.iframe import CONTENT, URLI, IFRAME

class TestFrames(unittest.TestCase):


    def setUp(self):

        self.actor = AnActor.named("Anton").who_can (BrowseTheWeb.using_firefox())

    @act("Perform")
    @scene("SwitchTo")
    def test_switch_to_iframe(self):
    
      Anton = self.actor

      given(Anton).attempts_to(Open.their_browser_on(URL))
      when(Anton).attempts_to(SwitchTo.the(IFRAME))
      then(Anton).should_see_the(
          (Text.of_the(CONTENT_BOX), ReadsExactly("Your content goes here."))
      )

    def tearDown(self):
        self.actor.exit()
                                                

/user_interface/iframe.py

# Locators and URL for the Iframe page.

from screenpy import Target

URLI = "https://the-internet.herokuapp.com/iframe"

IFRAME = Target.the("Iframe").located_by("#mce_0_ifr")
CONTENT = Target.the("The content box").located_by("p")

As python philosophy, "batteries are included" after installing Screenpy following dependencies will be installed as well: Selenium, Allure's pytest plugin, pytest, and PyOTP.

If you want to see Allure, you need to add "--alluredir allure_report" and then generate Allure reports "allure serve allure_report" and voila, no configuration required (Allure must be available locally https://tinyurl.com/y2rv2ebp).

Allure report displayed.

You can find specific configuration examples in the following Github repository link; the next step will be to add TestProject configuration and add the following BrowseTheWeb.using_TestProject(Chrome).

Final Thoughts

If you are looking for some alternatives, the Screenplay pattern can improve your test automation solution. Every project is different, and sometimes we need to create maintainable solutions. I encourage you to use it if it fits in your specific situation. The screenplay can be a great substitution of the PageObject model pattern; otherwise, it can use along with others that complement it.

The Screenplay pattern could be hard initially; still, the benefits of having a test suite that scales with reusable components could be a great addition to our knowledge base, besides this test automation patter can be easy to maintain and helpful. If you are using this model with Javascript (Besides Serenity), please share your findings.

Remember, SOLID principles can help to avoid your code smell in your test automation solution.

Happy Bug Hunting!

References

Selenium Design Patterns and Best Practices, Dima Kovalenko

https://www.infoq.com/articles/Beyond-Page-Objects-Test-Automation-Serenity-Screenplay/

https://screenpy-docs.readthedocs.io/?????????????????????????????????????

https://moduscreate.com/blog/the-screenplay-test-design-pattern/



Perry Goy

Experienced Programmer, Quality Assurance Engineer, and Friend.

1 年

Hi! I'm the creator and lead maintainer of ScreenPy, and i only just found out about this article. I'm humbled and excited! We had a major update with ScreenPy that broke it apart into a core + extensions sort of model, which made us retire `screenpy-quickstart` since `screenpy-selenium` (the Selenium extension for ScreenPy) was no longer included by default. I wanted to let you know that your article actually inspired us to un-retire it and make it even cooler—we didn't realize it was so useful to people! Thanks for writing this article!

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了