So the following code works for me in test.spec.ts:
//@ts-check
import { test, request } from '@playwright/test'
test('api test - to see if Playwright API calls are working', async ({ request }) => {
const response = await request.get(`https://catfact.ninja/fact`);
const statusCode = response.status()
const responseBody = JSON.parse(await response.text())
)}
So I want to extract this call in a separate module. I tried in separate file:
import { request } from '@playwright/test';
export async function getCatInfo() {
const response = await request.get(`https://catfact.ninja/fact`)
}
I get an Error in VS Code: Property 'get' does not exist on type 'APIRequest'
What am I doing wrong?
You should create apiRequestContext
first
import { request, APIRequestContext } from '@playwright/test';
test('api request test', async () => {
const apiRequestContext: APIRequestContext = await request.newContext();
});
then use apiRequestContext
const response = await apiContext.get('https://reqres.in/api/users?page=2');