【问题标题】:Accessing environmental variable in Cypress test在赛普拉斯测试中访问环境变量
【发布时间】:2020-09-10 17:59:15
【问题描述】:

我正在尝试使用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 环境变量返回未定义。

【问题讨论】:

    标签: cypress


    【解决方案1】:

    根据文档 (here),您必须将 cypress_ 或 CYPRESS_ 放在变量名之前:

    process.env.cypress_apikey = apikey;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-07
      • 2022-12-10
      • 2021-10-27
      • 2020-10-09
      • 1970-01-01
      • 2023-02-14
      • 1970-01-01
      • 2021-11-06
      相关资源
      最近更新 更多