This is my participation 2022 For the first time, the third challenge is 9 God , Check out the activity details :2022 For the first time, it's a challenge
stay selenium How to implement Keyword Driven in , I wrote a very simple keyword driver , But this program just runs through the function , There's a lot of room for optimization , This article I want to pass pytest To simplify the writing of automated test cases , The use is relatively basic pytest function . I'll write another complex version of the next article , Execute directly from the bottom yaml File as use case .
Before optimization , If you want to add a use case , First you need to write a yaml file , And then another one python Automated test cases for , The code of the use case is as follows :
def test_keyword(driver):
""" obtain 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 code
Although this program is relatively simple to use , If you want to create other use cases , Just copy this function , modify signin.yaml file name , Other codes don't need to be moved , It's not enough to use your head , It doesn't look good to copy so much duplicate code every time .
import pytest
@pytest.mark.yaml_case('signin.yaml')
def test_keyword():
pass
Copy code
The optimized use case is obviously simpler , There is not even a single line of code in the body of the function . yaml The configuration of the file is configured above the test function in the form of decorator , More clarity , It's easy to find places when modifying .
In fact, the implementation method only uses pytest Two knowledge points of :mark and fixture, First 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):
""" obtain page"""
return Page(driver)
@pytest.fixture(autouse=True)
def yaml_case(page, request):
"""yaml testing 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 code
Focus on the last fixture. First of all, I put the yaml_case This fixture Set to automatic use , In this way, I don't need to call it manually in the test function , So I don't need to pass in any parameters in the use case function .
stay yaml_case This fixture in , First line of code request.node.get_closest_marker('yaml_case')
Get yaml_case This sign mark, Second line of code yaml_marker.args
Get the parameters in the tag , That is to say signin.yaml The path to this file . Next , Read the test steps in this file , And call the specific execution operation , The calling code has been discussed in the previous article , If you have any questions, you can turn it back .
@pytest.mark.yaml_case('signin.yaml')
def test_keyword():
pass
Copy code
The implementation of this code mainly uses pytest agile mark The mechanism and fixture management , as long as pytest Use it skillfully , It's not hard . If you have any questions and suggestions , Welcome to discuss with me in private letters .