typescript 如何使用GitHub操作发送环境变量到Cypress?

iaqfqrcu  于 2023-01-27  发布在  TypeScript
关注(0)|答案(1)|浏览(135)

我尝试使用GitHub操作运行Cypress测试,但不知道如何将环境变量发送到Cypress以覆盖我的本地配置。
目前我已经将baseUrl设置为https://localhost:3000,但希望在我的开发环境中运行时发送另一个URL。我还希望发送一个beforeEach函数中使用的头(token),以便允许我的所有请求通过我的开发环境。
我尝试过使用process.env.CYPRESS_XYZ和Cypress.env('XYZ '),但都没有成功。我还尝试过在yaml文件的with:部分下设置config: baseUrl=https://xyz
当我在本地运行它时,安装程序可以工作,所以只有在尝试为我的dev env设置新的baseUrl时才会出现这些问题。
您知道如何将工作流中的BASE_URL和DEV_TOKEN放入Cypress配置中吗?
cypress.config.ts

import { addCucumberPreprocessorPlugin } from '@badeball/cypress-cucumber-preprocessor';
import createEsbuildPlugin from '@badeball/cypress-cucumber-preprocessor/esbuild';
import createBundler from '@bahmutov/cypress-esbuild-preprocessor';
import { defineConfig } from 'cypress';
import * as fs from 'fs';

export default defineConfig({
  blockHosts: [
    '*.googletagmanager.com',
    '*.google-analytics.com',
  ],
  chromeWebSecurity: false,
  defaultCommandTimeout: 10000,
  e2e: {
    baseUrl: process.env.CYPRESS_BASE_URL || 'https://localhost:3000',
    experimentalRunAllSpecs: true,
    async setupNodeEvents(
      on: Cypress.PluginEvents,
      config: Cypress.PluginConfigOptions,
    ): Promise<Cypress.PluginConfigOptions> {
      // This is required for the preprocessor to be able to generate JSON reports after each run, and more,
      await addCucumberPreprocessorPlugin(on, config);

      on(
        'file:preprocessor',
        createBundler({
          plugins: [createEsbuildPlugin(config)],
        }),
      );

      on('after:spec', async (_, results) => {
        if (results && results.video) {
          // Do we have failures for any retry attempts?
          const failures = results.tests?.some((test) =>
            test.attempts.some((attempt) => attempt?.state === 'failed'),
          );
          if (!failures) {
            // delete the video if the spec passed and no tests retried
            fs.unlink(results.video, (err) => {
              if (err) throw err;
              return;
            });
          }
        }
      });

      // Make sure to return the config object as it might have been modified by the plugin.
      return config;
    },
    specPattern: '**/*.feature',
  },
  env: {
    login_email: 'user@test.com',
    login_password: 'test123!',
  },
  projectId: 'xxxxx',
  screenshotsFolder: './cypress/screenshots',
  video: false,
  videosFolder: './cypress/videos',
  viewportHeight: 768,
  viewportWidth: 1024,
});

e2e.ts

import './commands';

Cypress.on('uncaught:exception', () => {
  return false;
});

beforeEach(() => {
  cy.intercept(`${Cypress.config('baseUrl')}**`, req => {
      req.headers['dev_token'] = Cypress.env('DEV_TOKEN')
  });
});

e2e.yaml

name: e2e tests

on:
  workflow_call:
    inputs:
      E2E_BASE_URL:
        type: string
        description: Cypress target URL
        default: false
        required: false
    secrets:
      CYPRESS_RECORD_KEY:
        required: true
      DEV_TOKEN:
        required: true

jobs:
  e2e-test:
    name: Cypress run
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        containers: [1, 2, 3, 4]
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Cypress run
        uses: cypress-io/github-action@v5
        with:
          browser: chrome
          record: true
          parallel: true
        env:
          DEV_TOKEN: ${{ secrets.DEV_TOKEN }}
          CYPRESS_BASE_URL: ${{ inputs.E2E_BASE_URL }}
          CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

工作流操作中的错误消息

Cypress could not verify that this server is running:

  > https://localhost:3000

We are verifying this server because it has been configured as your baseUrl.

Cypress automatically waits until your server is accessible before running tests.

We will try connecting to it 3 more times...
We will try connecting to it 2 more times...
We will try connecting to it 1 more time...

Cypress failed to verify that your server is running.
    • 编辑:**

我设法使用config参数让它工作,更新设置如下:
cypress.config.ts

...
e2e: {
  baseUrl: 'https://localhost:3000',
  experimentalRunAllSpecs: true,
...

e2e.yaml

...
- name: Cypress run
    uses: cypress-io/github-action@v5
    with:
      browser: chrome
      record: true
      parallel: true
      config: baseUrl=${{ inputs.E2E_BASE_URL }}
...

当我第一次尝试它的时候,我一定错过了一些东西,我假设我在上面最初的例子中做它的方式应该和下面的答案中描述的一样好,可能和我在这个例子中的输入有同样的问题。
吸取教训(再次),小错别字或遗漏花费了大量的时间和挫折...

afdcj2ne

afdcj2ne1#

在我的项目中,我是这样处理用户名的,类似地处理任何类型的变量:
1.您首先通过GitHub接口定义Actions密码
1.始终以**CYPRESS_**开头,并记住这将被截断
1.然后在工作流中执行一个步骤yml:

- name: set approx
  run: echo "CYPRESS_AUTOMATED_TESTS_USERNAME1=${{secrets.CYPRESS_AUTOMATED_TESTS_USERNAME1}}" >> $GITHUB_ENV

1.然后只需在使用Cypress.env(“AUTOMATED_TESTS_USERNAME1”)的测试中直接使用它,
This video帮助我回到了过去。

相关问题