Smrutiranjana Ray
Smrutiranjana Ray

Reputation: 85

Cypress: How can i use any variable/element of a describe block in different describe block?

//Enter data and add the customer profiles
describe('Enter data to New profiles', function() {
    it('Enter cuatomer details and add the customer profile', function() {

        //Dynamic Customer profile Name

        const todaysDateTime = Cypress.moment().format('MMMDDYYYYSS')
        const profileName = 'ProfileName-' + todaysDateTime
        //Entering the profile Name
        cy.get('input[id="fileDataSubform-portfolio_name"]').type(profileName)
        cy.get('textarea[name="fileDataSubform[customer_list]"]').type('Smith002')
        //clicking the submit button
        cy.get('button[id="pvwButton1"]').click()
    })
})
//I want to use above 'profileName' const value in diffrenet describe block.

describe('Customer details page Navigation', function() {
            it('Click on created customer profile name', function() {
                        cy.get('html > body > div:nth-of-type(1)').then(($elem) => {
                                const x = $elem.text()
                                cy.log(x)
                                if (x.includes(profileName)) {
                                    cy.log("found")
                                    cy.get($elem).click()

                                })
                        })

I have used cypres hook method to solve this but is there anyway, I could able to use the 1 dynamic value of a describe block in other describe block apart from cypress hook concept

Upvotes: 1

Views: 1101

Answers (1)

Zach Bloomquist
Zach Bloomquist

Reputation: 5871

You just need to move profileName outside of your describe so it's in the global scope. Then, it can be accessed from anywhere in the test file.

Read more about Javascript scopes here (it will supercharge your Cypress experience): https://www.w3schools.com/js/js_scope.asp

Something like this should work:

const todaysDateTime = Cypress.moment().format('MMMDDYYYYSS')
const profileName = 'ProfileName-' + todaysDateTime

describe('Enter data to New profiles', function() {
  // ... your tests that use profileName here ...
})

describe('Customer details page Navigation', function() {
  // ... more tests that use profileName here ...
})

Upvotes: 1

Related Questions