具有特定 URL 的 azure devops 中的 Testcafe

我正在尝试在我的 azure devops uisng IAAC(Infra as a code) 中对 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 测试,这确实是 storageaccount 的 url?就像我的命令应该在 azurepipelines.yaml


npm run test --URL

在我的 test.js 中,它应该选择我在管道中运行时传入 Yaml 的 URL。


拉丁的传说
浏览 136回答 2
2回答

喵喵时光机

您可以为 NPM 命令指定参数npm run <command> [-- <args>]。更多信息:向 npm 脚本发送命令行参数对于TestCafe,您似乎通过元数据指定值

HUWWW

嗯,这是 TestCafe 的一个很常见的问题。一个简单的答案是,没有直接的方法,但有一些解决方法:使用一些外部模块,例如minimist,这已经在 stackoverflow 上解决了。最重要的是,这样的外部模块允许您解析命令行参数,这正是您正在寻找的。使用应该能够在 Azure DevOps 中设置的环境变量。从 TestCafe 的角度来看,它在此处的文档中进行了描述。我在各种环境中进行这项工作的方式是我编写了一个像这样的小辅助函数:助手/baseUrl.jsimport config from '../config';const baseUrlOf = {&nbsp; &nbsp; "dev": config.baseUrlDev,&nbsp; &nbsp; "staging": config.baseUrlStaging,&nbsp; &nbsp; "prod": config.baseUrlProd};export function getBaseUrl () {&nbsp; &nbsp; return baseUrlOf[`${process.env.TESTCAFE_ENV}`];}这允许我在夹具和/或测试中使用该功能:import { getBaseUrl } from '../Helpers/baseUrl';fixture `Add User Child`&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; .page(getBaseUrl());&nbsp; &nbsp;而且我仍然只有以下具体网址config.json:{&nbsp; &nbsp; "baseUrlDev": "...",&nbsp; &nbsp; "baseUrlStaging": "...",&nbsp; &nbsp; "baseUrlProd": "..."}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript