Testcafe - 在测试用例外测试命令行参数
Testcafe - Test command line argument outside test case
随着我越来越熟悉 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 上下文获取命令参数
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}`);
});
However, I cannot print or use any variables outside the test cases.
请使用编程方式运行TestCafe。
我已经更改了您的代码示例 (test.js
) 并以编程方式创建了一个 运行s TestCafe 文件 (run.js
)。
将这些文件放入一个文件夹中,然后在您的终端中执行命令 'node run.js --env value'
。
然后你会看到如下输出:
'*** A SAMPLE CONSOLE OUTPUT ***'
Getting Started
value
test.js
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 ***');
fixture `Getting Started`
.page `http://devexpress.github.io/testcafe/example`;
test('My first test', async t => {
console.log(env); // prints
await t
.typeText('#developer-name', 'John Smith')
.wait(1000)
.click('#submit-button')
.expect(Selector('#article-header').innerText).eql('Thank you, John Smith!');
});
run.js
const createTestCafe = require('testcafe');
let runner = null;
createTestCafe('localhost', 1337, 1338, void 0, true)
.then(testcafe => {
runner = testcafe.createRunner();
})
.then(() => {
return runner
.src('test.js')
.browsers('chrome')
.run()
.then(failedCount => {
console.log(`Finished. Count failed tests:${failedCount}`);
process.exit(failedCount)
});
})
.catch(error => {
console.log(error);
process.exit(1);
});
随着我越来越熟悉 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 上下文获取命令参数
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}`);
});
However, I cannot print or use any variables outside the test cases.
请使用编程方式运行TestCafe。
我已经更改了您的代码示例 (test.js
) 并以编程方式创建了一个 运行s TestCafe 文件 (run.js
)。
将这些文件放入一个文件夹中,然后在您的终端中执行命令 'node run.js --env value'
。
然后你会看到如下输出:
'*** A SAMPLE CONSOLE OUTPUT ***'
Getting Started
value
test.js
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 ***');
fixture `Getting Started`
.page `http://devexpress.github.io/testcafe/example`;
test('My first test', async t => {
console.log(env); // prints
await t
.typeText('#developer-name', 'John Smith')
.wait(1000)
.click('#submit-button')
.expect(Selector('#article-header').innerText).eql('Thank you, John Smith!');
});
run.js
const createTestCafe = require('testcafe');
let runner = null;
createTestCafe('localhost', 1337, 1338, void 0, true)
.then(testcafe => {
runner = testcafe.createRunner();
})
.then(() => {
return runner
.src('test.js')
.browsers('chrome')
.run()
.then(failedCount => {
console.log(`Finished. Count failed tests:${failedCount}`);
process.exit(failedCount)
});
})
.catch(error => {
console.log(error);
process.exit(1);
});