这是我的登录设置:
import {test as setup} from '@playwright/test'
const authFile = '.auth/user.json'
setup('authentication', async ({page}) => {
await page.goto('https://justexample.ua/');
await page.getByRole('textbox', {name: 'Username'}).fill('Admin');
await page.getByRole('textbox', {name: 'Password'}).fill('Admin');
await page.locator('#kc-login').filter({hasText: 'Sign In'}).click();
await page.waitForResponse('https://api.justexample.ua/asset/filter-all-data');
await page.context().storageState({path: authFile})
})
这是我重命名集合的测试:
import { test, expect, request } from "@playwright/test";
test.beforeEach(async ({ page, request, context }) => {
await page.goto("https://justexample.ua/");
await expect(
page.getByText("From newest to oldest", {
exact: true,
})
).toBeVisible();
});
test("Rename collection", async ({ request }) => {
await request.patch(
"https://api.justexample.ua/collection/0856d250-79b7-496d-b0b9-c8163b818816",
{
data: { name: "Test name", description: "test description" },
}
);
});
使用authFile的配置文件:
projects: [ {name: 'setup', testMatch: '/globalFiles/auth.setup.ts'},
{ name: 'chromium for API', use: { ...devices['Desktop Chrome'],
storageState: '.auth/user.json', }, dependencies: ['setup'], },]
我的请求在身份验证后不起作用。 Playwright 刚刚登录成功,然后什么都没有发生...只显示主页。我尝试了不同的端点(发布、获取),但登录后总是没有任何反应。剧作家总是显示测试已通过。 同样在邮递员中一切正常。
您必须将存储状态分配给请求上下文。您将其分配给页面上下文。
const apiContext = await request.newContext({
storageState: 'auth.json', // Load the saved auth state
});
const response = await apiContext.patch(
"https://api.justexample.ua/collection/0856d250-79b7-496d-b0b9-c8163b818816",
{
data: { name: "Test name", description: "test description" },
}
);