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

Playwright设置base_url的三种方式

方式一:playwright创建new_context()

  • 在new_context(base_url=‘’)中指定base_url
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context(base_url="http://localhost:8080")

示例代码:

import json

from playwright.sync_api import Playwright, sync_playwright


def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context(base_url="http://localhost:8080")
    page = context.new_page()
    page.goto("/login")
    page.get_by_role("textbox", name="用户名").click()
    page.get_by_role("textbox", name="用户名").fill("admin")
    page.get_by_role("textbox", name="密码").click()
    page.get_by_role("textbox", name="密码").fill("admin123")
    page.get_by_role("button", name="登录").click()
    # 等待页面跳转,作为登录成功的标准
    page.wait_for_url(url='http://localhost:8080/index')
    # page.pause()

    # 保存storage state 到指定的文件
    # 使用该方法前,需要确认网站cookie的失效时间
    storage = context.storage_state(path='../../auth/ry_auto.json')
    print(storage)
    # ---------------------
    context.close()
    browser.close()


with sync_playwright() as playwright:
    run(playwright)

方式二:使用插件pytest-playwright

pip install pytest-playwright
  • pytest.ini配置文件中,使用addopts属性在命令行中添加参数--base-url http://localhost:8080

pytest.ini配置文件如下:

[pytest]
addopts = -s --base-url http://localhost:8080

方式三:使用插件pytest-base-url

  • 该插件在pytest.ini配置文件中,添加了一个行属性,base_url
pip install pytest-base-url

pytest.ini配置文件如下:

[pytest]
base_url = http://localhost:8080
http://www.dtcms.com/a/97884.html

相关文章:

  • BUUCTF-web刷题篇(3)
  • PHP如何搭建设置代理http并加密使用?
  • PPT——组合SCI论文图片
  • 深入解析:MySQL 中 NULL 值是否占用 1 bit 存储空间?
  • “三小时搞定AI工具开发“:基于MCP的Node.js极简实践
  • Optional的stream方法,flatMap, filter应用
  • Imgui处理glfw的鼠标键盘的方法
  • stable diffusion webui 更改为python3.11版本运行Windows11
  • 【C++】右值引用与完美转发
  • 软件工程面试题(十)
  • 妙用《甄嬛传》中的选妃来记忆概率论中的乘法公式
  • 交换技术综合实验
  • 第四章.4.3.1ESP32传感器数据采集与滤波处理实战教程
  • 从0开始——在PlatformIO下开展STM32单片机的HAL库函数编程指南
  • ​​​​​​​​​​​​​​Spring Boot数据库连接池
  • Vue学习笔记集--computed
  • 蓝桥杯-特殊的多边形(dfs/前缀和)
  • 指针和引用
  • 业务流程先导及流程图回顾
  • YOLO基础知识
  • 【C语言文件精选题】
  • 《网络管理》实践环节01:OpenEuler22.03sp4安装zabbix6.2
  • 验证Linux多进程时间片切换的程序
  • PyTorch 张量的new_tensor方法介绍
  • 算法基础——树
  • RAG基建之PDF解析的“流水线”魔法之旅
  • 网络安全-网络安全基础
  • freecad gear模块 生成齿轮导出fcstd step
  • 20组电影美学RED摄像摄影机视频胶片模拟色彩分级调色LUT预设包 Pixflow – CL – RED Camera LUTs
  • 项目实战--权限列表