python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Playwright设置base_url

Playwright设置base_url的三种方式

作者:南部余额

本文主要介绍了三种在使用Playwright或pytest-playwright进行Web自动化测试时设置base_url的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

方式一:playwright创建new_context()

    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配置文件如下:

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

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

pip install pytest-base-url

pytest.ini配置文件如下:

[pytest]
base_url = http://localhost:8080

到此这篇关于Playwright设置base_url的三种方式的文章就介绍到这了,更多相关Playwright设置base_url内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

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