pytest+Playwright-web自动化实践:02-脚本录制

目录

1、控制台输入

 2、效果

3、 演示:登录163邮箱


1、控制台输入

>playwright codegen+url

注:url可选

pytest+Playwright-web自动化实践:02-脚本录制_第1张图片

 2、效果

pytest+Playwright-web自动化实践:02-脚本录制_第2张图片

 左侧为录制浏览器,右侧为录制后的代码

备注:

生成代码可根据实际情况选择。

pytest+Playwright-web自动化实践:02-脚本录制_第3张图片

3、 演示:登录uniportal.huawei.com/

  • 1、输入账号
  • 2、输入密码
  • 3、点击登录

pytest+Playwright-web自动化实践:02-脚本录制_第4张图片

 生成脚本-pytest

from playwright.sync_api import Page, expect


def test_example(page: Page) -> None:
    page.goto("https://uniportal.huawei.com/uniportal1/login-pc.html")
    page.get_by_placeholder("帐号名/邮箱/手机号/W3帐号").click()
    page.get_by_placeholder("帐号名/邮箱/手机号/W3帐号").fill("手机号")
    page.get_by_placeholder("密码").click()
    page.get_by_placeholder("密码").fill("密码")
    page.locator("#loginForm").get_by_role("img").click()
    page.get_by_role("button", name="登录").click()

 生成脚本-async

import asyncio

from playwright.async_api import Playwright, async_playwright, expect


async def run(playwright: Playwright) -> None:
    #实例化浏览器--谷歌--有头模式
    browser = await playwright.chromium.launch(headless=False)
    context = await browser.new_context()
    page = await context.new_page()
    await page.goto("https://uniportal.huawei.com/uniportal1/login-pc.html")
    await page.get_by_placeholder("帐号名/邮箱/手机号/W3帐号").click()
    await page.get_by_placeholder("帐号名/邮箱/手机号/W3帐号").fill("手机号")
    await page.get_by_placeholder("密码").click()
    await page.get_by_placeholder("密码").fill("密码")
    await page.locator("#loginForm").get_by_role("img").click()
    await page.get_by_role("button", name="登录").click()

    # ---------------------
    await context.close()
    await browser.close()


async def main() -> None:
    async with async_playwright() as playwright:
        await run(playwright)


asyncio.run(main())

你可能感兴趣的:(pytest,自动化,运维)