This is the ninth day of my participation in the First Challenge 2022

In Selenium how to implement keyword drivers, I have written a very simple keyword driver, but this program only runs the function, there are a lot of areas that can be optimized, this article I want to use PyTest to simplify the writing of automated test cases, using the relatively basic PyTest function. In the next article, I’ll write a more sophisticated version that executes yamL files directly from the bottom as a use case.

Use cases before optimization

Before optimization, if you wanted to add a use case, you would first need to write a YAML file and then a Python automated test case. The code for the use case would look like this:

def test_keyword(driver) :
    """ Get yamL file """
    with open('signin.yaml', encoding='utf-8') as f:
        steps = yaml.safe_load(f)
        page = Page(driver)

    for step in steps:
        action_name = step.get('action')
        params = step.get('params')
        action = getattr(page, action_name)
        action(**params)
Copy the code

Although this program is relatively simple to use, if you want to create another use case, just copy the function once, change the signin.yaml file name, the other code does not need to move, but not to the degree that it is not a no-brainer, copying so many code at a time is not good.

Optimized use cases

import pytest

@pytest.mark.yaml_case('signin.yaml')
def test_keyword() :
	pass
Copy the code

The optimized use case is significantly simpler, with not even a line of code in the function body. The configuration of the YAML file is configured as a decorator on top of the test function, making it cleaner and easier to find places to modify.

The specific implementation

The implementation uses only two pyTest points: Mark and fixture. Take a look at the code:

# conftest.py
import pytest
import yaml
from selenium import webdriver
from keyworks import Page

@pytest.fixture
def driver() :
    d = webdriver.Chrome()
    d.implicitly_wait(8)
    d.maximize_window()
    yield d
    d.quit()

@pytest.fixture
def page(driver) :
    "" "access to the page "" "
    return Page(driver)

@pytest.fixture(autouse=True)
def yaml_case(page, request) :
    """ YAML Test Procedure """
    yaml_marker = request.node.get_closest_marker('yaml_case')
    yaml_file, *_ = yaml_marker.args
    with open(yaml_file, encoding='utf-8') as f:
        steps = yaml.safe_load(f)
        for step in steps:
            action = getattr(page, step['action'])
            action(**step['params'])
Copy the code

The focus is on the last fixture. First, I set the YAMl_case fixture to be automatic so I don’t need to call it manually in my test functions, so I don’t need to pass in any arguments in my use case functions.

In the yaml_case fixture, the first line of request.node.get_closest_marker(‘yaml_case’) gets the yaml_case mark, The second line of code yaml_marker.args gets the parameter in the tag, which is the path to the file signin.yaml. Next, read the test steps in this file and call the specific execution action, which was covered in the previous article, if in doubt.

@pytest.mark.yaml_case('signin.yaml')
def test_keyword() :
	pass
Copy the code

conclusion

The implementation of this code uses PyTest’s flexible mark mechanism and fixture management, which should not be difficult to implement if you are familiar with PyTest. If you have any questions and suggestions, welcome to discuss with me.