user10251019
user10251019

Reputation:

Setting Environment Variables in TestCafe giving undefined value

I am learning TestCafe and I am new to it.

I am trying to follow as it is mentioned here: https://devexpress.github.io/testcafe/documentation/recipes/access-environment-variables-in-tests.html

I have a code that runs on my windows machine!!

fixture`Getting Started`.page`http://devexpress.github.io/testcafe/example`;

test("My First Test", async t => {
    await t
    .typeText('#developer-name', 'John Smith')
    .click('#submit-button')
    .takeScreenshot();

    const articleHeader = await Selector('.result-content').find('h1');
    
    // Obtain the text of the article header
    let headerText = await articleHeader.innerText;
    console.log(process.env.DEV_MODE);
});

My Scripts Section is like this

"scripts": {
    "setnode": "set DEV_MODE=staging",
    "test:chrome": "npm run setnode & npx testcafe \"chrome --start-fullscreen\" e2e/tests"
  }

when I am running this command npm run test:chrome

I am getting output like this

[email protected] setnode C:\TestCafeProjects\ui-testcafe-poc set DEV_MODE=staging

 Running tests in:
 - Chrome 79.0.3945.130 / Windows 10

Getting Started  

undefined   

√ My First Test

why the console.log is writing as undefined instead of staging?

Upvotes: 2

Views: 2003

Answers (1)

Arseniy Rubtsov
Arseniy Rubtsov

Reputation: 880

The ampersand symbol (&) in your script runs them in parallel. However, you need to run them sequentially by using the && operand.

Also, npm scripts spawn the shell process under the hood (cmd on Windows) and the variable set by the set command lives only in its own session. The following script should work:

"scripts": {        
    "test:chrome": "set DEV_MODE=staging && npx testcafe \"chrome --start-fullscreen\" e2e/tests"
}

Upvotes: 6

Related Questions