我有这个 e2e 测试:
import { expect, test } from "@playwright/test"
test.describe("ChooseApp Page", () => {
test("should display correct content and navigate to app pages", async ({ page }) => {
await page.goto("./6XDCIRI/choose-app")
// Check if the main title is present
await expect(page.locator("text=Wählen Sie Ihre App")).toBeVisible()
// Check if Solar Analytics Card is present
const solarAnalyticsCard = page.getByTestId("choose-app-0")
await expect(solarAnalyticsCard).toBeVisible()
// Test navigation for Solar Analytics
await solarAnalyticsCard.click()
await page.waitForURL("./6XDCIRI/solar-analytics/electricity-requirements")
})
})
在本地,Chromium 和 Mozilla 已通过,但在 Azure CI/CD 中,仅 Mozilla 失败(超时):
/* eslint-disable lines-around-comment */
/* eslint-disable capitalized-comments */
import { defineConfig, devices } from "@playwright/test"
import dotenv from "dotenv"
import path from "path"
/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
const envFile = process.env.NODE_ENV === "production" ? ".env.production" : ".env.testing"
dotenv.config({ path: path.resolve(__dirname, envFile) })
/**
* See https://playwright.dev/docs/test-configuration.
*/
// Use process.env.PORT by default and fallback to port 3000
const PORT = process.env.PORT || 3000
// Set webServer.url and use.baseURL with the location of the WebServer respecting the correct set port
const baseURL = `http://localhost:${PORT}`
export default defineConfig({
timeout: 180000,
testDir: path.join(__dirname, "e2e"),
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: "html",
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
baseURL,
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: "on-first-retry",
},
/* Configure projects for major browsers */
projects: [
{ name: "setup", testMatch: /.*\.setup\.ts/ },
{
name: "chromium",
use: {
...devices["Desktop Chrome"],
storageState: "playwright/.auth/user.json",
},
dependencies: ["setup"],
},
{
name: "firefox",
use: { ...devices["Desktop Firefox"], storageState: "playwright/.auth/user.json" },
dependencies: ["setup"],
},
// {
// name: "webkit",
// use: { ...devices["Desktop Safari"] },
// },
/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: { ...devices['Pixel 5'] },
// },
// {
// name: 'Mobile Safari',
// use: { ...devices['iPhone 12'] },
// },
/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
// },
// {
// name: 'Google Chrome',
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
// },
],
/* Run your local dev server before starting the tests */
webServer: {
command: "npm run dev",
url: baseURL,
reuseExistingServer: !process.env.CI,
},
})
如果不查看跟踪就很难说,但这可能是因为您将
.waitForURL()
与相对路径一起使用。我建议使用 expect
作为导航断言而不是 .waitForURL()
,例如:
test.describe('ChooseApp Page', () => {
test('should display correct content and navigate to app pages',
async ({page}) => {
await page.goto('./6XDCIRI/choose-app');
// Check if the main title is present
await expect(page.locator('text=Wählen Sie Ihre App'))
.toBeVisible();
// Check if Solar Analytics Card is present
const solarAnalyticsCard = page.getByTestId('choose-app-0');
await expect(solarAnalyticsCard)
.toBeVisible();
// Test navigation for Solar Analytics
await solarAnalyticsCard.click();
const relativePath = /\/6XDCIRI\/solar-analytics\/electricity-requirements/;
await expect(async () => {
expect(page.url())
.toMatch(relativePath);
})
.toPass({timeout: 10000, intervals: [100]});
});
});
await expect(async()=>{}).toPass()
是重复测试某些代码块的一种非常好的方法。 只需确保回调中的断言是否是异步的,它们的超时时间比您的 expect().toPass()
超时时间短,否则实际上不会重试。