如何在 promise 的 then() 块之外使用 Cypress fixture 数据
How to use Cypress fixture data outside of a promise's then() block
我正在测试 Cypress,我有 custom Cypress command:
的代码
Cypress.Commands.add('login', (user, password) => {
if (!user || !password) {
user = 'user@test.com.br';
password = '123321';
}
cy.visit('');
cy.contains('Entrar').click();
cy.get('input[name=_username]')
.type(user);
cy.get('input[name=_password]')
.type(password);
cy.get('.btn').click();
});
如果您在不带任何参数的情况下调用 cy.login()
,则 user
和 password
会在 if
块中分配。否则,它使用传递的参数值。
我试图在这里添加固定装置,并想出了这个代码:
Cypress.Commands.add('login', (user, password) => {
if (!user || !password) {
cy.fixture('users').then((json) => {
var user, password;
user = json[0].email;
password = json[0].password;
login2(user,password);
});
} else {
login2(user, password);
}
function login2(user, password) {
cy.visit('');
cy.contains('Entrar').click();
cy.get('input[name=_username]')
.type(user);
cy.get('input[name=_password]')
.type(password);
cy.get('.btn').click();
}
});
当我设置 user = json[0].email
时,它的值正好在 .then
内,
所以我创建了函数登录来解决这个问题。
我想有更好的方法来做到这一点。有什么想法吗?
赛普拉斯文档提供了使用 Return Values 的指导。最值得注意的是,它提出以下建议:
Return Values
You cannot assign or work with the return values of any Cypress command. Commands are enqueued and run asynchronously.
文档接着解释了 Closures 的使用,以及 .then()
块中的嵌套命令。利用 cypress/support/commands.js 中的这种方法,您可以像这样简化代码:
Cypress.Commands.add("login", (user, pw) => {
let username;
let password;
cy.fixture('default-user') // <-- fixture in a separate file, default-user.js
.then((defaultUser) => {
username = user || defaultUser.username;
password = pw || defaultUser.password;
cy.get('input[name=_username]').type(username);
cy.get('input[name=_password]').type(password);
cy.get('.btn').click();
});
});
作为参考,默认-user.js 看起来像这样:
{
username: 'user@test.com.br',
password: '123321'
}
有关 Aliases and Sharing Context 的 Cypress 文档有助于理解在不同的 Cypress 上下文和用例中引用值的细微差别。
我正在测试 Cypress,我有 custom Cypress command:
的代码Cypress.Commands.add('login', (user, password) => {
if (!user || !password) {
user = 'user@test.com.br';
password = '123321';
}
cy.visit('');
cy.contains('Entrar').click();
cy.get('input[name=_username]')
.type(user);
cy.get('input[name=_password]')
.type(password);
cy.get('.btn').click();
});
如果您在不带任何参数的情况下调用 cy.login()
,则 user
和 password
会在 if
块中分配。否则,它使用传递的参数值。
我试图在这里添加固定装置,并想出了这个代码:
Cypress.Commands.add('login', (user, password) => {
if (!user || !password) {
cy.fixture('users').then((json) => {
var user, password;
user = json[0].email;
password = json[0].password;
login2(user,password);
});
} else {
login2(user, password);
}
function login2(user, password) {
cy.visit('');
cy.contains('Entrar').click();
cy.get('input[name=_username]')
.type(user);
cy.get('input[name=_password]')
.type(password);
cy.get('.btn').click();
}
});
当我设置 user = json[0].email
时,它的值正好在 .then
内,
所以我创建了函数登录来解决这个问题。
我想有更好的方法来做到这一点。有什么想法吗?
赛普拉斯文档提供了使用 Return Values 的指导。最值得注意的是,它提出以下建议:
Return Values
You cannot assign or work with the return values of any Cypress command. Commands are enqueued and run asynchronously.
文档接着解释了 Closures 的使用,以及 .then()
块中的嵌套命令。利用 cypress/support/commands.js 中的这种方法,您可以像这样简化代码:
Cypress.Commands.add("login", (user, pw) => {
let username;
let password;
cy.fixture('default-user') // <-- fixture in a separate file, default-user.js
.then((defaultUser) => {
username = user || defaultUser.username;
password = pw || defaultUser.password;
cy.get('input[name=_username]').type(username);
cy.get('input[name=_password]').type(password);
cy.get('.btn').click();
});
});
作为参考,默认-user.js 看起来像这样:
{
username: 'user@test.com.br',
password: '123321'
}
有关 Aliases and Sharing Context 的 Cypress 文档有助于理解在不同的 Cypress 上下文和用例中引用值的细微差别。