在赛普拉斯的测试之间保留动态变量
Keep dynamic variables between tests in Cypress
我想做一个赛普拉斯测试,我想在输入中设置一个随机数,然后检查随机数是否在另一个页面中设置正确。
我正在使用此函数创建随机数:
function getRandomArbitrary(min, max, decimals) {
return (Math.random() * (max - min) + min).toFixed(decimals);
}
并在描述中设置变量:
describe('Reflect changes from X to the Y', () => {
const termCap = getRandomArbitrary(0.01, 0.3, 2);
const termCapFee = getRandomArbitrary(0.01, 0.3, 2);
我目前遇到的问题是变量在我创建的每个不同的 it() 中都会重置。而且我不知道会发生什么,因为我想在所有测试中保持相同的一致数字。
我试过在 before() 中设置它们,但这也不起作用。
有谁知道我应该如何创建变量?
I've tried setting them in a before() but that didn't work either.
before
块应该可以工作。也许 before
块设置不正确?我可以使用以下设置让它工作。
describe("Foo Fighter", function() {
var foo;
before(function () {
foo = getRandomArbitrary(1,10,1);
});
it("Fighting foo 1", function () {
cy.log(foo);
});
it("Fighting foo 2", function () {
cy.log(foo);
});
it("Fighting foo 3", function () {
cy.log(foo);
});
});
function getRandomArbitrary(min, max, decimals) {
return (Math.random() * (max - min) + min).toFixed(decimals);
}
产生以下结果,随机数在每个 it
区块中保持不变。
逻辑流程为:
- 在
describe
块中,声明您的变量。
- 在
before
块中,使用随机数设置变量。
- 在
it
块中,使用set变量。
编辑:
回答您的评论:
If I set it like you it works fine, but imagine if in the first it you visit google, and in the second it you visit github, the before block will run twice
这里的问题是,当您访问一个新站点时,赛普拉斯会重新加载整个测试上下文,因此 before
块,就像您提到的,每次测试上下文都会再次 运行重新加载(即每次访问新域时)。
解决这个问题的方法是通过先设置另一个描述 运行s 并将变量写入固定装置并在测试中使用这些固定装置来作弊,如下所示:
describe("Before Describe", function(){
const foo = getRandomArbitrary(1,10,1);
it("Setting up test context", function() {
cy.writeFile("cypress/fixtures/test.json", { "foo" : foo});
});
});
describe("Foo Fighter", function() {
it("Fighting foo 1", function () {
cy.visit("https://example.cypress.io");
cy.fixture("test.json").then(kung => {
cy.log(kung.foo);
})
});
it("Fighting foo 2", function () {
cy.visit("https://google.com")
cy.fixture("test.json").then(kung => {
cy.log(kung.foo);
})
});
it("Fighting foo 3", function () {
cy.fixture("test.json").then(kung => {
cy.log(kung.foo);
})
});
});
function getRandomArbitrary(min, max, decimals) {
return (Math.random() * (max - min) + min).toFixed(decimals);
}
这将产生以下结果:
诚然,这不是构建测试流程的最简洁方法,但是,它会产生您想要的结果。如果您将设置放在与其他测试相同的 describe
中最先出现的 it
中,也应该有效。
我想做一个赛普拉斯测试,我想在输入中设置一个随机数,然后检查随机数是否在另一个页面中设置正确。 我正在使用此函数创建随机数:
function getRandomArbitrary(min, max, decimals) {
return (Math.random() * (max - min) + min).toFixed(decimals);
}
并在描述中设置变量:
describe('Reflect changes from X to the Y', () => {
const termCap = getRandomArbitrary(0.01, 0.3, 2);
const termCapFee = getRandomArbitrary(0.01, 0.3, 2);
我目前遇到的问题是变量在我创建的每个不同的 it() 中都会重置。而且我不知道会发生什么,因为我想在所有测试中保持相同的一致数字。
我试过在 before() 中设置它们,但这也不起作用。
有谁知道我应该如何创建变量?
I've tried setting them in a before() but that didn't work either.
before
块应该可以工作。也许 before
块设置不正确?我可以使用以下设置让它工作。
describe("Foo Fighter", function() {
var foo;
before(function () {
foo = getRandomArbitrary(1,10,1);
});
it("Fighting foo 1", function () {
cy.log(foo);
});
it("Fighting foo 2", function () {
cy.log(foo);
});
it("Fighting foo 3", function () {
cy.log(foo);
});
});
function getRandomArbitrary(min, max, decimals) {
return (Math.random() * (max - min) + min).toFixed(decimals);
}
产生以下结果,随机数在每个 it
区块中保持不变。
逻辑流程为:
- 在
describe
块中,声明您的变量。 - 在
before
块中,使用随机数设置变量。 - 在
it
块中,使用set变量。
编辑:
回答您的评论:
If I set it like you it works fine, but imagine if in the first it you visit google, and in the second it you visit github, the before block will run twice
这里的问题是,当您访问一个新站点时,赛普拉斯会重新加载整个测试上下文,因此 before
块,就像您提到的,每次测试上下文都会再次 运行重新加载(即每次访问新域时)。
解决这个问题的方法是通过先设置另一个描述 运行s 并将变量写入固定装置并在测试中使用这些固定装置来作弊,如下所示:
describe("Before Describe", function(){
const foo = getRandomArbitrary(1,10,1);
it("Setting up test context", function() {
cy.writeFile("cypress/fixtures/test.json", { "foo" : foo});
});
});
describe("Foo Fighter", function() {
it("Fighting foo 1", function () {
cy.visit("https://example.cypress.io");
cy.fixture("test.json").then(kung => {
cy.log(kung.foo);
})
});
it("Fighting foo 2", function () {
cy.visit("https://google.com")
cy.fixture("test.json").then(kung => {
cy.log(kung.foo);
})
});
it("Fighting foo 3", function () {
cy.fixture("test.json").then(kung => {
cy.log(kung.foo);
})
});
});
function getRandomArbitrary(min, max, decimals) {
return (Math.random() * (max - min) + min).toFixed(decimals);
}
这将产生以下结果:
诚然,这不是构建测试流程的最简洁方法,但是,它会产生您想要的结果。如果您将设置放在与其他测试相同的 describe
中最先出现的 it
中,也应该有效。