proxyplaywrighthttp-proxyconnection-timeout

Playwright - API testing - connection timeout error - possible reason behind corporate proxy


I tried to use this api tutorial to call a webservice from behind our proxy.

But i get

Error: apiRequestContext.get: Connection timeout
Call log:
  - → GET https://reqres.in/api/users/2
  -   user-agent: Playwright/1.40.1 (x64; windows 10.0) node/18.7
  -   accept: */*
  -   accept-encoding: gzip,deflate,br

If i open reqres.in/api/users/2 in a browser it works as expected. If i run npm run tests:chrome the demo test example.spec.ts that opens playwright.dev works as well.

My test api.spec.ts has these lines

import {test, expect} from '@playwright/test'
import { assert } from 'console'
import exp from 'constants'

test.describe.parallel("Api testing", () => {
    const baseURL = "https://reqres.in/api"
    test("Simple API 1", async ({request}) => {
        const response = await request.get( "https://reqres.in/api/users/2")
        // const response = await request.get( `${baseURL}/users/2`)
        expect(response.status()).toBe(200)
    })
})

And my api.config.ts has these lines

import { PlaywrightTestConfig } from '@playwright/test';
import exp from 'constants';
import { userInfo } from 'os';

const config: PlaywrightTestConfig = {
    timeout: 60000,
    retries: 0,
    testDir: 'tests/api',
    use: {
        headless: false, viewport: {width: 1280, height: 720},
        actionTimeout: 10000, ignoreHTTPSErrors: true, video: 'off', screenshot: 'off'
    },
    projects: [ { name: 'Chromium', use: { browserName: 'chromium'}, } ]
}

export default config

And my package.json has these lines

{
  "name": "api-24-01", "version": "1.0.0",
  "description": "Automated Software testing with Playwright",
  "main": "index.js",
  "scripts": {
    "tests:chrome" : "playwright test --config=playwright.config.ts --project=Chromium",
    "tests:api" : "playwright test --config=api.config.ts --project=Chromium"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@playwright/test": "^1.40.1",
    "@types/node": "^20.10.8"
  }
}

The current guide on api testing has no explanation if any additional actions or configurations must be done behind a corporate proxy that requires authentication.

Question

What do i have to do / to configure to use Playwright API testing behind a corporate proxy that requires authentication?


Solution

  • The playwright team added a section for How to use a proxy for api testing to the API testing guide.

    To use with the config file playwright.config.ts must have a configuration for your proxy : {} like in this example

    import { PlaywrightTestConfig } from '@playwright/test';
    import exp from 'constants';
    import { userInfo } from 'os';
    
    const config: PlaywrightTestConfig = {
        timeout: 60000,
        retries: 0,
        testDir: 'tests/api',
        use: {
            headless: true,
            viewport: {width: 1280, height: 720},
            actionTimeout: 10000,
            ignoreHTTPSErrors: true,
            video: 'off',
            screenshot: 'off',
            proxy: {
                server: 'http://<your-proxy-server>:<port>',
                username: '<your-user>',
                password: '<your-pass>'
            },
        },
        projects: [
            {
                name: 'Chromium',
                use: { browserName: 'chromium'},
            }
        ]
    }
    
    export default config
    

    This solved the connection timeout problem.