Testcafe - 在测试用例外测试命令行参数

5 javascript automated-tests e2e-testing testcafe

当我熟悉Testcafe时,我正在尝试使用命令行参数为用户提供有关如何运行测试的更多信息.出于这个原因,我正在使用该minimist包.

但是,我不能打印或使用测试用例之外的任何变量.请在下面找到我的代码.

import { Selector } from 'testcafe';
import minimist from 'minimist';

const args = minimist(process.argv.slice(2));
const env = args.env;

console.log('*** A SAMPLE CONSOLE OUTPUT ***'); // does not print

fixture `Getting Started`
  .page `http://devexpress.github.io/testcafe/example`;

test('My first test', async t => {
  console.log('*** ANOTHER SAMPLE CONSOLE OUTPUT ***'); // prints
  await t
    .typeText('#developer-name', 'John Smith')
    .wait(1000)
    .click('#submit-button')

    // Use the assertion to check if the actual header text is equal to the expected one
    .expect(Selector('#article-header').innerText).eql('Thank you, John Smith!');
});
Run Code Online (Sandbox Code Playgroud)

我想编写一个if语句来检查是否env === ''使用默认参数.

我怎么能做到这一点?

mlo*_*sev 4

但是,我不能打印或使用测试用例之外的任何变量.  

请使用编程方式运行TestCafe.我已经改变了你的代码example(test.js)并创建了一个以编程方式运行TestCafe的文件(run.js).将这些文件放入文件夹并\'node run.js --env value\'在终端中执行命令.然后你会看到以下输出:

\'*** A SAMPLE CONSOLE OUTPUT ***\'\nGetting Started\nvalue\n
Run Code Online (Sandbox Code Playgroud)
test.js\n\nimport { Selector } from \'testcafe\';\nimport minimist from \'minimist\';\n\nconst args = minimist(process.argv.slice(2));\nconst env = args.env;\n\nconsole.log(\'*** A SAMPLE CONSOLE OUTPUT ***\'); \n\nfixture `Getting Started`\n  .page `http://devexpress.github.io/testcafe/example`;\n\ntest(\'My first test\', async t => {\n  console.log(env); // prints\n  await t\n    .typeText(\'#developer-name\', \'John Smith\')\n    .wait(1000)\n    .click(\'#submit-button\')\n    .expect(Selector(\'#article-header\').innerText).eql(\'Thank you, John Smith!\');\n});\n
Run Code Online (Sandbox Code Playgroud)
run.js\nconst createTestCafe = require(\'testcafe\');\nlet runner           = null;\n\ncreateTestCafe(\'localhost\', 1337, 1338, void 0, true)\n    .then(testcafe => {\n        runner = testcafe.createRunner();\n    })\n    .then(() => {\n         return runner\n            .src(\'test.js\')\n            .browsers(\'chrome\')\n            .run()\n            .then(failedCount => {\n                console.log(`Finished. Count failed tests:${failedCount}`);\n                process.exit(failedCount)\n            });\n    })\n    .catch(error => {\n        console.log(error);\n        process.exit(1);\n    });\n
Run Code Online (Sandbox Code Playgroud)