简体   繁体   English

GitHub Action [cypress CI 集成]:如何在运行 cypress run 命令之前设置 env 变量

[英]GitHub Action [cypress CI integration ]: How to set env variables before running cypress run command

I am writing workflows for my repository, and I want to run test cases whenever there is a code push.我正在为我的存储库编写工作流,并且我想在有代码推送时运行测试用例。

Here is the code snippet from .yml file:这是 .yml 文件中的代码片段:

  - name: Run cypress test
    with:
      env: true
    env:
      username: ${{secrets.CYPRESS_USERNAME}}
      password: ${{secrets.CYPRESS_PASSWORD}}
    
    run: npm run cy:test --env fileConfig=production, username=$username, password=$password 
    continue-on-error: false

Snippet from JSON :来自 JSON 的片段:

{

"env": {
    "userId": "1",
    "environment": "production",
    "baseUrl": " base URL"
    }
}

So, I want to pass the username and password along with the configfile in the cypress run command so that they can be set as the env variable because I am using username and password for my login test module.因此,我想在 cypress run 命令中将用户名和密码与 configfile 一起传递,以便将它们设置为 env 变量,因为我在登录测试模块中使用了用户名和密码。

With the above code i am getting error :使用上面的代码,我收到错误:

The workflow is not valid.工作流无效。 .github/workflows/main.yml (Line: 43, Col: 9): Unexpected value 'run' .github/workflows/main.yml (Line: 36, Col: 9): Required property is missing: uses .github/workflows/main.yml (Line: 43, Col: 9): Unexpected value 'run' .github/workflows/main.yml (Line: 36, Col: 9): required property is missing: uses

Thanks :)谢谢 :)

First, you need to add the env variables as empty strings on cypress.json :首先,您需要在cypress.json上将 env 变量添加为空字符串:

{

  "env": {
    "userId": "1",
    "environment": "production",
    "baseUrl": "base URL",
    "username": "",
    "password": ""
  }
}

In main.yml file, you need to add the CYPRESS_ prefix to the variables:在 main.yml 文件中,您需要为变量添加 CYPRESS_ 前缀:

env:
  CYPRESS_username: ${{secrets.CYPRESS_USERNAME}}
  CYPRESS_password: ${{secrets.CYPRESS_PASSWORD}}

Also, you should use the setup that can be found on cypress docs ( https://docs.cypress.io/guides/continuous-integration/github-actions#Basic-Setup ), example:此外,您应该使用可以在 cypress docs ( https://docs.cypress.io/guides/continuous-integration/github-actions#Basic-Setup ) 上找到的设置,例如:

name: Cypress Tests

on: [push]

jobs:
  cypress-run:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      # Install NPM dependencies, cache them correctly
      # and run all Cypress tests
      - name: Cypress run
        uses: cypress-io/github-action@v2
        with:
          build: npm run build
          start: npm start
        env:
          CYPRESS_username: ${{secrets.CYPRESS_USERNAME}}
          CYPRESS_password: ${{secrets.CYPRESS_PASSWORD}}

You should also read this article: https://glebbahmutov.com/blog/keep-passwords-secret-in-e2e-tests/您还应该阅读这篇文章: https : //glebbahmutov.com/blog/keep-passwords-secret-in-e2e-tests/

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

相关问题 赛普拉斯未在 ci SyntaxError 中运行:使用 cypress-io/github-action@v2 typescript 的意外令牌“导出” - Cypress is not running in ci SyntaxError: Unexpected token 'export' with cypress-io/github-action@v2 typescript Gitlab CI - 服务器在 Cypress 测试可以运行之前被“杀死” - Gitlab CI - server gets 'killed' before Cypress tests can run Cypress.io:是否可以在 Cypress 中设置全局变量,如果是; 如何? - Cypress.io: Is is possible to set global variables in Cypress and if yes; how? 如何在大多数 cypress 测试之前运行,但不是全部 - How to run before on most cypress tests but not all 如何在 gitlab CI 上运行的同一端口上运行本地赛普拉斯测试? - How to run locally Cypress test on the same port as they are run at gitlab CI? 如何在 cypress.json 文件中使用 .env 变量? - How to use .env variables inside cypress.json file? 从命令行运行赛普拉斯测试得到=> CypressError:赛普拉斯测试在运行此命令时停止 - run cypress tests from command line getting => CypressError: Cypress test was stopped while running this command 如何在 Cypress 中使用变量 - How to use variables in Cypress 在 cypress.env.json 中定义变量 - Define variables in cypress.env.json 如何在赛普拉斯中创建命令 - How to creat command in Cypress
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM