【问题标题】:GitHub Action [cypress CI integration ]: How to set env variables before running cypress run commandGitHub Action [cypress CI 集成]:如何在运行 cypress run 命令之前设置环境变量
【发布时间】:2021-09-08 07:18:01
【问题描述】:

我正在为我的存储库编写工作流,并且我想在有代码推送时运行测试用例。

这是来自 .yml 文件的代码 sn-p:

  - 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

来自 JSON 的片段:

{

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

所以,我想在 cypress run 命令中将用户名和密码与配置文件一起传递,以便可以将它们设置为 env 变量,因为我正在为我的登录测试模块使用用户名和密码。

使用上面的代码我得到了错误:

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

谢谢:)

【问题讨论】:

标签: javascript continuous-integration cypress github-actions


【解决方案1】:

首先,您需要将环境变量作为空字符串添加到 cypress.json

{

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

在 main.yml 文件中,需要在变量中添加 CYPRESS_ 前缀:

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

此外,您应该使用可在 cypress 文档 (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}}

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

【讨论】:

    猜你喜欢
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    • 1970-01-01
    • 2022-11-06
    • 2015-12-24
    相关资源
    最近更新 更多