具有特定 URL 的 Azure Devops 中的 Testcafe

Testcafe in azure devops with specific URL

我正在尝试在我的 azure devops uisng IAAC(Infra 作为代码)中为 java 脚本代码进行 e2e-testcafe。所以我有不同的阶段构建、测试和部署到不同的环境。部署到测试环境(存储帐户)后,我需要在部署的代码中使用 e2e。所以我正在使用以下步骤 职位: 我的 azure-pipeline.yml 有以下

- 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');
});

但这里的 运行 来自我的 test.js 的测试是应用程序的一部分,所有测试都在 运行 代理的本地服务器中,azure devops 正在使用它我这里是 windows 服务器。但现在我想将 URI 传递给 npm test,当它进入我的应用程序时,它再次获取 localhost:8080 并在本地执行。那么有人可以帮助我在 url 中进行 运行 e2e 测试吗?我通过的确实是 st​​orageaccount 的 url?就像我的命令应该在 azurepipelines.yaml

npm run test --URL

在我的 test.js 中,它应该在 URL 之上,我在 Yaml 中传递,而 运行 在管道中。

好吧,这是 TestCafe 的一个常见问题。一个简单的答案是,没有直接的方法,但有一些解决方法:

  1. 使用一些外部模块,例如 minimist,这已经在 Whosebug here 上解决了。最重要的是,这样的外部模块允许您解析命令行参数,这正是您正在寻找的。

  2. 使用你应该能够在 Azure DevOps 中设置的环境变量。从 TestCafe 的角度来看,它在文档 here 中进行了描述。我在各种环境下使用它的方式是我写了一个像这样的小辅助函数:

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}`];
}

这允许我在 fixtures and/or 测试中使用该函数:

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

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

我仍然只有 config.json:

中的具体 URL
{
    "baseUrlDev": "...",
    "baseUrlStaging": "...",
    "baseUrlProd": "..."
}

您可以为 NPM 命令指定参数 npm run <command> [-- <args>]。更多信息:Sending command line arguments to npm script

对于TestCafe,好像是通过Metadata

来指定值