没有必要在测试中创建浏览器。使用预先构建的
page
。如果你想跑得很快,使用
npx playwright test --headed
或者在配置中设置。
不要在你的
await
调用,并避免XPath。
import {expect, test} from "@playwright/test"
test("login test demo", async ({page}) => {
await page.goto("https://ecommerce-playground.lambdatest.io");
await page.getByRole("link", {name: "My account"})
.evaluate(el => el.click());
await expect(page).toHaveURL(/\/index\.php\?route=account\/login$/);
await page.getByLabel("E-Mail Address").fill("user");
await page.getByLabel("Password").fill("1234");
await page.keyboard.press("Tab");
await page.keyboard.press("Tab");
await page.keyboard.press("Enter");
await expect(page.getByText("Warning:")).toBeVisible();
});
在实际测试中,您可能希望避免不受信任的点击,但由于可见性的原因,受信任的单击在这里不起作用,因此我们不得不后退。