简体   繁体   English

azure 中的 Testcafe 使用特定的 URL devops

[英]Testcafe in azure devops with specific URL

I am trying to e2e-testcafe in my azure devops uisng IAAC(Infra as a code) for java-script code.我正在尝试在我的 azure devops uisng IAAC(Infra as a code) 中对 java 脚本代码进行 e2e-testcafe。 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.部署到测试环境(存储帐户)后,我需要在该部署的代码中进行 e2e。 So i am using below steps for that jobs: my azure-pipeline.yml has below所以我对这些工作使用了以下步骤:我的 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');
});

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.但是在这里它运行来自我的 test.js 的测试,它是应用程序的一部分,所有测试都在代理的本地服务器中运行,azure devops 正在使用它的 windows 服务器。 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.但是现在我想将 URI 传递给 npm 测试,当它进入我的应用程序时,它再次获取 localhost:8080 并在本地执行。 So can some one help me running e2e test in the url which i pass which indeed will be the url of storageaccount?那么有人可以帮我在我通过的 url 中运行 e2e 测试,这确实是 storageaccount 的 url? like my command should be in azurepipelines.yaml就像我的命令应该在 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.在我的 test.js 中,它应该高于 URL 我在管道中运行时传入 Yaml。

Well, this is quite a common question with TestCafe.嗯,这是 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 .使用一些外部模块,例如minimist ,这已经在 stackoverflow 上解决 The bottom line is that such an external module allows you to parse command line arguments, which is what you are looking for.最重要的是,这样的外部模块允许您解析命令行 arguments,这正是您要寻找的。

  2. Use environment variables that you should be able to set up in Azure DevOps.使用您应该能够在 Azure DevOps 中设置的环境变量。 From the TestCafe's point of view, it's described in documentation here .从 TestCafe 的角度来看,它在此处的文档中进行了描述。 The way I made this work with various environments is I wrote a little helper function like this:我在各种环境下进行这项工作的方式是我写了一个小助手 function ,如下所示:

Helpers/baseUrl.js助手/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:这允许我在夹具和/或测试中使用 function:

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

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

and I still have the concrete URLs only in config.json :而且我仍然只有在config.json中有具体的 URL:

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

You could specify argument to NPM command npm run <command> [-- <args>] .您可以为 NPM 命令npm run <command> [-- <args>]指定参数。 More information: Sending command line arguments to npm script更多信息: 将命令行 arguments 发送到 npm 脚本

For TestCafe, it seems that you specify value through Metadata对于TestCafe,您似乎通过元数据指定值

In v1.20.0 and later, TestCafe offers a way to specify the baseUrl in the test run setup: 在 v1.20.0及更高版本中,TestCafe 提供了一种在测试运行设置中指定baseUrl的方法:

You can use this approach along with environment variables or custom command line arguments to determine what url should be assigned to the baseUrl option.您可以将此方法与环境变量或自定义命令行 arguments 一起使用,以确定应将什么 url 分配给baseUrl选项。 Alternatively, you can have a different configuration file for each test run setup and switch between these files using the --config-file option .或者,您可以为每个测试运行设置使用不同的配置文件,并使用--config-file选项在这些文件之间切换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM