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

如何在TestCafe中禁用HTTPS强制重定向?

TestCafe强制跳转HTTPS导致HTTP站点连接失败的解决方案

问题描述

使用TestCafe执行测试时,明明在代码中指定了HTTP地址,TestCafe却强制将请求重定向到HTTPS,而我的开发环境站点仅支持HTTP协议,导致连接被拒绝。尝试设置baseUrl属性后问题仍未解决。

测试代码

import { RequestMock } from 'testcafe';

fixture `Simple Test`
    .page `about:blank`;

test('Navigate to page', async t => {
    await t.wait(2000);
    await t.navigateTo("http://dop.ds73.dev.gelicon.biz/");
    await t.wait(2000);
});

报错信息

Running tests in:
 - Chrome 130.0.0.0 / Windows 10

 Simple Test
 × Navigate to page

   1) Failed to load the page at "https://dop.ds73.dev.gelicon.biz/".
      Increase the value of the "pageRequestTimeout" variable, enable the "retryTestPages" option, or use quarantine mode to perform additional attempts to execute this test.
      You can find troubleshooting information for this issue at "https://go.devexpress.com/TestCafe_FAQ_ARequestHasFailed.aspx".

      Error details:
      Error: ConnectionRefused

      Browser: Chrome 130.0.0.0 / Windows 10

          5 |
          6 |test('Navigate to page', async t => {
          7 |
          8 |    await t.wait(2000);
          9 |
       > 10 |    await t.navigateTo("http://dop.ds73.dev.gelicon.biz/");
         11 |
         12 |    await t.wait(2000);
         13 |
         14 |});

         at <anonymous> (C:\projects\testcafe\testcafedemo\tests\test.js:10:13)
         at asyncGeneratorStep (C:\projects\testcafe\testcafedemo\tests\test.js:1:40)
         at _next (C:\projects\testcafe\testcafedemo\tests\test.js:1:40)

解决方法

1. 禁用浏览器HTTPS自动跳转

Chrome等浏览器会自动将部分HTTP域名重定向到HTTPS,可通过添加启动参数禁用该行为:

testcafe "chrome --unsafely-treat-insecure-origin-as-secure=http://dop.ds73.dev.gelicon.biz/ --disable-web-security" tests/test.js

该参数将目标HTTP域名标记为安全来源,避免强制跳转HTTPS。

2. 检查站点HSTS设置

若目标站点响应头包含Strict-Transport-Security(HSTS),浏览器会强制使用HTTPS。通过浏览器开发者工具查看响应头,确认后在开发环境移除该设置即可。

3. 用RequestHook拦截重定向

利用TestCafe的RequestHook拦截HTTPS请求,替换回HTTP地址:

import { RequestHook } from 'testcafe';

class RedirectInterceptor extends RequestHook {
    constructor() {
        super();
    }

    onRequest(event) {
        if (event.request.url.startsWith('https://dop.ds73.dev.gelicon.biz/')) {
            event.request.url = event.request.url.replace('https://', 'http://');
        }
    }

    onResponse(event) {}
}

const redirectInterceptor = new RedirectInterceptor();

fixture `Simple Test`
    .page `about:blank`
    .requestHooks(redirectInterceptor);

test('Navigate to page', async t => {
    await t.navigateTo("http://dop.ds73.dev.gelicon.biz/");
});

4. 验证TestCafe配置

检查.testcaferc.json配置文件,确保baseUrl设置为正确的HTTP地址:

{
    "baseUrl": "http://dop.ds73.dev.gelicon.biz/"
}

内容的提问来源于stack exchange,提问作者Дмитрий Сорвачев

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 15:58:16