2

I am trying to e2e-testcafe in my azure devops uisng IAAC(Infra as a code) for java-script code. So i have different stages build, test and deploy to different environments. After deploying to test environment (Storage account) I need to e2e in that deployed code. So i am using below steps for that jobs: my azure-pipeline.yml has below

- job: e2e_tests
  pool:
    vmImage: 'Ubuntu 16.04'
  steps:
  - task: NodeTool@0
    inputs:
      # Replace '10.14' with the latest Node.js LTS version
      versionSpec: '10.14'
    displayName: 'Install Node.js'
  - script: npm install
    displayName: 'Install TestCafe'
  - script: npm test
    displayName: 'Run TestCafe Tests'
  - task: PublishTestResults@2
    inputs:
      testResultsFiles: '**/report.xml'
      testResultsFormat: 'JUnit'
---------------------------------------------------
my test.js: 

import { Selector } from 'testcafe';
const articleHeader = Selector('#article-header');
const header = Selector('h1');
fixture `Getting Started`
    .page `localhost:8080`
          
test('My first test', async t => {
    await t
        .expect(header.innerText).eql('Welcome to Your new App');
});

But here its running the tests from my test.js which is part of application and all the tests are running in localserver of the agent which is being used by azure devops for me here its windows server. But now i want to pass the URI to npm test and while it goes to my application its again picking up localhost:8080 and executing in local. So can some one help me running e2e test in the url which i pass which indeed will be the url of storageaccount? like my command should be in azurepipelines.yaml

npm run test --URL

and in my test.js it should pick up above URL i passed in Yaml while running in the pipeline.

Dev25
  • 23
  • 3

2 Answers2

3

You could specify argument to NPM command npm run <command> [-- <args>]. More information: Sending command line arguments to npm script

For TestCafe, it seems that you specify value through Metadata

starian chen-MSFT
  • 33,174
  • 2
  • 29
  • 53
2

Well, this is quite a common question with TestCafe. An easy answer is, there's no direct way, but there're some workarounds:

  1. Use some external module such as minimist, this was already solved on stackoverflow here. The bottom line is that such an external module allows you to parse command line arguments, which is what you are looking for.

  2. Use environment variables that you should be able to set up in Azure DevOps. From the TestCafe's point of view, it's described in documentation here. The way I made this work with various environments is I wrote a little helper function like this:

Helpers/baseUrl.js

import config from '../config';

const baseUrlOf = {
    "dev": config.baseUrlDev,
    "staging": config.baseUrlStaging,
    "prod": config.baseUrlProd
};

export function getBaseUrl () {
    return baseUrlOf[`${process.env.TESTCAFE_ENV}`];
}

this allows me to use the function in fixtures and/or tests:

import { getBaseUrl } from '../Helpers/baseUrl';

fixture `Add User Child`    
    .page(getBaseUrl());   

and I still have the concrete URLs only in config.json:

{
    "baseUrlDev": "...",
    "baseUrlStaging": "...",
    "baseUrlProd": "..."
}
pavelsaman
  • 7,399
  • 1
  • 14
  • 32
  • How do you tell it which env to use? – Daniel Jan 05 '22 at 23:39
  • @Daniel I set environment variable `TESTCAFE_ENV`, see the `getBaseUrl()` method. – pavelsaman Jan 06 '22 at 09:09
  • I see that, but how are you actually setting it. That is to say, on my local machine how do i tell testcafe to use one of the three env's. I see nowhere for passing the env i'm using into the test run. So it will call getBaseUrl() and have no idea what env I'm using. I don't see how you are setting TESTCAFE_ENV. – Daniel Jan 06 '22 at 15:03
  • @Daniel: that's little bit OS-dependent, on Linux, you typically have 3 options: 1) `$ TESTCAFE_ENV=dev testcafe my-test.js`, 2) you can export an environment variable: `$ export TESTCAFE_ENV=dev`, so then you can run only TestCafe: `$ testcafe my-test.js`, 3) creating an `.env` file in the root of the project and using [`dotenv` package](https://www.npmjs.com/package/dotenv). I typically use the last option on a localhost, and the first or second option in a pipeline. On Windows, you can use the `.env` file, or you can use a GUI to set up environment variables. – pavelsaman Jan 06 '22 at 15:10