如何在 Cypress 中共享变量值
How to share variable values in Cypress
我正在学习使用 Cypress。此时我有几个spec文件,结构如下:
/cypress
/integration
/child-a
list.spec.js
item.spec.js
/child-b
list.spec.js
item.spec.js
index.spec.js
当测试 运行 开始时,我想设置一个值,一次。该值来自网络 api。出于这个原因,我 不想 在每次测试之前都设置该值。我也不想为每个文件设置它。相反,我只想设置一次。然后我想在所有 spec.js 文件的所有测试 运行 中使用该值。
有没有办法在 Cypress 中做到这一点?有点像 beforeAll
或 beforeSession
?或者有其他推荐的方法吗?我看到的最接近的是 [before][2]
钩子。谢谢。
在所有规格之前执行一次代码:
您可以使用您在 cypress/support/index.js
. =]
// This example support/index.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
// You can read more here:
// https://on.cypress.io/configuration
提示:我正在使用完全相同的挂钩来执行一个脚本,该脚本会在测试前重置我的数据库状态。我在这里无法成功 运行 一些 JS 语句,所以我将它们放在单独的 Node 脚本中并使用 cy.exec()
执行它。我猜你可以从那个脚本中输出你需要的 webapi 值。 cy.exec() 可以读取命令的输出 运行s:
before(() => {
cy.exec("node import-ui-test-data-to-firestore.js", {
failOnNonZeroExit: false
});
});
规范之间共享变量:
我无法真正提供有关跨所有规格共享变量的建议,但您可能会发现这很有帮助 - https://docs.cypress.io/guides/core-concepts/variables-and-aliases.html#Sharing-Context
Cypress fixture 应该适合你!
https://docs.cypress.io/api/commands/fixture.html#Syntax
// cypress/fixtures/user.json
{
"username": "tj",
"password": "password"
}
// cypress/integration/login_test.js
cy.fixture('users').as('usersJson') // load data from users.json
我有一个类似的问题,但我希望该变量是动态的。例如,我希望我创建的测试用户在其电子邮件中有一个日期,例如 test+1575177520@example.com。
要做到这一点并在所有测试中重新使用它,我想使用以下代码,但无法使用夹具,因为 JSON 不可执行:
test+${new Date().getTime()}@example.com
我发现 cypress/support/index.html
很有用 (但我认为您也可以在规范中初始化变量)。通过将以下行添加到文件中,我能够在我的测试中的任何地方重用该动态变量,并且它对于每个柏树都是唯一的 运行.
// support/index.js
//...
Cypress.config('email', `test+${new Date().getTime()}@example.com`)
然后在我的测试中
Cypress.config('email')
我正在学习使用 Cypress。此时我有几个spec文件,结构如下:
/cypress
/integration
/child-a
list.spec.js
item.spec.js
/child-b
list.spec.js
item.spec.js
index.spec.js
当测试 运行 开始时,我想设置一个值,一次。该值来自网络 api。出于这个原因,我 不想 在每次测试之前都设置该值。我也不想为每个文件设置它。相反,我只想设置一次。然后我想在所有 spec.js 文件的所有测试 运行 中使用该值。
有没有办法在 Cypress 中做到这一点?有点像 beforeAll
或 beforeSession
?或者有其他推荐的方法吗?我看到的最接近的是 [before][2]
钩子。谢谢。
在所有规格之前执行一次代码:
您可以使用您在 cypress/support/index.js
. =]
// This example support/index.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
// You can read more here:
// https://on.cypress.io/configuration
提示:我正在使用完全相同的挂钩来执行一个脚本,该脚本会在测试前重置我的数据库状态。我在这里无法成功 运行 一些 JS 语句,所以我将它们放在单独的 Node 脚本中并使用 cy.exec()
执行它。我猜你可以从那个脚本中输出你需要的 webapi 值。 cy.exec() 可以读取命令的输出 运行s:
before(() => {
cy.exec("node import-ui-test-data-to-firestore.js", {
failOnNonZeroExit: false
});
});
规范之间共享变量: 我无法真正提供有关跨所有规格共享变量的建议,但您可能会发现这很有帮助 - https://docs.cypress.io/guides/core-concepts/variables-and-aliases.html#Sharing-Context
Cypress fixture 应该适合你!
https://docs.cypress.io/api/commands/fixture.html#Syntax
// cypress/fixtures/user.json
{
"username": "tj",
"password": "password"
}
// cypress/integration/login_test.js
cy.fixture('users').as('usersJson') // load data from users.json
我有一个类似的问题,但我希望该变量是动态的。例如,我希望我创建的测试用户在其电子邮件中有一个日期,例如 test+1575177520@example.com。
要做到这一点并在所有测试中重新使用它,我想使用以下代码,但无法使用夹具,因为 JSON 不可执行:
test+${new Date().getTime()}@example.com
我发现 cypress/support/index.html
很有用 (但我认为您也可以在规范中初始化变量)。通过将以下行添加到文件中,我能够在我的测试中的任何地方重用该动态变量,并且它对于每个柏树都是唯一的 运行.
// support/index.js
//...
Cypress.config('email', `test+${new Date().getTime()}@example.com`)
然后在我的测试中
Cypress.config('email')