在赛普拉斯测试中访问环境变量

时间:2020-09-10 08:28:49

标签: cypress

我试图使用cy.exec从赛普拉斯测试中调用外部脚本来创建环境变量,然后我想在测试中访问该环境变量。使用Cypress.env(<environmental variable>)可以做到吗?

外部脚本如下:

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环境变量。

1 个答案:

答案 0 :(得分:0)

根据文档(here),您必须在变量名称前放置cypress_或CYPRESS _:

*//id[matches(.,'.*(^|\s)xander($|\s).*','i')]