0

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
    
  • 1
    could you please add your yaml file ? It will help to reproduce the issue ? Also should you be updated the json file based on what you've selected as environment ? – Thomas Apr 22 '22 at 01:11
  • Added. Please check. Can you let me know if I am missing script to call env in .yml file. Thanks for responding. – YESWANTH EMURI Aug 24 '22 at 10:33

1 Answers1

1

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');
}
Sebastiano Schwarz
  • 1,060
  • 2
  • 13
  • 32
  • Added. Please check. Can you let me know if I am missing script to call env in .yml file. Or is there anything I need to add in scripts. Thanks for responding. – YESWANTH EMURI Aug 24 '22 at 10:33
  • Ok, so your pipeline really looks like I anyway assumed in my answer already. So as I provided you with the code here that you could directly use to transfer the values for the "TestType" and "Environment" variables from your pipeline run to your actual Cypress test execution and also added the code that you can use to access those variables then within your Cypress test file, what else do you need? If something is still unclear, feel free to let me know. Otherwise I would be very happy if you accept my answer :) – Sebastiano Schwarz Aug 25 '22 at 17:04