Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass data from one step to the next synchronously

Tags:

Running Cypress 3.1.1 with cypress-cucumber-preprocessor 1.5.1. I need to pass some static data from one step to another (in the same scenario/test). I can do this using an alias, like this: cy.wrap(someString).as('myString'), but then I have to access it asynchronously:

cy.get('@myString').then(myString => ...)

This is rather cumbersome, particularly when I have to pass multiple values, requiring multiple wrapped closures, for no apparent benefit. (Currently I'm working around this by aliasing an object, but I shouldn't need to do this.)

How can I pass primitive values from one step to another synchronously?

I thought I might be able to simply set this.myString='' to set the value on the Mocha shared context object, but in that case, the property exists but is set to undefined when accessed in later steps.

Even creating my own context variable with let outside of the step definition does not work. Is this simply a limitation of Cypress and/or the cypress-cucumber-preprocessor?

like image 860
hackel Avatar asked Nov 19 '18 20:11

hackel


1 Answers

I managed to get it working the following way:

  1. Add 2 tasks to the /plugins/index.js
const testStore = {}
    module.exports = (on, config) => {
      on('task', {
        pushValue({ name, value }) {
          console.log(name, value)
          testStore[name] = value
          console.log(testStore)
          return true
        },
      })
      on('task', {
        getValue(name) {
          return testStore[name]
        },
      })
  1. Then you can add a variable in any test and reach it in any other place:
it('test', ()=>{
   cy.task('pushValue', { name: 'orderNumber', value: orderNumber })
})
it('test 2', ()=>{
    cy.task('getValue', 'orderNumber').then((order) => {
      cy.visit(`/bookings/${order}`)
    })   
})
like image 119
Артем Шойко Avatar answered Nov 15 '22 06:11

Артем Шойко