Hi, I have a test where I need the result from a curl request like: curl 'localhost/path/to/endpoint' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'DNT: 1' -H 'Origin: localhost' -H 'X-API-Key: 12345' --data-binary '{"datainfo"}' --compressed
Is this possible to do in playwright?
something like:
import { test, expect,request } from '@playwright/test';
test.beforeEach(async ({ page }) => { await page.goto(HOMEPAGE_OPEN); });
test('test case description', async ({ page }) => { //Here the curl request action
...
// Here the interaction with the curl result }
This thread is trying to answer question "How to make a curl request with Playwright?"
Something like this probably
const command = `your curl here`
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error executing cURL command: ${error}`)
return
}
if (stderr) {
console.error(`cURL command returned an error: ${stderr}`)
return
}
console.log(`cURL command executed successfully. Response: ${stdout}`)
})
@passe_bln try this test('test case description', async ({ page }) => { const curlCommand =
curl 'localhost/path/to/endpoint' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'DNT: 1' -H 'Origin: localhost' -H 'X-API-Key: 12345' --data-binary '{"datainfo"}' --compressed`;
const result = await page.evaluate(async (curlCommand) => { const response = await fetch(curlCommand, { compress: true }); const data = await response.json(); return data; }, curlCommand); expect(result).toEqual(/* expected value */); });`
Yup request fixture: https://playwright.dev/docs/api/class-apirequestcontext#api-request-context-get
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].