我在单击运行之前选择的未在测试环境(DEV,QA,阶段和产品)中运行的柏树测试的Azure DevOps管道

发布于 2025-01-23 02:58:32 字数 6913 浏览 0 评论 0原文

我在cypress.env.json文件中添加了不同的环境,例如下面

{
   "environment":"staging",
   "prod":"http://production.com",
   "staging":"http://qual.com/dashboard",
   "sandbox":"http://sandbox.com/dashboard",
   "test": "http://test.com/dashboard",
   "versionProd": "vAlpha6",
   "versionStage":"vAlpha7"
}

和.yml文件,

   parameters:
- name: TestType
  displayName: Test
  type: string
  default: sanity
  values:
  - sanity
  - smoke

  - parameterization
- name: environment
  displayName: environment
  type: string
  default: staging
  values:
  - test
  - sandbox
  - staging
    

当我尝试选择测试类型(Sanity/Smoke等)时,我的堆栈数据如下所示。但是,当我尝试选择测试堆栈时,它正在考虑我在Env.json文件中提供的内容。我需要它在我在管道中选择的环境中运行,而不是在Env.Json中通过的环境。我可以在这里得到一些帮助吗?预先感谢

下面的是我的软件包文件和YML文件。

==== package.json file

 {
  "name": "automation",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "clean:reports": "rmdir /S /Q cypress\\reports && mkdir cypress\\reports && mkdir cypress\\reports\\mochareports",
    "presanity": "npm run clean:reports",
    "presmoke": "npm run clean:reports",
    "combine-reports": "mochawesome-merge cypress/reports/mocha/*.json > cypress/reports/mochareports/report.json",
    "generate-reports": "marge cypress/reports/mochareports/report.json -f report -o cypress/reports/mochareports -i true",
    "junitReport": "node_modules/.bin/jsonjunit --json cypress/reports/mocha --junit cypress/reports/mocha",
    "cy:verify": "cypress verify",
    "postsanity": "npm run combine-reports && npm run generate-reports && npm run junitReport",
    "postsmoke": "npm run combine-reports && npm run generate-reports && npm run junitReport",
    "sanityTest": "cypress run --headless --spec 'cypress/integration/SanityTestCases/**/*'",
    "smokeTest": "cypress run --headless --spec 'cypress/integration/smokeTests/**/*'",
    "postparameterization": "npm run combine-reports && npm run generate-reports && npm run junitReport",
    "parameterizationTest": "cypress run --headless --spec 'cypress/integration/parameterizationTests/**/*'",
    "sanity": "npm run sanityTest || npm run postsanity",
    "smoke": "npm run smokeTest || npm run postsmoke",
    "parameterization": "npm run parameterizationTest || npm run postparameterization"
  },
  "author": "Yeswanth",
  "license": "ISC",
  "devDependencies": {
    "cypress": "^8.7.0",
    "cypress-file-upload": "^4.0.6",
    "cypress-junit-reporter": "^1.3.1",
    "cypress-localstorage-commands": "^1.2.4",
    "cypress-multi-reporters": "^1.2.4",
    "json-to-junit": "^2.2.0",
    "mocha": "^7.1.1",
    "mocha-junit-reporters": "^1.23.6",
    "mochawesome": "^5.0.0",
    "mochawesome-merge": "^4.0.3",
    "mochawesome-report-generator": "^5.1.0"
  },
  "dependencies": {
    "convert-csv-to-json": "^1.3.0"
  }
}}

======= yml文件==

   name: $(TeamProject)_$(BuildDefinitionName)_$(SourceBranchName)_$(Date:yyyyMMdd)$(Rev:.r)
resources:
  repositories:
    - repository: templates
      endpoint: cto.3dp.ind.GHE
      name: codeway/templates
      type: githubenterprise
      #ref: refs/heads/3d-euphoria-dev

parameters:
- name: TestType
  displayName: Test
  type: string
  default: sanity
  values:
  - sanity
  - smoke
  - parameterization
- name: environment
  displayName: environment
  type: string
  default: staging
  values:
  - test
  - sandbox
  - staging

variables:
- name: result
  value: 0
  
trigger: none

pr: none

schedules:
- cron: "0 */6 * * *"
  displayName: Daily  build
  branches:
    include:
    - master
    - develop
  always: true

pool: CodeWayAWSWindows

stages:
  - stage: apps_3d
    displayName: 3d App
    jobs: 
    - job: apps_3d_react
      displayName: 3d Apps
      steps:
        - checkout: self
          persistCredentials: true
          clean: true
        - template: lib/installers/nodejs-v0.yaml@templates
        - bash: |
            echo "Entering npm install"
            npm install
            echo "exiting into bash"
          displayName: 'Npm Install'
        - bash: |
            npm run cy:verify
          displayName: 'Cypress verify' 
          
        - bash: |
            npm run ${{ parameters.TestType }}
          displayName: npm run 
        - task: PublishBuildArtifacts@1
          displayName: "Publish Artifact: html test report"
          inputs:
             PathtoPublish: cypress/reports/mochareports
             ArtifactName: CypressAndAzureDevopsTestRunReport
             condition: succeededOrFailed()
        
        - task: PublishHtmlReport@1
          displayName: 'Publish HTML Report'
          inputs:
             reportDir: 'cypress/reports/mochareports/report.html'
             
        - task: PublishTestResults@1
          inputs:
            testResultsFormat: 'JUnit'
            testResultsFiles: '**/test-results.*.xml'
            mergeTestResults: true
            publishRunAttachments: true
            condition: succeededOrFailed()

        - bash: |
            !/bin/bash
            echo "Entering into bash"
            echo "##vso[task.setvariable variable=result]{passPercentage}"
            echo ${{variables.result}}
            echo $(result)
            echo "exiting from bash"
          displayName: 'update results'
        - bash: |
           # condition: succeededOrFailed()
           # !/bin/bash
           set -x
           umask 0002
           cat > ./post.json <<'endmsg'
           {
                   "@type": "MessageCard",
                   "@context": "https://schema.org/extensions",
                   "summary": "1 new build message",
                   "themeColor": "008000",
                   "sections": [
                           {
                                   "activityTitle": "${{ parameters.TestType }} test-${{ parameters.environment }} stack : [$(agent.jobstatus)] ,$(result) tests passed",
                                   "activitySubtitle": "$(Build.RequestedFor) - $(Build.SourceBranchName) - $(Build.SourceVersion)",
                                   "potentialAction": [
                                           {
                                                   "@type": "OpenUri",
                                                   "name": "View results",
                                                   
                                           }
                                   ]
                           }
                   ]
           }
           endmsg
           curl --retry 3 -X POST -H "Content-Type: application/json" -d @post.json https://hp.webhook.office.com/webhookb2/75b2a1e8-6d3a-4421-95bc-4fc863ca2e6c@ca7981a2-785a-463d-b82a-3db87dfc3ce6/IncomingWebhook/78a9fd28974f4d28b1bef23cd282eb7f/f82806b8-765d-475d-8c5f-b7a12b378ee3
    

I have different environments added in my cypress.env.json file like below

{
   "environment":"staging",
   "prod":"http://production.com",
   "staging":"http://qual.com/dashboard",
   "sandbox":"http://sandbox.com/dashboard",
   "test": "http://test.com/dashboard",
   "versionProd": "vAlpha6",
   "versionStage":"vAlpha7"
}

And in .YML file, I have stack data like below

   parameters:
- name: TestType
  displayName: Test
  type: string
  default: sanity
  values:
  - sanity
  - smoke

  - parameterization
- name: environment
  displayName: environment
  type: string
  default: staging
  values:
  - test
  - sandbox
  - staging
    

When I try to select test type(Sanity/smoke, etc) it is taking correctly. But when I try to select test stack, it is considering what I provided in env.json file. I need it to run in the environment that I selected in pipeline but not the one that I have passed in env.json. Can I get some help here please. Thanks in Advance

Below is my package file and YML file.

====Package.json file

 {
  "name": "automation",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "clean:reports": "rmdir /S /Q cypress\\reports && mkdir cypress\\reports && mkdir cypress\\reports\\mochareports",
    "presanity": "npm run clean:reports",
    "presmoke": "npm run clean:reports",
    "combine-reports": "mochawesome-merge cypress/reports/mocha/*.json > cypress/reports/mochareports/report.json",
    "generate-reports": "marge cypress/reports/mochareports/report.json -f report -o cypress/reports/mochareports -i true",
    "junitReport": "node_modules/.bin/jsonjunit --json cypress/reports/mocha --junit cypress/reports/mocha",
    "cy:verify": "cypress verify",
    "postsanity": "npm run combine-reports && npm run generate-reports && npm run junitReport",
    "postsmoke": "npm run combine-reports && npm run generate-reports && npm run junitReport",
    "sanityTest": "cypress run --headless --spec 'cypress/integration/SanityTestCases/**/*'",
    "smokeTest": "cypress run --headless --spec 'cypress/integration/smokeTests/**/*'",
    "postparameterization": "npm run combine-reports && npm run generate-reports && npm run junitReport",
    "parameterizationTest": "cypress run --headless --spec 'cypress/integration/parameterizationTests/**/*'",
    "sanity": "npm run sanityTest || npm run postsanity",
    "smoke": "npm run smokeTest || npm run postsmoke",
    "parameterization": "npm run parameterizationTest || npm run postparameterization"
  },
  "author": "Yeswanth",
  "license": "ISC",
  "devDependencies": {
    "cypress": "^8.7.0",
    "cypress-file-upload": "^4.0.6",
    "cypress-junit-reporter": "^1.3.1",
    "cypress-localstorage-commands": "^1.2.4",
    "cypress-multi-reporters": "^1.2.4",
    "json-to-junit": "^2.2.0",
    "mocha": "^7.1.1",
    "mocha-junit-reporters": "^1.23.6",
    "mochawesome": "^5.0.0",
    "mochawesome-merge": "^4.0.3",
    "mochawesome-report-generator": "^5.1.0"
  },
  "dependencies": {
    "convert-csv-to-json": "^1.3.0"
  }
}}

=====YML file==

   name: $(TeamProject)_$(BuildDefinitionName)_$(SourceBranchName)_$(Date:yyyyMMdd)$(Rev:.r)
resources:
  repositories:
    - repository: templates
      endpoint: cto.3dp.ind.GHE
      name: codeway/templates
      type: githubenterprise
      #ref: refs/heads/3d-euphoria-dev

parameters:
- name: TestType
  displayName: Test
  type: string
  default: sanity
  values:
  - sanity
  - smoke
  - parameterization
- name: environment
  displayName: environment
  type: string
  default: staging
  values:
  - test
  - sandbox
  - staging

variables:
- name: result
  value: 0
  
trigger: none

pr: none

schedules:
- cron: "0 */6 * * *"
  displayName: Daily  build
  branches:
    include:
    - master
    - develop
  always: true

pool: CodeWayAWSWindows

stages:
  - stage: apps_3d
    displayName: 3d App
    jobs: 
    - job: apps_3d_react
      displayName: 3d Apps
      steps:
        - checkout: self
          persistCredentials: true
          clean: true
        - template: lib/installers/nodejs-v0.yaml@templates
        - bash: |
            echo "Entering npm install"
            npm install
            echo "exiting into bash"
          displayName: 'Npm Install'
        - bash: |
            npm run cy:verify
          displayName: 'Cypress verify' 
          
        - bash: |
            npm run ${{ parameters.TestType }}
          displayName: npm run 
        - task: PublishBuildArtifacts@1
          displayName: "Publish Artifact: html test report"
          inputs:
             PathtoPublish: cypress/reports/mochareports
             ArtifactName: CypressAndAzureDevopsTestRunReport
             condition: succeededOrFailed()
        
        - task: PublishHtmlReport@1
          displayName: 'Publish HTML Report'
          inputs:
             reportDir: 'cypress/reports/mochareports/report.html'
             
        - task: PublishTestResults@1
          inputs:
            testResultsFormat: 'JUnit'
            testResultsFiles: '**/test-results.*.xml'
            mergeTestResults: true
            publishRunAttachments: true
            condition: succeededOrFailed()

        - bash: |
            !/bin/bash
            echo "Entering into bash"
            echo "##vso[task.setvariable variable=result]{passPercentage}"
            echo ${{variables.result}}
            echo $(result)
            echo "exiting from bash"
          displayName: 'update results'
        - bash: |
           # condition: succeededOrFailed()
           # !/bin/bash
           set -x
           umask 0002
           cat > ./post.json <<'endmsg'
           {
                   "@type": "MessageCard",
                   "@context": "https://schema.org/extensions",
                   "summary": "1 new build message",
                   "themeColor": "008000",
                   "sections": [
                           {
                                   "activityTitle": "${{ parameters.TestType }} test-${{ parameters.environment }} stack : [$(agent.jobstatus)] ,$(result) tests passed",
                                   "activitySubtitle": "$(Build.RequestedFor) - $(Build.SourceBranchName) - $(Build.SourceVersion)",
                                   "potentialAction": [
                                           {
                                                   "@type": "OpenUri",
                                                   "name": "View results",
                                                   
                                           }
                                   ]
                           }
                   ]
           }
           endmsg
           curl --retry 3 -X POST -H "Content-Type: application/json" -d @post.json https://hp.webhook.office.com/webhookb2/75b2a1e8-6d3a-4421-95bc-4fc863ca2e6c@ca7981a2-785a-463d-b82a-3db87dfc3ce6/IncomingWebhook/78a9fd28974f4d28b1bef23cd282eb7f/f82806b8-765d-475d-8c5f-b7a12b378ee3
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫七 2025-01-30 02:58:32

在未看到实际管道的情况下。yml文件和测试中环境变量的使用,当然很难给出答案。

据我了解,您想在执行管道之前选择一个可以在该环境中运行测试的环境。因此,我想您的管道参数看起来像这样:

parameters:
- name: TestType 
  displayName: Test 
  type: string 
  default: sanity 
  values:
  - sanity
  - smoke
  - parameterization
- name: environment 
  displayName: environment 
  type: string 
  default: staging 
  values:
  - test
  - sandbox
  - staging

然后您有几个选项可以将此信息用于柏树测试执行。另请参见环境变量官方文档的部分。

您的一个选项是使用cypress_ namepspace,如描述 。有了这样,您可以从测试中可用的管道参数中获取信息,如下所示:

- script: |
    npm run cy:int
  workingDirectory: $(Build.SourcesDirectory)
  displayName: Run Cypress Tests
  env:
    CYPRESS_TestType: '$(TestType)'
    CYPRESS_environment: '$(environment)'

然后,您可以在柏树测试中访问如下的信息,例如访问某个URL:

if(Cypress.env('environment') === 'sandbox') {
  cy.visit('http://sandbox.com/dashboard');
}

Without seeing your actual pipeline .yml file and the use of the environment variables in your tests, it is of course difficult to give an answer.

As far as I understand your question, you want to select an environment on which to run the tests before executing your pipeline. So I guess your pipeline parameters look something like this:

parameters:
- name: TestType 
  displayName: Test 
  type: string 
  default: sanity 
  values:
  - sanity
  - smoke
  - parameterization
- name: environment 
  displayName: environment 
  type: string 
  default: staging 
  values:
  - test
  - sandbox
  - staging

Then you have several options to use this information for your Cypress test execution. See also the Environment Variables section of the official docs.

One option for you would be to use the CYPRESS_ namepspace like described here. With this you could make the information from your pipeline parameters available in your tests as follows:

- script: |
    npm run cy:int
  workingDirectory: $(Build.SourcesDirectory)
  displayName: Run Cypress Tests
  env:
    CYPRESS_TestType: '$(TestType)'
    CYPRESS_environment: '$(environment)'

You can then access the information in your Cypress tests as follows, e.g. for visiting a certain url:

if(Cypress.env('environment') === 'sandbox') {
  cy.visit('http://sandbox.com/dashboard');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文