python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Playwright Web自动化测试

Playwright中Web自动化测试的实现

作者:程序员与诗人

Playwright是一个现代的Web自动化测试框架,本文主要介绍了Playwright中Web自动化测试的实现,具有一定的参考价值,感兴趣的可以了解一下

1. 主流框架的认识

image.png

总结:

2. Playwright认识

Playwright简介

Playwright优势

3. Playwright环境搭建

Python环境:python解释器+IDE工具,不知道怎么安装的可参考: python3.4 + pycharm 环境安装 + pycharm 使用

Playwright环境:

安装Playwright:pip install playwright -i https://pypi.tuna.tsinghua.edu.cn/simple

image.png

安装内置浏览器(chromium,firefox,webkit):playwright install

image.png

4. Playwright入门实战

1)脚本录制

案例1:实现Web ui自动化,打开浏览器,访问测试派,点击登录后在进行登录操作(输入用户名、密码,点击登录按钮)

操作步骤:

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("http://175.178.53.95:8081/")
    page.get_by_text("登录").click()
    page.get_by_placeholder("用户名/邮箱/手机号").click()
    page.get_by_placeholder("用户名/邮箱/手机号").fill("kemi")
    page.get_by_placeholder("用户名/邮箱/手机号").press("Tab")
    page.get_by_placeholder("密码").fill("123456")
    page.pause()  # 断点调试功能
    page.get_by_role("button", name="登录").click()

    # ---------------------
    context.close()
    browser.close()


with sync_playwright() as playwright:
    run(playwright)

2)录制工具快速定位元素

如果我自己写代码,需要定位元素,通过Playwright Inspector工具的Pick locator可以快速获取元素定位的信息,具体操作如下截图:

image.png

Playwright支持两大类元素定位方式,包括:

1)get_by方式

2)page.locator方式

4. Pytest+Allure+Playwright完整项目演示

1)脚本转为测试用例

案例需求:将上面测试派的登录操作转为测试用例

操作步骤:

image.png

代码如下:

from playwright.sync_api import Page, expect

def test_example(page: Page) -> None:
    page.goto("http://175.178.53.95:8081/")
    page.get_by_text("登录").click()
    page.get_by_placeholder("用户名/邮箱/手机号").click()
    page.get_by_placeholder("用户名/邮箱/手机号").fill("kemi")
    page.get_by_placeholder("用户名/邮箱/手机号").press("Tab")
    page.get_by_placeholder("密码").fill("123456")
    page.get_by_role("button", name="登录").click()

执行用例前必须先安装插件,否则会报错,因为用例只有核心的业务操作,没有浏览器、上下文、页面的初始化,装了插件后会自动做这些初始化的操作,从而执行用例不会报错。命令:pip install pytest-playwright

image.png

2)数据驱动

集合pytest的parametrize实现数据驱动

import pytest
from playwright.sync_api import Page, expect

# 测试用例数据
case_data = [{'caseid':1,'username':'kemi123', 'password':'kemi123'},
             {'caseid':2,'username':'zhangsan', 'password':'123456'},
             {'caseid':3,'username':'lisi', 'password':'123456'}]

# 数据驱动
@pytest.mark.parametrize('case', case_data)
def test_example(page: Page,case) -> None:
    username = case['username']
    password = case['password']

    page.goto("http://175.178.53.95:8081/")
    page.get_by_text("登录").click()
    page.get_by_placeholder("用户名/邮箱/手机号").click()
    page.get_by_placeholder("用户名/邮箱/手机号").fill(username)
    page.get_by_placeholder("用户名/邮箱/手机号").press("Tab")
    page.get_by_placeholder("密码").fill(password)
    page.get_by_role("button", name="登录").click()
    page.screenshot(path=f"images/login/登录{case['caseid']}.png")  # 截图保存到指定位置

3)报告显示

通过pytest执行用例后,通过命令生成报告:allure serve outputs/allure,查看报告显示,截图如下:

image.png

到此这篇关于Playwright中Web自动化测试的实现的文章就介绍到这了,更多相关Playwright Web自动化测试内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文