从 runner.class 启动 TestCafe 时使用环境变量

using environment variables when launching TestCafe from runner.class

我的任务是使用 Testcafe 测试网站的不同语言。我不需要为每种语言编写 n 个测试,而是想要一个需要一个(不断变化的)语言文件的测试文件。

我读了很多关于这不可能的文章,所以我的想法是将有问题的语言打包到一个环境变量中,并将其转换为所需语言对象的路径,就像这样:

// test.spec.js
//
const lang = require(path(__baseDir,"ressources","languages",process.env.LANG))

因为我需要使用跑步者 class 我想 'inject' env.variable 和 "startApp()" 方法,像这样:

//tcRunner.js
//
const createTestCafe = require("testcafe");

const launch = async function (tests, browser) {
  const testcafe = await createTestCafe("localhost", 1337, 1338);
  const runner = await testcafe.createRunner();

  runner
    .startApp("set LANG=en-GB")
    .src("test.spec.js")
    .browsers(browser)
    .reporter("json", "reports/report.json")
    .run()
    .then(failed => {
      console.log(failed + " Tests failed");
      testcafe.close();
    });
};

正如我的帖子所暗示的那样,这是行不通的,我希望您能为我指明方向(甚至是替代方向)以避免为每种语言编写数百个测试。

提前致谢。

您需要在 运行 测试之前设置 [=13th=] 环境变量。

//tcRunner.js
//
const createTestCafe = require("testcafe");

const launch = async function (tests, browser) {
  const testcafe = await createTestCafe("localhost", 1337, 1338);
  const runner = await testcafe.createRunner();

  process.env.LANG = 'en';

  runner
    .src("test.spec.js")
    .browsers(browser)
    .reporter("json", "reports/report.json")
    .run()
    .then(failed => {
      console.log(failed + " Tests failed");
      testcafe.close();
    });
};

除了上面的迷人答案外,我还通过

找到了另一个解决方案

//test.spec.js const lang = require("../lang.js")

并且在转轮函数中

fs.copyFileSync(path.join(__dirname, "lang", lang + ".lang.js"), "lang.js")