我有一个问题,我需要在测试中重新运行配置,因为我们使用的角色只有一个小时的权限。您甚至无法扩展角色权限,因为我们在使用此角色时进行角色链接。有人遇到过这个问题吗?cypress/plugins/index.js我的问题是,当测试失败或测试在凭据过期后运行时,如何重新运行代码以获取新的凭据?
插件/index.ts
import * as secretsManager from '@amzn/cypress-midway-plugin/secret_manager';
import PluginEvents = Cypress.PluginEvents;
import PluginConfigOptions = Cypress.PluginConfigOptions;
import * as AWS from 'aws-sdk'
import { CYPRESS_PRINCIPAL, CYPRESS_SECRET_ID, REGION, STAGE } from '../resources/constants';
import fetchSigv4Session from "./sigv4";
import getEnvVariables from "./env-variables";
/**
* @type {Cypress.PluginConfig}
*/
export default async (on: PluginEvents, config: PluginConfigOptions): Promise<PluginConfigOptions> => { // `on` is used to hook into various events Cypress emits
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
// assuming running from Hydra
on('task', {
log (message) {
console.log(message)
return null
}
})
config.env.SIGV4_SESSION = await fetchSigv4Session(AWS);
config.env.REGION = REGION;
config.env.CYPRESS_ENV_VARIABLES = getEnvVariables(STAGE)
on('after:run', () => {
console.log("Test finished at: ", new Date())
});
return config;
};
Run Code Online (Sandbox Code Playgroud)
支持/index.ts
// Import commands.js using ES2015 syntax:
import AWS = require('aws-sdk');
import fetchSigv4Session from '../plugins/sigv4';
import './commands'
// Alternatively you can use CommonJS syntax:
// require('./commands')
const CYPRESS_LOG_NAME = 'Login with Midway';
Cypress.on('uncaught:exception', (err, runnable) => {
// returning false here prevents Cypress from
// failing the test
console.warn('Uncaught exception (suppressed):', err);
return false;
});
//Runs at start of each test suites
before(() => {
cy.log("Starting Authentication")
cy.setCookie('region', Cypress.env('REGION'));
cy.setCookie('session', Cypress.env('SIGV4_SESSION'));
const preserve = [
'session',
'cypress',
'region'
];
Cypress.Cookies.defaults({ preserve });
return cy.request({
url: `https://authentication.api.com/api/session-status`,
method: 'GET',
headers: {'Clear-Site-Data': "*"} //This will allow us to do a fresh call rather than using browser's cache
}, ).then(async response => {
Cypress.log({
name: CYPRESS_LOG_NAME, message: [`Logged in and running cypress tests.`]
});
cy.wrap(response, {log: false});
})
});
Run Code Online (Sandbox Code Playgroud)
因此,当我遇到这个问题时,我需要获得新的凭据,如果我在测试之间或在 cypress 失败事件处理程序中执行此操作,它不会识别任何节点环境变量。不确定是否有任何其他钩子可以调用来拥有plugins/index.ts正确运行代码的环境
plugins要在测试中重新运行配置,您可以为当前仅在启动时运行的步骤创建任务。
这只是大概的模式,因为我不知道完整的应用程序。
...
function runConfig() {
config.env.SIGV4_SESSION = await fetchSigv4Session(AWS);
config.env.REGION = REGION;
config.env.CYPRESS_ENV_VARIABLES = getEnvVariables(STAGE)
}
export default async (on: PluginEvents, config: PluginConfigOptions): Promise<PluginConfigOptions> => { // `on` is used to hook into various events Cypress emits
on('task', {
...
runConfig () {
runConfig() // for mid-test change
return null
}
})
// for startup
runConfig()
return config;
}
Run Code Online (Sandbox Code Playgroud)
同样,在支持(如果需要)中,这是一般模式:
...
Cypress.Command.add('runConfig', () => {
cy.log("Starting Authentication")
cy.setCookie('region', Cypress.env('REGION'));
cy.setCookie('session', Cypress.env('SIGV4_SESSION'));
const preserve = [
'session',
'cypress',
'region'
];
Cypress.Cookies.defaults({ preserve });
return cy.request({
url: `https://authentication.api.com/api/session-status`,
method: 'GET',
headers: {'Clear-Site-Data': "*"} //This will allow us to do a fresh call rather than using browser's cache
}, ).then(async response => {
Cypress.log({
name: CYPRESS_LOG_NAME, message: [`Logged in and running cypress tests.`]
});
cy.wrap(response, {log: false});
})
})
//Runs at start of each test suites
before(() => {
runConfig()
});
Run Code Online (Sandbox Code Playgroud)
您现在可以在任何测试或挂钩中调用这两个方法
it('runs new config', () => {
...
cy.task('runConfig')
...
cy.runConfig()
})
Run Code Online (Sandbox Code Playgroud)
小智 6
在测试中更改配置的方法是使用Cypress.config(configKey, newConfigValue).
但是 Cypress 有固定的配置键,因此对于凭据,您可能使用环境变量,这是类似的语法:Cypress.env(envKey, newEnvValue)。
这可用于任何密钥,而不仅仅是赛普拉斯定义的密钥。
管理凭据的正确方法是使用cy.session()命令。例如,
// Caching session when logging in via API
cy.session(username, () => {
cy.request({
method: 'POST',
url: '/login',
body: { username, password },
}).then(({ body }) => {
window.localStorage.setItem('authToken', body.token)
})
})
Run Code Online (Sandbox Code Playgroud)
然后您可以通过使用cy.session()不同的username.
以下是进一步阅读的参考:Session
| 归档时间: |
|
| 查看次数: |
149 次 |
| 最近记录: |