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

如何在Git bash CLI控制台运行Cypress测试时打印base Url?

在Git Bash CLI中打印Cypress baseUrl的几种方法

方法一:通过测试代码输出到终端

适合需要在测试执行流程中自动打印baseUrl的场景:

  1. 在cypress.config.js中注册终端打印任务:
const { defineConfig } = require("cypress");

module.exports = defineConfig({
  e2e: {
    baseUrl: "https://your-test-site.com", // 你的baseUrl配置
    setupNodeEvents(on, config) {
      // 注册log任务,将内容输出到终端
      on('task', {
        log(message) {
          console.log(message);
          return null;
        }
      });
    },
  },
});
  1. 在测试文件的钩子中调用任务打印baseUrl:
before(() => {
  const baseUrl = Cypress.config('baseUrl');
  cy.task('log', `Cypress baseUrl: ${baseUrl}`);
});

// 你的测试代码
it('示例测试', () => {
  cy.visit('/');
  // ...
});

运行测试命令(如npx cypress run)后,Git Bash控制台会输出对应的baseUrl。

方法二:直接通过Cypress命令行打印

适合快速查看当前配置的baseUrl,或在测试前打印:

  • 单独打印baseUrl:
npx cypress config baseUrl
  • 先打印baseUrl再运行测试:
npx cypress config baseUrl && npx cypress run

方法三:通过环境变量打印(baseUrl由环境变量设置时)

如果baseUrl通过CYPRESS_BASE_URL环境变量传递,直接在Git Bash中打印变量后执行测试:

echo "当前Cypress baseUrl: $CYPRESS_BASE_URL" && npx cypress run

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 08:50:43