Search code examples
javascripttestingautomationenvironment-variablestestcafe

Accessing enviroment variable in testcafe


I am having trouble accessing environment variable that I have set permanently in windows using setx. I have set a path to the download folder that I would access in my test code.

C:\Users\XXXX>echo %DOWNLOADS%
    C:/Users/XXXX/Downloads/

In my test code -

import { Selector, fixture } from 'testcafe';
fixture`Auto Test`
  .page`http://XXXXXX/index.html`.beforeEach(async (browser) => {
  let directory;
  if (browser.browser.os.name === 'Windows') {
        directory = env.DOWNLOADS;

I see following error -
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL. Received undefined

What am I missing here?


Solution

  • you can acess the environment variable like

    process.env.DEV_MODE
    

    DEV_MODE is my environment variable

    sample code

      if (process.env.DEV_MODE) {
        baseUrl = 'http://localhost:5000';
      } else {
        baseUrl = config.API.BaseUrl;
      }