Playwright设置base_url的三种方式

作者:

文章目录
  • 在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)
  • pip install pytest-playwright 在pytest.ini配置文件中,使用addopts属性在命令行中添加参数–base-url http://localhost:8080 pytest.ini配置文件如下: [pytest] addopts = -s –base-url http://localhost:8080
  • 该插件在pytest.ini配置文件中,添加了一个行属性,base_url。 pip install pytest-base-url pytest.ini配置文件如下: [pytest] base_url = http://localhost:8080 到此这篇关于Playwright设置base_url的三种方式的文章就介绍到这了,更多相关Playwright设置base_url内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客! 您可能感兴趣的文章: python playwright–pytest-playwright、pytest-base-url插件编写用例
  • 目录
    • 方式一:playwright创建new_context()
    • 方式二:使用插件pytest-playwright
    • 方式三:使用插件pytest-base-url

    • 在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)
    
    

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

    pytest.ini配置文件如下:

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

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

    pytest.ini配置文件如下:

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

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

    您可能感兴趣的文章:

    • python playwright–pytest-playwright、pytest-base-url插件编写用例

    站内搜索