点击名片关注 阿尘blog,一起学习,一起成长
本部分内容主要测试将playwright实际用于pytest的可行性
事实证明:确实很简单
网上有许多可用于测试Web UI自动化的网站,这里我们直接挑选一个电商网站作为测试实践
测试网站:https://www.saucedemo.com/
1、开始录制脚本
首先尝试看看能否录制pytest
python -m playwright codegen -o "D:\PythonProject\playwright_project2\code.py" --target python-pytest
结果:
from playwright.sync_api import Page, expect
def test_example(page: Page) -> None:
page.goto("https://www.saucedemo.com/")
page.locator("[data-test=\"username\"]").click()
page.locator("[data-test=\"username\"]").fill("standard_user")
page.locator("[data-test=\"password\"]").click()
page.locator("[data-test=\"password\"]").fill("secret_sauce")
page.locator("[data-test=\"login-button\"]").click()
page.locator("[data-test=\"add-to-cart-sauce-labs-backpack\"]").click()
page.locator("[data-test=\"add-to-cart-sauce-labs-bolt-t-shirt\"]").click()
page.locator("[data-test=\"add-to-cart-sauce-labs-onesie\"]").click()
page.locator("a").filter(has_text="3").click()
page.locator("[data-test=\"checkout\"]").click()
page.locator("[data-test=\"cancel\"]").click()
page.get_by_text("Swag Labs").click()
这似乎并没有不同,看来不符合自己的预期不能自动实现与pytest的结合,这样看来其实我们用到的也只是playwright的元素定位,那么我们换回默认的录制方法:
python -m playwright codegen -o "D:\PythonProject\playwright_project2\code.py"
登录录制脚本如下:
from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
page.goto("https://www.saucedemo.com/")
page.get_by_text("Accepted usernames are:standard_userlocked_out_userproblem_userperformance_glitc").dblclick()
page.locator("[data-test=\"username\"]").click()
page.locator("[data-test=\"username\"]").fill("standard_user")
page.locator("[data-test=\"password\"]").click()
page.locator("[data-test=\"password\"]").fill("secret_sauce")
page.locator("[data-test=\"login-button\"]").click()
# ---------------------
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)
2、编写一个验证登录的测试用例
先下载pytest框架
pip install pytest -i http://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn
一个测试用例,把它分为page层,用于管理公共元素;case_data 层用于储存数据,便于管理;testcase层用于编写测试用例,其中结合PO封装模式实现元素操作,结合pytest.mark.paramtrize实现参数化、结合pytest.fixture实现预处理
目录架构图:
现在我们开始将元素定位扔到page层面
login_ele = {"username": "[data-test=\'username\']",
"password" : "[data-test=\'password\']",
"login_button" : "[data-test=\'login-button\']",
"Swag_Labs" : 'Swag Labs'}
接着定义数据层:
先定义一个用例的数据
url = 'https://www.saucedemo.com/'
success_login = [{'username':'standard_user',
'password':'secret_sauce',
'expect_url':'https://www.saucedemo.com/inventory.html',
'erro_info1':'wrong net address',
'expect_text':'Swag Labs',
'erro_info2':'wrong page text'}]
再来定义前置,打开conftest.py
import pytest
from case_data.login_data import url
from playwright.sync_api import sync_playwright
@pytest.fixture(scope="class")
def every_one():
with sync_playwright() as playwright:
browser = playwright.chromium.launch()
context = browser.new_context()
page =context.new_page()
page.goto(url)
yield page
context.close()
page.close()
接下来正式编辑用例层
import case_data.login_data as lg
import page.page_index as pg
import pytest
class Test_login():
@pytest.mark.parametrize('info',lg.success_login)
def test_login(self,every_one,info):
page = every_one
page.locator(pg.login_ele['username']).fill(info['username'])
page.locator(pg.login_ele['password']).fill(info['password'])
page.locator(pg.login_ele['login_button']).click()
aspect_url = page.url
expect_url = info['expect_url']
aspect_text = page.get_by_text(pg.login_ele['Swag_Labs']).text_content()
expect_text = info['expect_text']
assert aspect_url == expect_url,info['erro_info1']
assert aspect_text == expect_text,info['erro_info2']
if __name__ == '__main__':
pytest.main()
运行结果:
好了今天到这,浅尝辄止,更多用例以及结合allure报告就没有多大差别,可以参考以往文章Python-Ui自动化测试
注意事项(避坑):
1、文件名一定要正确,测试用例层的类名和方法名一定要正确
2、尽量不要更改文件名,不要直接把录制脚本的文件名直接改为用例层的文件名,如果改了,就把代码复制重新粘贴一遍,正常先把目录创建好再挨个编辑
3、前置一定要用with 关键字,不要和网上那样,否则会报错
错误示例:以下都会导致前置启动失败
@pytest.fixture(scope="function")
def preview(page):
page.goto(pg.url)
page.click(pg.popup)
yield page
page.close()
#*********或者*****
from playwright.sync_api import Playwright
import pytest
import case_data.login_data as lg
@pytest.fixture(scope='class')
def every_test(playwright:Playwright):
# 定义前置,开启用例场景打开page
browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
page.goto(lg.url)
yield page
#定义后置
context.close()
page.close()
扫描二维码关注阿尘blog,一起交流学习