playwright+pytest+BDD+pom write automation use cases

Posted by janggu on Mon, 29 Nov 2021 19:55:06 +0100

BDD behavior driven development describes what features I need and what our verification scenarios are from the user's perspective, so as to ensure the unity of documents and code, design and testing, and develop test cases that meet the requirements

BDD is divided into fixture, scenario, given, when, and then

fixture (feature): it is a function provided to end users or other stakeholders to support the functions they need to achieve their business objectives.

Scenario: when a user accesses an application, there are many conditions to be met between the user and the application. Each different situation corresponds to a different scenario. A short declarative sentence summarizes the particularity of this example

  given: what are the prerequisites before the test operation? All scenarios should be defined, but not the preconditions of the required scope. given mainly serves the following test subjects

when (user operation): what operation did the user do and what data was entered

then (expected result): in response to the expected result of line operation, what changes will be triggered by the system and what data will be output after the user does the operation

 

 

So how do we combine playwright with pytest BDD to write automated use cases

1. Download Plug-in pytest BDD

pip insall  pytest-bdd

2. Create. Feature files with only one feature description in each feature file

@hotlink @shop
  Feature: As a new user,
    I want to be order a t-shirt,
    In order to buy a t-shirt, I must register my account
#background the same front part of each scene
    Background:
      Given Sean Open the home page of the website

    Scenario: After I log in to the shopping website, I place an order according to the shopping process
      Given Sean Sign in xx Shopping website
      And Sean Open T-shirt classification
      When Sean I chose one T-shirt add to cart
      And Sean Completed the order
      When Sean View profile order page
      Then Sean The order page has T-shirt order

3. Convert the given (precondition) when (expected result) in the feature file into the page element operation encapsulated in POM (POM is encapsulated as follows https://www.cnblogs.com/mumu-lin/p/15585133.html )

You can also convert element operations that are not encapsulated

import allure
from pytest_bdd import (
    given,
    scenario,
    then,
    when
)
# Need from pytest_ Import given, when, then, etc. into BDD
from page_objects.registation.registration_object import RegistrationPage from page_objects.shop.shop_object import ShopPage from utils.bdd_helper import BddHelper feature_path = BddHelper.get_feature_path("shop/shop_order_t_shirt.feature")
@scenario(feature_path, 'After I log in to the shopping website, I place an order according to the shopping process')
def test_shop_order_t_shirt():
"""I place orders according to the shopping process, New user registration and other operations."""
pass
#Functions decorated with scenario decorator behave like normal test functions, which will be executed after all scenario steps. However, it is better to include logic only in given and when then

@when('Sean Completed the order')
def finish_order_after_registration(page):
    """Sean Completed the order."""
    ShopPage(page).finish_order_after_registration()


@given('Sean Open T-shirt classification')
def open_t_shirt_category(page):
    """Sean Open T-shirt classification."""
    ShopPage(page).open_t_shirt_category()


@given('Sean Open the home page of the website')
def open_site(page):
    """Sean Open the home page of the website."""
    ShopPage(page).open_site()


@when('Sean View profile order page')
def open_profile_order_page(page):
    """Sean View profile order page."""
    ShopPage(page).open_profile_order_page()

4. Execute test cases

The function using the @ scenario decorator is actually a Test case, so we only need to run the Test case containing @ scenario. Note: the rules for retrieving Test cases in pytest are file name, Test case and class name, which should start with Test by default.

If you do not want to register according to each feature file, you can register according to the directory. Then use pytest to execute the current file to run the corresponding features

#Method 1: get the current features All in the directory feature
scenarios(feature_path)
#Method 2: get a directory or a features,These paths can be feature files or feature folders.
scenarios(feature_path, 'other_features/some.feature', 'some_other_features')
#Method 3: only one scene is bound
@scenario(feature_path, 'After entering the home page, View number of attachments')
def test_shop_attachment_count():
    pass
if __name__ == '__main__':
pytest.main(['-v', 'D:\\jiaoben\\ui-test-regression-main\\tests\step_definition\\shop\\test_bdd_shop.py'])

 

Personally, it is necessary to explore and adjust how to package and store the given (precondition) when (expected result) of the BDD use case, which is more convenient to maintain, so as to avoid encapsulating multiple identical step s in different folders

reference material:

https://pypi.org/project/pytest-bdd/

 

Topics: playwright