本文主要是介绍Playwright 和 Pytest 之 自动化用例自愈功能实战,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言
当使用 Playwright 的 Locator 进行元素定位,并结合 pytest 编写自动化测试时,可以利用 pytest 的功能和 Playwright 提供的定位方法来实现自动化修复。下面我将详细展示如何实现这些功能:
自动化修复
在测试过程中捕获失败并尝试自动修复问题。我们将使用 Playwright 的 Locator 来定位元素,并在失败时重新定位更新元素。
base.py
# -*- coding: utf-8 -*-
# @Author : blues_C
# @File : base.py
# @Desc:import re
from utils.logger import log
from playwright.sync_api import Page, expect, Locatorclass BasePage:def __init__(self, page: Page):self.page = pagedef locator(self, selector: str) -> Locator:"""查找并返回元素"""element = self.page.locator(selector)if not element.is_visible():self.auto_repair()return elementdef click(self, selector: str):self.locator(selector).click()def fill(self, selector: str, input_value: str):self.locator(selector).fill(input_value)def auto_repair(self, scope_selector: str = "body",selector: str = "input, button, a, select, textarea, div, span, img, iframe, label, svg",filename="login_element.py"):page_title = self.page.title()scope = self.page.locator(scope_selector)elements = scope.locator(selector).all()log.info(f"自愈元素: {selector} (共 {len(elements)} 个)")# 检查文件是否存在if os.path.exists(filename):# 读取现有文件内容,以便检查变量名with open(filename, "r") as file:lines = file.readlines()existing_variables = set()# 获取现有的变量名for line in lines:if "=" in line:variable_name = line.split("=")[0].strip()existing_variables.add(variable_name)# 打开文件,准备写入新内容with open(filename, "w") as file:file.write(f"# Existing variables from {page_title}\n\n")for element in elements:element_info = f"element='{element}', "start_index = element_info.find("selector=")if start_index != -1:selector_str = element_info[start_index + len("selector='"):]end_index = selector_str.find("'")if end_index != -1:selector_content = selector_str[:end_index]# 获取各个属性text = element.inner_text()value = element.get_attribute('value')element_id = element.get_attribute('id')element_class = element.get_attribute('class')element_name = element.get_attribute('name')element_type = element.get_attribute('type')element_placeholder = element.get_attribute('placeholder')# 检查属性是否为 None,并记录非 None 的属性log_info = f"element='{selector_content}', "if text != '':log_info += f"text='{text}' "if value != '' and value is not None:log_info += f"[value='{value}'] "if element_id is not None:log_info += f"#{element_id} "if element_class is not None:log_info += f"[class='{element_class}'] "if element_name is not None:log_info += f"[name='{element_name}'] "if element_type is not None:log_info += f"[type='{element_type}'] "if element_placeholder is not None:log_info += f"[placeholder='{element_placeholder}']"# 打印日志信息log.info(log_info)# 如果变量名已存在,替换现有的定义行if variable_name in existing_variables:file.write(f"# {log_info} (replaced)\n")if element.get_attribute('name') is not None:variable_name = element.get_attribute('name')variable_type = selector_contentfile.write(f"{variable_name} = '{variable_type}'\n\n")existing_variables.add(variable_name)elif element.get_attribute('type') is not None:variable_name = element.get_attribute('type')variable_type = selector_contentfile.write(f"{variable_name} = '{variable_type}'\n\n")existing_variables.add(variable_name)else:variable_type = selector_contentfile.write(f"'{variable_type}'\n\n")existing_variables.add(variable_name)else:# 如果文件不存在,则创建新文件并写入内容with open(filename, "w") as file:file.write(f"# {page_title}\n")for element in elements:element_info = f"element='{element}', "start_index = element_info.find("selector=")if start_index != -1:selector_str = element_info[start_index + len("selector='"):]end_index = selector_str.find("'")if end_index != -1:selector_content = selector_str[:end_index]text = element.inner_text()value = element.get_attribute('value')element_id = element.get_attribute('id')element_class = element.get_attribute('class')element_name = element.get_attribute('name')element_type = element.get_attribute('type')element_placeholder = element.get_attribute('placeholder')info = f"element='{selector_content}', "if text != '':info += f"text='{text}' "if value != '' and value is not None:info += f"[value='{value}'] "if element_id is not None:info += f"#{element_id} "if element_class is not None:info += f"[class='{element_class}'] "if element_name is not None:info += f"[name='{element_name}'] "if element_type is not None:info += f"[type='{element_type}'] "if element_placeholder is not None:info += f"[placeholder='{element_placeholder}']"log.info(info)file.write(f"# {info}\n")if element.get_attribute('name') is not None:variable_name = element.get_attribute('name')variable_type = selector_contentfile.write(f"{variable_name} = '{variable_type}'\n\n")elif element.get_attribute('type') is not None:variable_name = element.get_attribute('type')variable_type = selector_contentfile.write(f"{variable_name} = '{variable_type}'\n\n")else:variable_type = selector_contentfile.write(f"'{variable_type}'\n\n")
conftest.py
import pytest
from playwright.sync_api import sync_playwright@pytest.fixture(scope='session')
def browser():with sync_playwright() as p:browser = p.chromium.launch()yield browserbrowser.close()
test_cases.py
import pytest
from common.base import BasePage
from playwright.sync_api import sync_playwrightdef test_auto_repair(browser):page = browser.new_page()page.goto('https://example.com')try:# 运行测试步骤BasePage(page).fill(login_element.username, 'username')BasePage(page).fill(login_element.password, 'password')BasePage(page).click(login_element.login)except Exception as e:pytest.fail(f'Test failed: {e}')BasePage(page).fill(login_element.username, 'username')BasePage(page).fill(login_element.password, 'password')BasePage(page).click(login_element.login)
login_element.py 示例:
# 登录到 standard
# element='form >> input,button >> nth=0', #username [class='form-control'] [name='username'] [type='text']
username = 'form >> input,button >> nth=0'# element='form >> input,button >> nth=1', #password [class='form-control'] [name='password'] [type='password']
password = 'form >> input,button >> nth=1'# element='form >> input,button >> nth=2', #id-hidden-input [name='credentialId'] [type='hidden']
credentialId = 'form >> input,button >> nth=2'# element='form >> input,button >> nth=3', [value='登录'] #kc-login [class='btn btn-primary btn-block btn-lg'] [name='login'] [type='submit']
login = 'form >> input,button >> nth=3'
总结
在上面的示例中:
pytest.fixture(scope=‘session’) 是一个 pytest fixture,用于在测试函数运行前后启动和关闭浏览器。
page.locator(‘selector’) 定位页面上的元素,如果元素不可见或定位失败,则重新定位更新元素。
如果测试步骤失败(例如,元素点击失败),则使用 pytest.fail() 来标记测试失败。
这篇关于Playwright 和 Pytest 之 自动化用例自愈功能实战的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!