如何使用 PlayWright 执行 POST 请求

问题描述 投票:0回答:3

我已经被这个问题困扰了一段时间了。我需要测试一个网站,并且需要发布信息以测试它是否出现在页面上。

到目前为止我所拥有的是这个

    (async () => {
        const browser = await webkit.launch();
        const page = await browser.newPage();
        await page.route('http://100.100.100.100/', route => route.fulfill({
            status: 200,
            body: body,
        }));
        await page.goto('https://theurlofmywebsite/');
        await page.click('button')
        await page.click('text=Login with LoadTest')
        await page.fill('#Username','username')
        await page.fill('#Password','password')
        await page.click('#loginButton')
        // await page.waitForSelector('text=Dropdown');
        await page.click('css=span >> text=Test')
        await page.click('#root > div > div > header > ul.nav.navbar-nav.area-tabs > li:nth-child(6) > a','Test')
        await page.waitForSelector('text=Detail')
        await page.screenshot({ path: `example3.png` })
        await browser.close();
    })();
    
    const body = [ my json post request ]
post request playwright
3个回答
3
投票
jest.setTimeout(1000000);
let browser: any;
let page: any;
beforeAll(async () => {
    browser = await chromium.launch();
});
afterAll(async () => {
    await browser.close();
});
beforeEach(async () => {
    page = await browser.newPage();
});
afterEach(async () => {
    await page.close();
});



it("should work", async () => {
    await fetch("http://YOUAWESOMEURL", {
        method: "post",
        body: JSON.stringify(body),
    })
        .then((response) => console.log(response))
        .catch((error) => console.log(error));
    await page.goto("https://YOUAWESOMEURL");
    await page.click("button");
    await page.click("text=Login");
    await page.fill("#Username", "YOURUSERNAME");
    await page.fill("#Password", "YOURPASSWORD");
    await page.click("#loginButton");
    // await page.click("css=span >> text=Load Test");
    await page.click(
        "#root > div > div > header > ul.nav.navbar-nav.area-tabs > li:nth-child(6) > a >> text=Test"
    );
    await page.waitForSelector("text=SOMETEXTYOUWANTTOCHECKIFTHERE");
    // await page.waitForSelector(`text=SOMEOTHERTEXTYOUWANTTOCHECKIFTHERE`);
    // Another way to check for success
    // await expect(page).toHaveText(`SOMEOTHERTEXTYOUWANTTOCHECKIFTHERE`);
    console.log("test was successful!");
});


2
投票

1.19 版本看起来很简单。

test('get response variable form POST in Playwright', async ({ request }) => {
  const response = await request.post(`/repos/${USER}/${REPO}/issues`, {
    data: {
      title: '[Bug] report 1',
      body: 'Bug description',
    }
  });
  expect(response.ok()).toBeTruthy();
}

查看更多信息https://playwright.dev/docs/test-api-testing


1
投票
import { expect, request } from '@playwright/test';

const baseApiUrl = "https://api.xxx.pro/node-api/graphql";

test('API Search', async ({ request }) => {

        const search_query = `query  {me {  id username}}   `;

        const response = await request.post(baseApiUrl, {
            data: {
                query: search_query
            },
            headers: {
                authorization: `Bearer eyJhbGciOiJIUzcCI6IkpXVCJ9.eyJzd`
            }
        });

        const bodyResponse = (await response.body()).toString();
        expect(response.ok(), `${JSON.stringify(bodyResponse)}`).toBeTruthy();
        expect(response.status()).toBe(200);

        const textResponse = JSON.stringify(bodyResponse);
        expect(textResponse, textResponse).not.toContain('errors');
    });
© www.soinside.com 2019 - 2024. All rights reserved.