当前位置: 首页 > news >正文

自动化测试工具playwright中文文档-------14.Chrome 插件

介绍

注意

插件仅在以持久化上下文启动的 Chrome/Chromium 浏览器中工作。请谨慎使用自定义浏览器参数,因为其中一些可能会破坏 Playwright 的功能。

以下是获取位于 ./my-extension 的 Manifest v2 插件背景页面句柄的代码示例。

from playwright.sync_api import sync_playwright, Playwrightpath_to_extension = "./my-extension"
user_data_dir = "/tmp/test-user-data-dir"def run(playwright: Playwright):context = playwright.chromium.launch_persistent_context(user_data_dir,headless=False,args=[f"--disable-extensions-except={path_to_extension}",f"--load-extension={path_to_extension}",],)if len(context.background_pages) == 0:background_page = context.wait_for_event('backgroundpage')else:background_page = context.background_pages[0]# Test the background page as you would any other page.context.close()with sync_playwright() as playwright:run(playwright)

测试

为了在运行测试时加载扩展,你可以使用测试夹具(fixture)来设置上下文。此外,你还可以动态地检索扩展ID,并使用它来加载和测试例如弹出页面等。

首先,添加将加载扩展的测试夹具(fixtures)。

conftest.py
from typing import Generator
from pathlib import Path
from playwright.sync_api import Playwright, BrowserContext
import pytest@pytest.fixture()
def context(playwright: Playwright) -> Generator[BrowserContext, None, None]:path_to_extension = Path(__file__).parent.joinpath("my-extension")context = playwright.chromium.launch_persistent_context("",headless=False,args=[f"--disable-extensions-except={path_to_extension}",f"--load-extension={path_to_extension}",],)yield contextcontext.close()@pytest.fixture()
def extension_id(context) -> Generator[str, None, None]:# for manifest v2:# background = context.background_pages[0]# if not background:#     background = context.wait_for_event("backgroundpage")# for manifest v3:background = context.service_workers[0]if not background:background = context.wait_for_event("serviceworker")extension_id = background.url.split("/")[2]yield extension_id

然后在测试中使用

test_foo.py
from playwright.sync_api import expect, Pagedef test_example_test(page: Page) -> None:page.goto("https://example.com")expect(page.locator("body")).to_contain_text("Changed by my-extension")def test_popup_page(page: Page, extension_id: str) -> None:page.goto(f"chrome-extension://{extension_id}/popup.html")expect(page.locator("body")).to_have_text("my-extension popup")

无头模式(Headless Mode)

警告

--headless=new 模式并不是 Playwright 官方支持的功能,并且可能会导致意外的行为。

在 Playwright 中,Chrome 的默认无头模式并不支持 Chrome 扩展。为了克服这一限制,你可以通过以下代码以新的无头模式运行 Chrome 的持久化上下文(persistent context):

conftest.py
path_to_extension = Path(__file__).parent.joinpath("my-extension")
context = playwright.chromium.launch_persistent_context("",headless=False,args=["--headless=new",f"--disable-extensions-except={path_to_extension}", # 禁用 Chrome 浏览器中的所有扩展,除了指定路径({path_to_extension})下的扩展。f"--load-extension={path_to_extension}", # 指定 Chrome 浏览器启动时应该加载的扩展的路径],
)

相关文章:

  • 如何在NS3中搭建窄带干扰和扫频干扰场景?
  • 844. 比较含退格的字符串
  • 安装SQLServer管理工具
  • 日语学习-日语知识点小记-构建基础-JLPT-N4阶段(4): 可能形(かのうけい)
  • Coze平台技术解析:零代码AI开发与智能体应用实践
  • 跑得快的标准详细规则·棒球1号位
  • OSPF协议基础
  • 《分布式软总线:网络抖动下的数据传输“定海神针”》
  • 【四川省第三届青少年C++算法设计大赛 (小低组) 第 一试】
  • 【Java学习】全局异常处理器的使用流程
  • opencv二值化实验
  • 解决7穴连接器的信号完整性问题
  • Linux——管道
  • OpenCV---图像基础
  • 4.14-4.15学习总结 IO流:缓冲流+转换流+序列化流+打印流+压缩流+Commons—io工具包+Hutool工具包
  • ZUS6000在无线充电测试上的应用
  • Java二分查找——数据结构篇
  • linux 内核 static-key机制分析
  • 数据结构(三)---单向循环链表
  • 【Pandas】pandas DataFrame itertuples
  • 牛市早报|特朗普称或将“大幅降低”对华关税,外交部回应
  • 科普|结石疼痛背后的危机信号:疼痛消失≠警报解除
  • 被电诈100万元又要被骗71万元,女子经民警近8小时劝阻幡然醒悟
  • 吉林省委原书记、吉林省人大常委会原主任何竹康逝世
  • 直播中抢镜“甲亢哥”的翁东华卸任!此前任文和友小龙虾公司董事
  • 印尼塔劳群岛附近发生6.3级左右地震