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

如何让TestCafe访问localhost而非本地机器IP地址?

TestCafe 强制使用localhost访问本地服务的配置方法

核心解决方案

TestCafe默认会自动解析本地网卡的IP作为服务访问地址,你可以通过启动参数强制指定绑定的hostname为localhost即可解决该问题。

命令行启动场景

直接在启动命令中添加--hostname localhost参数即可,示例命令如下:

testcafe chrome test.js --hostname localhost

编程API调用场景

在创建TestCafe实例或者运行测试的配置中指定hostname参数,示例代码如下:

const createTestCafe = require('testcafe');
let testcafe = null;
createTestCafe('localhost', 1337, 1338)
  .then(tc => {
    testcafe = tc;
    const runner = testcafe.createRunner();
    return runner
      .src(['tests/fixture1.js'])
      .browsers(['chrome'])
      .run();
  })
  .then(failedCount => {
    console.log('Tests failed: ' + failedCount);
    testcafe.close();
  });

配置文件场景

如果你使用.testcaferc.json作为配置文件,直接在配置中添加hostname字段即可:

{
  "hostname": "localhost"
}

补充说明

配置完成后TestCafe启动的所有浏览器都会通过localhost访问本地测试服务,不会再使用网卡IP,即可绕过防火墙对本地IP访问的限制。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 14:54:02