简体   繁体   English

Cypress Cucumber,如何一步从页面获取数据并在另一个场景步骤中使用它

[英]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.

相关问题 错误:步骤实现缺少:[step_definition] with cypress & Cucumber 使用示例场景大纲时 - Error: Step implementation missing for: [step_definition] with cypress & Cucumber when using Scenario outline with example Cucumber JS获取当前功能/场景/进入世界 - Cucumber JS Get current Feature / Scenario / Step in World 有没有办法在12月的方案范围内注入黄瓜步骤变量 - Is there a way to Inject to the cucumber step variable from the scenario scope in cucmber 如何在 Cypress 中编写场景大纲特征的步骤定义文件 - How to write step definition file of scenario outline feature in Cypress cypress cucumber - 如何识别调用步骤定义的功能文件? - cypress cucumber - How to identify the which feature file calling step definition? 如何使用Jquery逐步获取PHP数据? - How can I use Jquery to get PHP data step by step? 上面一步如何获取一个state的数据 - How to get data of a state in one step above Cypress Cucumber Step 运行多步 - Cypress Cucumber Step running multiple steps 如何一步封装一些cucumber步骤? 或在参数中? 还是在标签中? - How to encapsulate some cucumber steps in one step? or in a parameter? or in tag? 使用ajax的3步表单,可将​​数据从一个步骤发送到下一个步骤 - 3 step form using ajax that sends data from one step to the next
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM