最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Testcafe - Test command line argument outside test case - Stack Overflow

programmeradmin3浏览0评论

As I'm getting familiar with Testcafe, I'm trying to use a mand line argument to give the user more information on how to run tests. For that reason, I'm using the minimist package.

However, I cannot print or use any variables outside the test cases. Please find below my code.

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 ``;

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!');
});

I want to write an if statement that checks if env === '' or use a default argument.

How can I acplish this?

As I'm getting familiar with Testcafe, I'm trying to use a mand line argument to give the user more information on how to run tests. For that reason, I'm using the minimist package.

However, I cannot print or use any variables outside the test cases. Please find below my code.

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!');
});

I want to write an if statement that checks if env === '' or use a default argument.

How can I acplish this?

Share Improve this question edited Apr 29, 2021 at 9:25 Alex Skorkin 4,2743 gold badges27 silver badges48 bronze badges asked Feb 14, 2019 at 18:58 user11063797user11063797
Add a ment  | 

2 Answers 2

Reset to default 4

However, I cannot print or use any variables outside the test cases.  

Please use a programming way to run TestCafe. I've changed you code example (test.js) and created a file that runs TestCafe programmatically (run.js). Put these files into a folder and perform mand 'node run.js --env value' in your terminal. Then you will see the following output:

'*** 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);
    });

A solution to acplish this is:

1) Create a separate config.js file that will handle your custom mand-line options:

import * as minimist from 'minimist';

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

// get the options --env=xxx --user=yyy from the mand line
export const config = {
  env: args.env,
  user: args.user,
};

2) In you test file:

remove any code outside the fixture and the test methods.

import the config file and inject it in the TestController context

get the mand args via the TestController context

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}`);
});
发布评论

评论列表(0)

  1. 暂无评论