Cypress 柏树试验中环境变量的获取

Cypress 柏树试验中环境变量的获取,cypress,Cypress,我试图使用cy.exec从Cypress测试调用外部脚本来创建环境变量,然后我希望在测试中访问该环境变量。使用Cypress.env()是否可以实现这一点 外部脚本如下所示: const cypress = require('cypress'); const pkg = require('@glib/cypress-secrets'); const { createAuthApiKeyKubeSecret } = pkg; const username = 'test-consumer';

我试图使用
cy.exec
从Cypress测试调用外部脚本来创建环境变量,然后我希望在测试中访问该环境变量。使用
Cypress.env()
是否可以实现这一点

外部脚本如下所示:

const cypress = require('cypress');
const pkg = require('@glib/cypress-secrets');

const { createAuthApiKeyKubeSecret } = pkg;
const username = 'test-consumer';    

const apikey = createAuthApiKeyKubeSecret(username);
console.log(apikey);
process.env.apikey = apikey;
此脚本由测试中的before函数调用

describe("Test to create a capability", function () {
    before(() => {
        cy.exec('node create-secret.js');
    });

    after(() => {
        cy.exec('node delete-secret.js', {log: true});
    });

    it('Checks on the Create page', function() {
        cy.visit(Cypress.config().baseUrl + "?apikey=" + Cypress.env('apikey'));
        // We need to check if we are on the correct page
        // We just need to check two elements, a label and a button.
        cy.contains('About the capability').should('exist');
        cy.contains('button', 'Next Step').should('exist')
    });
});

baseUrl
设置正确,但是
apikey
环境变量返回时未定义。

根据文档()您必须在变量名称前放置cypress\uuu或cypress\uu:

process.env.cypress_apikey = apikey;