最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

typescript - apiRequestContext.get: Invalid URL error during playwright tests - Stack Overflow

programmeradmin1浏览0评论

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?

发布评论

评论列表(0)

  1. 暂无评论