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

问题描述

我试图使用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环境变量。

解决方法

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

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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...