最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing a variable's value from a test to the next one in Cypress - Stack Overflow

programmeradmin1浏览0评论

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 mon 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-mands plugin (I have multiple variables to retrieve)
  • Include the succeeding test inside the arrow function of the previous test (thus bination both tests in one test)

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 mon 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-mands plugin (I have multiple variables to retrieve)
  • Include the succeeding test inside the arrow function of the previous test (thus bination both tests in one test)
Share Improve this question edited Jan 1, 2021 at 12:16 ebanster asked Jan 1, 2021 at 9:10 ebansterebanster 1,0863 gold badges15 silver badges32 bronze badges 6
  • docs.cypress.io/guides/core-concepts/…? – jonrsharpe Commented Jan 1, 2021 at 9: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 Commented Jan 1, 2021 at 9: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 Commented Jan 1, 2021 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. – user8745435 Commented Jan 1, 2021 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. – user8745435 Commented Jan 1, 2021 at 22:35
 |  Show 1 more ment

3 Answers 3

Reset to default 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('[email protected]').invoke('val').then((value) => {
      this.str = value;
    });
  });

  it('put value', () => {
    cy.visit('/');
    cy.log(this.str);
  });
});

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
});

A bit late to respond but could help others, the way I done it was to use cy.write and created a json fixture file with the data in it

eg first test would have this line of code in:

cy.writeFile('cypress/fixtures/data.json', { Title: title, Desc: desc });

the second test would use this fixture file

cy.fixture('data.json').then(dataExtract=> {
      cy.get('#input').type(dataExtract.Title)
发布评论

评论列表(0)

  1. 暂无评论