javascript - 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!');
});
我想编写一个 if 语句来检查是否env === ''
或使用默认参数。
我怎样才能做到这一点?
解决方案
实现此目的的解决方案是:
1) 创建一个单独的config.js
文件来处理您的自定义命令行选项:
import * as minimist from 'minimist';
const args = minimist(process.argv.slice(2));
// get the options --env=xxx --user=yyy from the command line
export const config = {
env: args.env,
user: args.user,
};
2)在你的测试文件中:
删除fixture
和test
方法之外的任何代码。
导入配置文件并将其注入到 TestController 上下文中
通过 TestController 上下文获取命令 args
import 'testcafe';
import { Selector } from 'testcafe';
import { config } from './config';
fixture('Getting Started')
.beforeEach(async (t) => {
// inject config in the test context
t.ctx.currentConfig = config;
});
test('My first test', async (t) => {
// retrieve cli args from the test context
const currentConfig = t.ctx.currentConfig;
console.log(`env=${currentConfig.env}`);
});
推荐阅读
- python - 计算 24 小时时间以确定开店/关店
- r - 如何减小回归对象的大小适合保存到.rds时大小爆炸的函数
- python - 按组从 agg 函数创建新列
- python - 无法从任何环境而不是基地打开 anaconda 导航器
- r - 如何生成包含所有相关小数位的汇总统计表以显示在 R 的结果表中?
- r - 如何格式化'gtsummary :: tbl_summary'中分类变量的位数?
- php - 我如何在 Doctrine 中的多对多关系上执行 findBy?
- python - 正确终止二进制串行消息以使用 pySerial 读取
- spring-cloud-stream - 有 Spring Cloud Stream Artemis / ActiveMQ binder 吗?
- rust - 如何在结构中存储不同的异步函数?