我有以下全局设置文件
import { Browser,Page,chromium, expect } from "@playwright/test";
async function globalSetup(){
const browser: Browser = await chromium.launch();
const context = await browser.newContext();
const page: Page = await context.newPage();
await page.goto("https://website/login");
await page.getByPlaceholder('[email protected]').fill('[email protected]');
await page.getByPlaceholder('Password...').fill('John123');
await page.getByRole('button', { name: 'Sign In' }).click();
await expect(page).toHaveURL("https://website/dashboard");
await page.context().storageState({path:"./LoginAuth.json"})
await browser.close();
}
export default globalSetup;
以及以下测试
import { test, expect, Page } from '@playwright/test';
test('Login to Teampulse', async ({ page }) => {
await page.goto(" ");
await expect(page).toHaveURL("https://website/dashboard");
});
基本 url 在配置文件中定义
use: {
baseURL: 'https://website/dashboard',
trace: 'on-first-retry',
storageState:"./LoginAuth.json"
},
直到不久前,测试都会通过,但现在失败并出现以下错误
Expected string: "https://website/dashboard"
Received string: "https://website/login"
请注意,如果我在没有任何登录的情况下直接访问网站的 /dashboard 部分,它将带我进入 /login 页面,因此它的行为几乎就像全局设置部分由于某种原因被忽略一样,但奇怪的是问题是,如果我拼错了全局设置文件中的占位符之一,它将抛出一个错误,指出找不到定位器。
我刚刚检查过,在这种情况下,storageState 文件似乎是空的,这就是为什么没有保存登录信息。这里出了什么问题?
根据要求,这是完整的 config.ts 文件
import { defineConfig, devices } from '@playwright/test';
/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// require('dotenv').config();
/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
// timeout:80000,
// expect: {
// timeout: 20 * 1000,
// },
globalSetup:"./global-setup",
testDir: './tests/',
/* 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:3,
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('/')`. */
// launchOptions:{
// slowMo:2000
// },
baseURL: 'https://website/dashboard/',
//baseURL: 'https://www.google.com/',
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
storageState:"./LoginAuth.json"
},
/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
// {
// name: 'firefox',
// use: { ...devices['Desktop Firefox'] },
// },
// {
// 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 start',
// url: 'http://127.0.0.1:3000',
// reuseExistingServer: !process.env.CI,
// },
});