I have problem with making API request in my playwright project. Here is example of code
const { test, expect, request, chromium } = require('@playwright/test');
test('API request with browser automation', async ({ page }) => {
// Launch the browser
const browser = await chromium.launch();
const context = await browser.newContext();
// Correctly formatted URL with "https"
const apiUrl = '';
console.log('Making API request to:', apiUrl);
// Perform the GET request
const apiResponse = await context.request.get(apiUrl);
// Check the response status
expect(apiResponse.status()).toBe(200);
// Parse and log the response body
const apiResponseBody = await apiResponse.json();
console.log('API Response:', apiResponseBody);
// Validate response data
expect(apiResponseBody).toHaveProperty('userId');
expect(apiResponseBody.userId).toBe(1);
expect(apiResponseBody).toHaveProperty('title', 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit');
expect(apiResponseBody).toHaveProperty('body');
// Optionally interact with the browser
await page.goto('');
const title = await page.title();
console.log('Page Title:', title);
// Clean up: close the page and browser
await page.close();
await browser.close();
});
I see that if I have test in main catalog of project, the test finishes with success, but when file is in subfolder x/y/z/a/apiTest.spec.ts I see error:
TypeError: apiRequestContext.get: Invalid URL',
(also when I add just a string to line const apiResponse = await context.request.get('myUrl');
)
What am I missing/doing wrong?