4

Is it possible to pass a variable from one it test to the next it test ? The following using cy.wrap does not work:

it('test1', () => {
 const var1 = 'test'
 cy.wrap(var1).as('var1Alias')
})
it('test2', () => {
 cy.get('@var1Alias').then(var1Alias => {
    // do stuff with the var1Alias
  })
})

I've checked old stackoverflow questions similar to mine such as this: Use variables across multiple 'it' statements to track change in Cypress and the difference is that the variable is declared globally outside the tests (it). Hence, you can always replace the variable.

My specific issue is that the dependency on the variable's value from the previous test. I know Cypress best practice suggests that tests shouldn't be sequential but this is a common scenario in my opinion to properly categorise tests in a readable manner.

For context, my first test is calling a POST endpoint then the response body will be passed as a query parameter for my GET endpoint in the succeeding test.

Current workarounds (while I am still looking for a better option):

  • Use fixtures and write then retrieve (slower runtime)
  • Use localstorage-commands plugin (I have multiple variables to retrieve)
  • Include the succeeding test inside the arrow function of the previous test (thus combination both tests in one test)
ebanster
  • 886
  • 1
  • 12
  • 29
  • https://docs.cypress.io/guides/core-concepts/variables-and-aliases.html#Sharing-Context? – jonrsharpe Jan 01 '21 at 09:15
  • Checked this but the wrap / invoke / alias is done on the beforeEach / before level here. What I tried is using wrap or invoke from one test (`it`) as I needed that variable to pass to my next test (`it`). As per above, it does not work. – ebanster Jan 01 '21 at 09:28
  • Sorry, I copied the `cy.log` just to see if the value is picked up. I have updated my original post. I also missed the statement of Aliases being cleared down so thanks for that. I might as well go with closure variables and group the tests together. The initial intent was to separate them so it looks neat on the test results / report that I generate. **Hence, you can replace the variable** - what I'm trying to mean here is that it can always be changed per test (without dependency on each tests) and what I really wanted is that the value from test 1 is picked up in test 2. – ebanster Jan 01 '21 at 12:18
  • Cleaning down everything between tests is a way to ensure nothing "bleeds" over from one test to another causing false positives - admittedly rare but could happen and Cypress takes the path of caution. –  Jan 01 '21 at 22:32
  • 2
    The other thing to be aware of, if tests are run ***in parallel*** (for speed, as with Cypress Dashboard) they can run in any order, so passing something from one test to another doesn't work. I guess that's why `before()` exists - it's guaranteed to run, well, before. –  Jan 01 '21 at 22:35
  • Giving context to why these things are the way they are is helpful. Thanks for this. The parallelisation part makes a lot of sense on running tests in a way, asynchronously. – ebanster Jan 03 '21 at 10:57

2 Answers2

3

If your it() tests share the same domain, you should use traditional function declaration "function() {do smth}" so you can use this. context, just right after the describe() define let variable with no value. After that in .then() inside your test "it()" you can redefine variable value like this.str = value; This value will be saved and can be passed to other it();

Example:

describe('test', function() {
  let str;
  it('take value from input', () => {
    cy.visit('/');

    cy.contains('Forms').click();
    cy.contains('Form Layouts').click();
    cy.contains('nb-card', 'Using the Grid').find('[data-cy="imputEmail1"]').type('someemil@email.com').invoke('val').then((value) => {
      this.str = value;
    });
  });

  it('put value', () => {
    cy.visit('/');
    cy.log(this.str);
  });
});
Yurii Ya
  • 139
  • 1
  • 4
2

Although this is not suggested by the Cypress team, we can't assume a one-size-fits-all approach. Following Paul's instructions, you could add Getter and Setter Tasks to the Plugins/Index.js file, like such:

on('task', {
   setVar1(val) {
     return (storeVariable= val);
   },
   getVar1(){
     return storeVariable;
   }
})

(Please note that Paul suggests you place the above within the "Support" index,js, this is wrong. Place it within the "plugins" index.js; furthermore, place it within the "module.exports" function.) Now you can add cy.task('setVar1 ', var1); to your first Test, and then the following within your second Test:

cy.task('getVar1').then((var1) => {
  //do stuff with var1 here
});
Kyle Burriss
  • 721
  • 1
  • 5
  • 11