Testcafe in azure devops with specific URL

Viewed 513

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.

2 Answers

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": "..."
}
Related