Testcafe - Test command line argument outside test case

前端 未结 2 1008
你的背包
你的背包 2021-01-18 06:58

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

2条回答
  •  别那么骄傲
    2021-01-18 07:36

    A solution to accomplish this is:

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

    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) 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 command 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}`);
    });
    

提交回复
热议问题