Cypress: Changed variable value after visit new page

Created on 17 Oct 2018  路  3Comments  路  Source: cypress-io/cypress

Is this a Feature or Bug?

Current behavior:

I wrote a function makeid() that generates random id.

I'm calling it and assigning to a var, the problem is that i'm visiting two pages in this test. And after second visit var is getting new value

It looks like that:

var email = utils.makeId();

describe('create new account', function(){
    it('go to localhost', function(){
        cy.visit('localhost');
        cy.get('#email').type(email);
    })

    it('go to external app'), function() {
        cy.visit('external app');
        cy.get('#externalapp_email').type(email);
    }
})

Before visit in second app the new value is assigning to var email

Desired behavior:

I want to use the same value for email variable for both tests.

Steps to reproduce:

up

Versions

3.1.0

Most helpful comment

This is a good use-case for cy.task(). The browser gets reloaded when you visit a different domain, so it's not possible to store state between tests in your spec file because it gets reloaded along with the browser. However, the background process run by the plugins file will persist and not be reloaded. You can create your random id there, and use cy.task() to get it for both tests, like so:

// in test
describe('create new account', function(){
  it('go to localhost', function(){
    cy.visit('localhost');
    cy.task('get:email').then(function(email) {
      cy.get('#email').type(email);
    })
  })

  it('go to external app'), function() {
    cy.visit('external app');
    cy.task('get:email').then(function(email) {
      cy.get('#externalapp_email').type(email);
    })
  })
})

// in plugins/index.js
var utils = require('./path/to/utils')

module.exports = function(on){
  var email = utils.makeId()

  on('task', {
    'get:email': function(){
      return email
    }
  })
}

All 3 comments

This is a good use-case for cy.task(). The browser gets reloaded when you visit a different domain, so it's not possible to store state between tests in your spec file because it gets reloaded along with the browser. However, the background process run by the plugins file will persist and not be reloaded. You can create your random id there, and use cy.task() to get it for both tests, like so:

// in test
describe('create new account', function(){
  it('go to localhost', function(){
    cy.visit('localhost');
    cy.task('get:email').then(function(email) {
      cy.get('#email').type(email);
    })
  })

  it('go to external app'), function() {
    cy.visit('external app');
    cy.task('get:email').then(function(email) {
      cy.get('#externalapp_email').type(email);
    })
  })
})

// in plugins/index.js
var utils = require('./path/to/utils')

module.exports = function(on){
  var email = utils.makeId()

  on('task', {
    'get:email': function(){
      return email
    }
  })
}

Thank you very much - it works :)

This is a good use-case for cy.task(). The browser gets reloaded when you visit a different domain, so it's not possible to store state between tests in your spec file because it gets reloaded along with the browser. However, the background process run by the plugins file will persist and not be reloaded. You can create your random id there, and use cy.task() to get it for both tests, like so:

// in test
describe('create new account', function(){
  it('go to localhost', function(){
    cy.visit('localhost');
    cy.task('get:email').then(function(email) {
      cy.get('#email').type(email);
    })
  })

  it('go to external app'), function() {
    cy.visit('external app');
    cy.task('get:email').then(function(email) {
      cy.get('#externalapp_email').type(email);
    })
  })
})

// in plugins/index.js
var utils = require('./path/to/utils')

module.exports = function(on){
  var email = utils.makeId()

  on('task', {
    'get:email': function(){
      return email
    }
  })
}
Was this page helpful?
0 / 5 - 0 ratings