首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Test咖啡馆-测试命令行在测试用例之外的参数

Test咖啡馆-测试命令行在测试用例之外的参数
EN

Stack Overflow用户
提问于 2019-02-14 18:58:19
回答 2查看 2K关注 0票数 6

当我熟悉Test咖啡馆时,我尝试使用命令行参数来向用户提供关于如何运行测试的更多信息。因此,我使用的是minimist包。

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

代码语言:javascript
复制
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 === ''或者使用默认参数。

我怎样才能做到这一点?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-02-15 08:59:32

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

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

代码语言:javascript
复制
'*** A SAMPLE CONSOLE OUTPUT ***'
Getting Started
value
代码语言:javascript
复制
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!');
});
代码语言:javascript
复制
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);
    });
票数 4
EN

Stack Overflow用户

发布于 2019-02-14 21:04:21

实现这一目标的一个解决方案是:

1)创建一个单独的config.js文件来处理自定义命令行选项:

代码语言:javascript
复制
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)在您的测试文件中:

删除fixturetest方法之外的任何代码。

导入配置文件并将其注入TestController上下文中

通过TestController上下文获取命令args

代码语言:javascript
复制
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}`);
});
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54697349

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档