typescript - playwright `Thuộc tính 'get' không tồn tại trên kiểu 'APIRequest'`
Vì vậy, đoạn mã sau hoạt động đối với tôi trong 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())
)}
Vì vậy tôi muốn trích xuất cuộc gọi này vào một mô-đun riêng. Tôi đã thử trong một tệp riêng:
import { request } from '@playwright/test';
export async function getCatInfo() {
const response = await request.get(`https://catfact.ninja/fact`)
}
Tôi nhận được lỗi trong VS Code: Thuộc tính 'get' không tồn tại trên kiểu 'APIRequest'
Tôi đang làm gì sai?
Câu trả lời #1
Bạn nên tạo apiRequestContext trước
import { request, APIRequestContext } from '@playwright/test';
test('api request test', async () => {
const apiRequestContext: APIRequestContext = await request.newContext();
});
sau đó sử dụng apiRequestContext
const response = await apiContext.get('https://reqres.in/api/users?page=2');
Câu trả lời #2
Bạn nên nhập từ playwright/test:
import { test, request } from '@playwright/test';
export async function getCatInfo() {
const response = await request.get(`https://catfact.ninja/fact`)
}
