1,playwright支持使用node.js、python、java方式进行使用
下面是playwright网站
Installation | Playwrighthttps://playwright.dev/docs/intro#introduction
2,在python里面使用的话是比较方便的
pip install pytest-playwright
playwright install
使用上面的命令就可以配置好python使用playwright了
import re
from playwright.sync_api import Page, expect
def test_has_title(page: Page):
page.goto("https://playwright.dev/")
# Expect a title "to contain" a substring.
expect(page).to_have_title(re.compile("Playwright"))
def test_get_started_link(page: Page):
page.goto("https://playwright.dev/")
# Click the get started link.
page.get_by_role("link", name="Get started").click()
# Expects page to have a heading with the name of Installation.
expect(page.get_by_role("heading", name="Installation")).to_be_visible()
这是一个demo可以使用
3,使用node可以使用命令一键生成项目
npm init playwright@latest
yarn create playwright
pnpm create playwright
这是关于nodejs创建playwright项目的命令
在生成的项目内的路径C:\myfile\Files\demo-file\tests(这里是以我的路径为展示)
import { test, expect } from '@playwright/test';
// 设置测试超时时间为 120 秒
test.beforeEach(async () => {
test.setTimeout(120000);
})
test('has title', async ({ page }) => {
await page.goto('https://playwright.dev/',{waitUntil:'domcontentloaded'});
// Expect a title "to contain" a substring.
await expect(page).toHaveTitle(/Playwright/);
});
test('get started link', async ({ page }) => {
await page.goto('https://playwright.dev/',{waitUntil:'domcontentloaded'});
// Click the get started link.
await page.getByRole('link', { name: 'Get started' }).click();
// Expects page to have a heading with the name of "Get started".
await expect(page.getByRole('heading', { name: 'Get started' })).toBeVisible();
});
按照这个可以解决上面has title的超时报错问题
不过目前还有出现
expect(page.getByRole('heading', { name: 'Get started' })).toBeVisible();
默认调用出现超时5秒的情况,在后面给配置timeout就会对应显示报错时长