You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitHub Action中Cypress环境变量为空致测试失败求助

问题描述

向TestBranch分支发起Pull Request时,Cypress测试无法正常运行,核心问题是环境变量RESOLUTION为空:

  • 测试第一步依赖该变量获取分辨率,但执行echo ${{ vars.RESOLUTION }}无任何输出
  • 触发测试错误:

CypressError: cy.viewport() can only accept a string preset or a width and height as numbers.

附上当前GitHub Action配置:

name: Cypress Tests

run-name: ${{ github.actor }} is running Cypress tests 🚀
on:
  pull_request:
    branches:
      - TestBranch
  workflow_dispatch:
jobs:
  cypress-run:
    runs-on: ubuntu-latest

    steps:
      # Install Node.js
      - name: Use Node.js 20.x
        uses: actions/setup-node@v3
        with:
          node-version: '20.x'
        # checkout code
      - name: Checkout
        uses: actions/checkout@v4

      - name: Echo environment variable
        run: echo "The environment variable is ${{ vars.RESOLUTION }}"

        # run tests
      - name: Cypress run
        uses: cypress-io/github-action@v5
        with:
          working-directory: .
        env: 
          DEBUG: '@cypress/github-action'
          AUTH_DOMAIN: ${{ vars.AUTH_DOMAIN }}
          EMAIL: ${{ vars.EMAIL }}
          EMAIL2: ${{ vars.EMAIL2 }}
          ORGNAME: ${{ vars.ORGNAME }}
          PASSWORD2: ${{ secrets.PASSWORD2 }}
          RESOLUTION: ${{ vars.RESOLUTION }}
          STAGINGENVIRONMENT: ${{ vars.STAGINGENVIRONMENT }}
排查与解决步骤
  • 确认变量配置存在:检查仓库的「Settings → Secrets and variables → Actions」页面,确认RESOLUTION变量已正确创建,且值格式符合要求(比如1920x1080这类自定义分辨率,或macbook-16这类Cypress预设字符串)
  • 检查变量权限范围:如果是组织级变量,需确认该变量的「Repository access」设置包含当前仓库;仓库级变量默认对PR触发的Workflow可读,若有特殊权限限制需解除
  • 验证测试代码的变量读取方式:Cypress会将Workflow中env字段的变量注入到Cypress.env()中,测试代码需通过Cypress.env('RESOLUTION')获取,而非直接读取process.env.RESOLUTION
  • 处理分辨率格式:如果变量值是宽x高格式的字符串,需在测试代码中拆分并转换为数字后传入cy.viewport(),示例代码:
    const [width, height] = Cypress.env('RESOLUTION').split('x');
    cy.viewport(parseInt(width), parseInt(height));
    
  • 增加调试步骤:在Cypress run步骤前添加打印所有环境变量的命令,确认变量是否被正确加载:
    - name: Print all environment variables
      run: printenv
    

内容的提问来源于stack exchange,提问作者Ryan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 18:42:35