我正在使用 playwright 来运行 UI 测试。这个测试请求获取一个URL,登录,然后检查页面内容。作为开始,我只是尝试登录并捕获
import keyring
import asyncio
from playwright.async_api import async_playwright,Page, expect
from utils.util import get_logger
logger = get_logger(__name__)
async def login_to_dashboard():
url = r"https://mytesturl"
credential = keyring.get_credential("webpage", None)
username = credential.username
password = credential.password
async with async_playwright() as playwright:
browser = await playwright.chromium.launch(channel="chrome",headless=False)
context = await browser.new_context()
page = await context.new_page()
await page.goto(url)
await page.locator("[placeholder=\"Username\"]").fill(username)
await page.locator("[placeholder=\"Password\"]").fill(password)
await page.locator("input:has-text(\"Sign In\")").click()
html1 = await page.content()
await page.screenshot(path="o.png")
#logger.info(html1)
url = page.url
logger.info(url)
await context.storage_state(path='login_data.json')
if __name__ == "__main__":
asyncio.run(login_to_dashboard())
我从 python 命令行运行它。我可以看到 chrome 启动,填写了 id/password,然后 chrome 页面立即消失了。我page.screenshot(path="o.png")
捕获了屏幕截图,但这是登录前的屏幕截图,而我的期望是它应该在我登录后捕获屏幕截图await page.locator("input:has-text(\"Sign In\")").click()
。
如果我在这里误解了什么,你能告诉我吗?