• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
金大鑫要坚持
博客园    首页    新随笔    联系   管理    订阅  订阅

【playwright学习】wait_for_url的三种传参方法 in github create issue test

from playwright.sync_api import sync_playwright,Page,Playwright,Browser,expect
import pytest
import random
import re
@pytest.fixture(scope="module",autouse=True)
def login_github(playwright:Playwright):
    chrome = playwright.chromium.launch(headless=False)
    context = chrome.new_context()
    page = context.new_page()
    page.goto('https://github.com/login')
    # Interact with login form
    page.get_by_label("Username or email address").fill("120946018@qq.com")
    page.get_by_label("Password").fill("1***")
    page.get_by_role("button", name="Sign in", exact=True).click()

    # Verify login success
    # page.wait_for_url("https://github.com/jin-wen-xin")
    storage = context.storage_state(path="state.json")
    yield storage
    # Close browser
    context.close()
    #chrome.close()

def test_login_github(browser:Browser, login_github):
    context = browser.new_context(storage_state="state.json")
    page = context.new_page()
    page.goto('https://github.com/microsoft/playwright-pytest/blob/main/pytest_playwright/pytest_playwright.py')
    page.get_by_label("Open global navigation menu").click()
    page.get_by_role("link", name="Jinwenxin/frontblog").click()
    expect(page.get_by_label("Page context", exact=True).get_by_role("list")).to_contain_text("Jinwenxin")

def test_create_issue(browser:Browser, login_github):
    context = browser.new_context(storage_state="state.json")
    page = context.new_page()
    page.goto('https://github.com/Jinwenxin/frontblog/issues/new')
    # generate random issue title and description
    issue_title = "Test issue" + str(random.randint(1,1000))
    issue_description = "test issue description" + str(random.randint(1,1000))
    page.get_by_label("Title").fill(issue_title)
    page.get_by_placeholder(" ", exact=True).click()
    page.get_by_placeholder(" ", exact=True).fill(issue_description)
    page.get_by_role("button", name="Submit new issue").click()
    # Verify issue creation success
    # Verify url contains issue number
    pattern = r'.*/Jinwenxin/frontblog/issues/\d+$'
    url_pattern = re.compile(pattern)
    page.wait_for_url(url_pattern)
    #page.wait_for_event('framenavigated', lambda frame: "/Jinwenxin/frontblog/issues/" in frame.url and frame.url.split("/")[-1].isdigit())
    expect(page.locator("bdi")).to_contain_text(issue_title)
    expect(page.get_by_role("cell")).to_contain_text(issue_description)

值得看看的是wait_for_url方法

    def wait_for_url(
        self,
        url: typing.Union[str, typing.Pattern[str], typing.Callable[[str], bool]],
        *,
        wait_until: typing.Optional[
            Literal["commit", "domcontentloaded", "load", "networkidle"]
        ] = None,
        timeout: typing.Optional[float] = None
    ) -> None:

可以传字符串,pattern对象,还有callable对象

pattern得compile才行。。。我由于基础差,试了好久

wait_for_url 传pattern的话就是上面那么用。

传callable,就是自己定义一个传string,返回bool的方法。

url_validator 被赋予了 include_url 函数的引用,并且可以通过 url_validator  来调用 include_url函数,就像使用普通的函数一样。

 

def include_url(url:str)->bool:
    pattern = r'.*/Jinwenxin/frontblog/issues/\d+$'
    url_pattern = re.compile(pattern)
    return bool(url_pattern.match(url))

url_validator : Callable[[str], bool] = include_url

test里这样去使用:

page.wait_for_url(url_validator)

我再试试非得new一个引用吗?我直接用可以吗?哈哈也可以的。。。可能那样更清晰?

posted @ 2024-07-03 12:10  金大鑫要坚持  阅读(377)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3