You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

企业认证代理环境下Playwright API测试连接超时的配置问询

企业代理环境下Playwright API测试的代理配置问题

我跟着API教程尝试在企业代理后调用Web服务,出现如下错误:

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

浏览器访问https://reqres.in/api/users/2正常,执行npm run tests:chrome运行打开playwright.dev的示例测试example.spec.ts也正常。

我的测试文件api.spec.ts代码:

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)
    })
})

api.config.ts代码:

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

package.json代码:

{
  "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"
  }
}

当前Playwright API测试指南未提及需要认证的企业代理环境下的额外配置,请问:在需要认证的企业代理环境下,使用Playwright API测试需进行哪些配置?


解决方案

1. 配置全局代理环境变量

Playwright的request对象会读取系统环境变量中的代理配置,执行测试前可设置以下变量:

  • Windows(命令提示符):
    set HTTP_PROXY=http://username:password@proxy-server:port
    set HTTPS_PROXY=http://username:password@proxy-server:port
    set NO_PROXY=localhost,127.0.0.1,reqres.in
    npm run tests:api
    
  • Linux/macOS(终端):
    export HTTP_PROXY=http://username:password@proxy-server:port
    export HTTPS_PROXY=http://username:password@proxy-server:port
    export NO_PROXY=localhost,127.0.0.1,reqres.in
    npm run tests:api
    

2. 在Playwright配置文件中指定代理

直接在api.config.ts的use字段中添加全局代理配置,支持带认证的代理:

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',
        // 添加代理配置
        proxy: {
            server: 'http://proxy-server:port',
            username: 'your-proxy-username',
            password: 'your-proxy-password',
            // 可选:不需要代理的域名列表
            bypass: 'localhost,127.0.0.1'
        }
    },
    projects: [ { name: 'Chromium', use: { browserName: 'chromium'}, } ]
}

3. 为单个request请求设置代理

如果仅需给特定API请求配置代理,可在request.get方法中传入proxy参数:

test("Simple API 1", async ({request}) => {
    const response = await request.get( "https://reqres.in/api/users/2", {
        proxy: {
            server: 'http://proxy-server:port',
            username: 'your-proxy-username',
            password: 'your-proxy-password'
        }
    })
    expect(response.status()).toBe(200)
})

注意事项

  • 若代理使用HTTPS,server字段需写成https://proxy-server:port
  • 部分企业代理需忽略SSL错误,确保配置中ignoreHTTPSErrors: true已开启
  • 若代理密码包含特殊字符(如@、#),需进行URL编码(例如@编码为%40)

内容的提问来源于stack exchange,提问作者surfmuggle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 08:50:39