Testcafe in azure devops with specific URL



我正在尝试在我的 azure devops uisng IAAC(Infra as a code( 中进行 e2e-testcafe for java-script code。因此,我有不同的阶段构建、测试和部署到不同的环境。部署到测试环境(存储帐户(后,我需要在该部署的代码中进行 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');
});

但是在这里它运行我的测试中的测试.js这是应用程序的一部分,所有测试都在代理的本地服务器中运行,该代理由 azure devops 为我使用,它的 Windows 服务器在这里。但是现在我想通过 URI 到 npm 测试,当它进入我的应用程序时,它再次选取 localhost:8080 并在本地执行。那么有人可以帮助我在我通过的网址中运行 e2e 测试,这确实是存储帐户的网址吗?就像我的命令应该在 azurepipelines.yaml 中一样

npm run test --URL

在我的测试中.js它应该在管道中运行时拾取我在 Yaml 中传递的 URL 以上。

您可以为 NPM 命令指定参数npm run <command> [-- <args>]。详细信息:将命令行参数发送到 npm 脚本

对于TestCafe,似乎您可以通过元数据指定值

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

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

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

帮助程序/基本网址.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}`];
}

这允许我在夹具和/或测试中使用该功能:

import { getBaseUrl } from '../Helpers/baseUrl';
fixture `Add User Child`    
.page(getBaseUrl());   

我仍然只有config.json的具体 URL:

{
"baseUrlDev": "...",
"baseUrlStaging": "...",
"baseUrlProd": "..."
}

最新更新