[英]Cypress Cucumber, how Get to data from page in one step and use it another scenario step
Below is mine scenario where in one scenario I'm getting data from page and saving it in a variable as alias.下面是我的场景,在一个场景中,我从页面获取数据并将其作为别名保存在变量中。 tHen I want to use same variable/data in other scenario to put in an input field.I'm using Alias but getting this error.
然后我想在其他场景中使用相同的变量/数据放入输入字段。我正在使用 Alias 但出现此错误。
cy.wait() could not find a registered alias for: @Orderinfo. cy.wait() 找不到注册的别名:@Orderinfo。 You have not aliased anything yet.
你还没有为任何东西起别名。
Even it alliased properly.即使它正确地关联了。 Data stores in @Orderinfo but not accessable in other sceanrio step.
数据存储在@Orderinfo 中,但在其他 sceanrio 步骤中不可访问。
Then("Get Data from page", () => {
cy.get(".os-order-number").invoke("text").then(($Oid) => {
let Order = $Oid.text();
let Order_id = Order.replace(/[^0-9]/g, "");
cy.wrap(Order_id).as("Orderinfo");
});
});
Given("Go to Login", () => {
cy.visit("https://dev.simplifyshopping.com/register/");
});
When("Paste variable here", () => {
cy.wait(2000);
cy.wait("@Orderinfo")
cy.get("@Orderinfo")).then((Orderinfo) => {
console.log(Orderinfo);
cy.get("#id_email").type(Orderinfo);
});
});
So both, the use across several steps of the same scenario, as well as scenario overlapping are possible with Cypress using Cucumber Preprocessor.因此,赛普拉斯使用 Cucumber 预处理器,跨同一场景的多个步骤使用,以及场景重叠都是可能的。
1. Use of values across multiple steps of the same scenario 1. 跨同一场景的多个步骤使用值
Referring to the example from the question, the Order_Id
can be defined outside the steps and is thus accessible in the global scope from all steps.参考问题中的示例,
Order_Id
可以在步骤之外定义,因此可以从所有步骤在全局 scope 中访问。 If I understood the code correctly, it would be something like this (probably unnecessary code commented out):如果我正确理解代码,它会是这样的(可能不需要的代码被注释掉了):
let Order_id;
Then("Get Data from page", () => {
cy.get(".os-order-number").invoke("text").then(($Oid) => {
let Order = $Oid.text();
Order_id = Order.replace(/[^0-9]/g, "");
// cy.wrap(Order_id).as("Orderinfo");
});
});
Given("Go to Login", () => {
cy.visit("https://dev.simplifyshopping.com/register/");
});
When("Paste variable here", () => {
cy.wait(2000);
// cy.wait("@Orderinfo")
// cy.get("@Orderinfo")).then((Orderinfo) => {
// console.log(Orderinfo);
// cy.get("#id_email").type(Orderinfo);
// });
console.log(Order_id);
cy.get("#id_email").type(Order_id);
});
2. Use of values across scenarios (hold state across tests) 2.跨场景取值(hold state跨测试)
To make certain values accessible across the execution of different scenarios, for example, a helper.js
file can be created containing the following code:例如,为了在不同场景的执行过程中访问某些值,可以创建一个包含以下代码的
helper.js
文件:
export const stateStore = {};
Inside your step definition files, you can then import the stateStore
and fill it with values as you like:在你的步骤定义文件中,你可以导入
stateStore
并用你喜欢的值填充它:
import { Given, When } from 'cypress-cucumber-preprocessor/steps';
import { stateStore } from '../helpers';
// step used in first scenario
Given('some value is made available in scenario 1', () => {
stateStore.someValue = 'this is a value';
});
// step used in second scenario
When('this value can be used in another step of scneario 2', () => {
console.log(`Print some value: ${stateStore.someValue}`);
});
It can be done using "alias".as in cypress.它可以使用“别名”来完成。就像在赛普拉斯中一样。 Store variable.as("vaiableName") and then access it in required function as this.variableName.
存储 variable.as("vaiableName") 然后在 required function 中访问它作为 this.variableName。 It could be like this.
可能是这样的。
Then("Get Data from page", function () {
cy.get(".os-order-number").then($Oid => {
const Order = $Oid.text()
const Order_id = Order.replace(/[^0-9]/g, "")
cy.log("inside Then function" + Order_id)
cy.wrap(Order_id).as("wrapText")
})
})
Given("Go to Login", function () {
cy.log(this.wrapText)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.