0

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?

2
  • Your code looks good and i have tried on my machine it works perfectly fine. May be your company does not allow to use it. You can use below API for your practice. It has authorization as bearer token which need to add in your config file. You can get Authorization token by signing up to application. gorest.co.in Commented Jan 11, 2024 at 4:43
  • Hello Ketan i believe that the access to the URI is not blocked since i can access it with the browser. What is probably missing is to pass proxy authentication credentials from playwright to the proxy that every reqeust and response is routed over. Commented Jan 11, 2024 at 20:25

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.