什么是Playwright? Playwright是一个开源的自动化测试工具,用于测试网页应用。 Playwright的原理是什么? Playwright通过直接控制浏览器引擎(如Chromium和Firefox)来执行测试,而不是通过浏览器界面。 这意味着Playwright可以直接操控浏览器引擎,执行更快速和稳定的测试。Playwright使用浏览器引擎自带的JavaScript执行环境和Web API来控制浏览器。 Playwright的主要应用场景有: • 跨浏览器测试:可以使用Playwright在Chromium、Firefox和WebKit等主流浏览器中执行测试,覆盖更广范围的用户场景。 在Python中安装Playwright: pip install playwright 然后我们可以编写第一个脚本: from playwright.sync_api import sync_playwright
安装playwright只需要一条命令,就是pip安装命令,命令如下: pip install playwright 注:playwright需要Python3.7或更新的版本 2、安装浏览器 和selenium 安装命令如下: playwright install 直接上图 解决国内安装慢的问题: pip install -i https://pypi.douban.com/simple playwright 安装所需的浏览器 可以playwright --version 查看版本是否安装好了 是不是很简单,一起来学吧!
上篇我们已经安装好了playwright和各个浏览器,那么现在我们直接开始吧 1、怎么使用playwright? 我们需要先导入sync_playwright,然后用start启动,stop关闭 直接举例 使用谷歌浏览器打开百度网页 from playwright.sync_api import sync_playwright # 先导包 playwright = sync_playwright().start() # 创建playwright对象 browser = playwright.chromium.launch () # 关闭playwright对象释放资源 上述是跟selenium同样的,都属于同步运行,playwright有2种运行方式:同步和异步 与selenium sync_playwright # 先导包 playwright = sync_playwright().start() # 创建playwright
一、page_func.py # coding=utf-8 """ @Project :playwright_test @File :page_func.py @Author gaojs @Date :2022/7/14 21:48 @Blogs : https://www.gaojs.com.cn """ import time import playwright from playwright.sync_api import Playwright, sync_playwright, expect from playwright.sync_api import sync_playwright class AMPTest: """ amp类 """ def __init__(self): """ 初始化参数 """ self.playwright = sync_playwright().start() self.browser = self.playwright.chromium.launch
话不多说直接安装 Playwright 目前提供了 Python 和 Node.js 的 API,我对 Python 版的 Playwright 进行介绍。 使用 Playwright支持两种编写模式,同步和异步 同步例子 from playwright.sync_api import sync_playwright with sync_playwright 自动生成代码 懒人福音 使用playwright的codegen 来实现 查看命令参数 playwright codegen --help Usage: npx playwright codegen [ run(playwright: Playwright) -> None: browser = playwright.chromium.launch(headless=False) context () as playwright: run(playwright) 移动端浏览器模拟 Playwright 另外一个特色功能就是可以支持移动端浏览器的模拟,比如模拟打开 iPhone 12 Pro
Playwright官网 https://playwright.dev/playwright.dev/ Playwright的Github链接 https://github.com/microsoft/ playwright-pythongithub.com/microsoft/playwright-python Playwright教程(2)— 安装及自动生成代码 安装playwright pip install playwright 官网推荐使用pip install pytest-playwright来安装,但没必要,会安装playwright及其他一堆测试所用的库,如果只是使用playwright,那么就没必要这样去安装 () as playwright: run(playwright) 异步方式代码模板 import asyncio from playwright.async_api import Playwright , async_playwright, expect async def run(playwright: Playwright) -> None: browser = await playwright.chromium.launch
本章我们将学习如何使用Playwright进行网页性能测试,掌握性能指标的测量方法和如何分析测试报告进行性能优化。 Playwright性能测试API Playwright提供以下主要API用于性能测试: - page.metrics():获取页面加载及性能相关指标。 运行测试并获取性能数据 运行测试脚本,Playwright自动获取页面加载指标和资源指标。 Playwright与Lighthouse进行深度整合,可以通过page.evaluateHandle()方法直接在Playwright测试中生成Lighthouse报告。 借助Playwright,可以更高效地进行性能增强,打造高性能的Web应用。
前言 playwright 设置 ignore_https_errors 参数忽略 SSL 错误 context 上下文中设置 browser.new_context() 创建上下文时 from playwright.sync_api import sync_playwright, expect with sync_playwright() as p: browser = p.chromium.launch(headless ignore_https_errors=True) page = context.new_page() codegen 录制用例 录制用例时,启动命令添加--ignore-https-errors playwright codegen --ignore-https-errors https://example.com 结合 pytest-playwright 用例插件 pytest-playwright 插件写自动化用例时
我喜欢Playwright! 这是微软开源的一款非常强大的自动化工具,再过几年,他很有可能取代Selenium在浏览器自动化的通知地位。 playwright codegen https://v4.ketangpai.com/User/login.html弹出浏览器和代码生成界面,在界面的菜单栏可以停止录制,复制代码。 playwright inspector 除了可以进行录制,还可以辅助元素定位。 当暂停录制后,在页面下方会出现辅助定位的控件,当输入元素定位表达式后,对应的页面元素将会高亮显示。
import sync_playwright def run_request(playwright): chromium = playwright.chromium # or "firefox # 手机设备设置为iPhone 12 Pro Max iphone_12 = playwright.devices['iPhone 12'] # 启动谷歌有窗口浏览器 () as playwright: run_request(playwright) 高德H5网页为例,运行后,可查看监听结果: request: GET https://m.amap.com import sync_playwright import re def run_route(playwright): """ 拦截所有的图片请求 :param playwright : :return: """ chromium = playwright.chromium # or "firefox" or "webkit"
Python示例 from playwright.sync_api import sync_playwright def run(playwright): browser = playwright.chromium.launch browser.close() with sync_playwright() as playwright: run(playwright) 这个测试脚本在运行过程中进行了录屏,并生成了test.mp4 public static void main(String[] args) { Playwright playwright = Playwright.create(); Python示例 from playwright.sync_api import sync_playwright def run(playwright): browser = playwright.chromium.launch () as playwright: run(playwright) 这个测试脚本访问了example.com网站, Playwright会在测试结束时生成metrics.json报告,包含页面加载时间
在 Playwright 测试中,我们经常会遇到一些实用场景,如等待页面加载、处理弹窗、上传下载文件等。 Playwright 提供了专门的 API 用于上传和下载文件。 这里推荐两款支持 Playwright 的断言库: - `expect-playwright`:Playwright 官方推荐的断言库,API 与 Jest expect 类似,提供丰富的 Playwright - `chai`:功能强大的通用断言库,可以通过 `chai-playwright` 扩展包使用 Playwright 相关断expect-playwright言。 expect-playwright示例 await expect(page).toHaveTitle('Playwright'); await expect(text).toBeVisible();
如果用Playwright,该怎么办呢? 我们来看命令选项,看有没有解决方案。 例如用下面命令访问网站并登陆,关闭浏览器时自动把cookie等浏览器信息存入hik文件中: python -m playwright cr https://XXX.top --save-storage ,使用时输入命令即可,文件默认储存在当前执行命令的目录 在网站录制操作的过程中也可以用--sava,例如: python -m playwright codegen --target python -o codegen --target python -o 'run.py' https://xxx.top --load-storage cway 然后我们看一下录制的代码: def run(playwright ,phonenumber): browser = playwright.chromium.launch(headless=True) context = browser.new_context
Playwright与selenium都是基于WEB的GUI自动化测试的框架,前面我介绍了selenium的封装,这里来介绍基于Playwright的封装。 基础操作(basepage) basepage目录封装基础操作,包括base.py、db.py和page.py 2.1 base.py base.py对Playwright专门做了调整,包括base、judge /data") from playwright.sync_api import sync_playwright class mypages: def __init__(self,config 比较 最后让我们比较一下selenium、playwright有头模式和无头模式运行电子商务产品7个用例的时间。 selenium 200.651s playwright有头模式 29.960s playwright无头模式 24.954s 由此可见从运行速度而言playwright远远都快于selenium、无头模式快于有头模式
本章我们将学习如何封装 Playwright 测试以及部署测试用例,来完善测试开发的最后一环。 Playwright 测试封装 为了便于管理和维护测试脚本,我们需要对测试进行封装和模块化设计。 以上就是 Playwright 测试封装的常用方式。 在测试环境中安装 Playwright:npm install playwright。 3. 执行测试命令:npx playwright test tests/ 运行测试文件夹中的所有测试。 4. 那么,Playwright系列之旅到这里就真的结束了。 配合Playwright强大的API,你可以设计出高效和全面,同时兼顾质量和发布频率的测试方案。 我相信通过阅读这16章内容,对Playwright和自动化测试你有了很深入的认识。
Python - 同步模式 使用sync_api,我们可以编写同步的测试脚本: from playwright.sync_api import sync_playwright def run(playwright () as playwright: run(playwright) 这里我们使用sync_playwright上下文管理器同步启动Playwright,然后编写同步测试逻辑。 Python - 异步模式 使用async_api,我们可以编写异步的测试脚本: from playwright.async_api import async_playwright async def () as playwright: await run(playwright) asyncio.run(main()) 这里我们使用asyncio模块异步启动Playwright,然后编写异步测试逻辑 .*; public class Example { public static void main(String[] args) { Playwright playwright = Playwright.create
playwright-java 的 Browser、BrowserContext、Page 挺好理解的,唯独这厮,就有一丢丢 ……package com.microsoft.playwright;/** package demo;import com.microsoft.playwright. playwright = Playwright.create()) { // 三个 BrowserType List<BrowserType> browserTypes = Arrays.asList ( playwright.chromium(), playwright.webkit(), playwright.firefox #createImplcom.microsoft.playwright.impl.ReaderThread#runcom.microsoft.playwright.impl.ChannelOwner#runUntilcom.microsoft.playwright.impl.Connection
Playwright的优势总结下来具体如下。 pip3 install playwright pip3 install pytest-playwright Playwright支持主流的浏览器,但是首先是需要安装浏览器的驱动,安装命令以及安装后输出的信息如下 playwright install #执行如上命令后输出的结果信息如下 Downloading Chromium 112.0.5615.29 (playwright build v1055) /Caches/ms-playwright/chromium-1055 Downloading FFMPEG playwright build v1008 from https://playwright.azureedge.net 理解了这部分,其实再看最初的Playwright编写的测试代码部分,就很轻松了。结合一个具体的案例来使用Playwright来测试下WEB产品,案例代码如下。 #!
今天我们来讲下文本输入这个操作 上文中我们已经可以定位元素了,并且还学习了点击操作,但有些是需要我们输入文本信息的,我们来学习下playwright的文本输入 1、文本输入 page.fill(selector
什么是 Playwright 呢?简单来说,Playwright 就像是一位万能的遥控器,可以遥控 Chrome、Firefox、Safari(其实是 WebKit)这些浏览器。 你只需要打开命令行,输入下面这条命令就能安装 Playwright 的 Python 包:pip install playwright安装完了以后,还得告诉 Playwright “嘿,快去下载你需要用到的浏览器驱动吧 这四步用代码写起来就像下面这样:from playwright.sync_api import sync_playwrightdef login(): with sync_playwright() 别担心,Playwright 也有办法解决这个问题。你只需要告诉 Playwright “等一下,等到页面上某个特定的元素出现以后再继续操作”,就能保证你获取到完整的数据。 Playwright 同样支持录制视频,只不过设置起来稍微复杂一点。