Home > Blockchain >  Playwright API tests time out and fail over corporate proxy
Playwright API tests time out and fail over corporate proxy

Time:10-01

My Playwright UI tests (which use page, not request) work fine. Only the API tests are failing. For example const response = await request.get('https://reqres.in/api/users/3')

I found that it must be the corporate proxy by leaving the corporate wifi, joining my phone's hotspot, and seeing the tests pass. I've played with VSCode proxy settings but have not found one that fixes the timeout. It also fails when I run the test script directly from the root directory with npx playwright test --config=api.config.ts so I don't think it's VSCode.

The error message (containing my code) is below, the problem comes from the get method. The script times out there, and the code after it is never attempted. All API methods, such as get, post delete time out similarly.

  1) [chromium] › api.spec.ts:6:10 › API Testing › Simple API Test - Assert Response Status ========

apiRequestContext.get: read ETIMEDOUT
=========================== logs ===========================
→ GET https://reqres.in/api/users/3
  user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.5195.19 Safari/537.36
  accept: */*
  accept-encoding: gzip,deflate,br
============================================================

   5 |
   6 |     test.only('Simple API Test - Assert Response Status', async ({ request }) => {
>  7 |         const response = await request.get('https://reqres.in/api/users/3')
     |                                        ^
   8 |         console.log(response)
   9 |         //expect(response.status()).toBe(200)
  10 |

    at /Users/USERNAME/playwright-framework/tests/api/api.spec.ts:7:40

CodePudding user response:

Solved. The proxy is set within the Playwright config file, along with the rest of the more common configuration items. If required, username and password can be set here too.

For example:

    use: {
    headless: false,
    viewport: { width: 1280, height: 720 },
    actionTimeout: 10000,
    screenshot: 'only-on-failure',
    ignoreHTTPSErrors: true,
    proxy: { server: 'http://yourproxy.com:8080/' },
    trace: 'off',
    video: 'off'

},
  • Related