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("[email protected]") 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一个引用吗?我直接用可以吗?哈哈也可以的。。。可能那样更清晰?
标签:playwright,get,url,pattern,create,github,context,issue,page From: https://www.cnblogs.com/jin-wen-xin/p/18281370