赛普拉斯:如何在不同的describe块中使用describe块的任何变量/元素?

时间:2019-07-08 17:51:18

标签: cypress

//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()

                                })
                        })

我已经使用cypres hook方法解决了这个问题,但是无论如何,除了cypress hook概念之外,我还可以在其他describe块中使用describe块的1动态值

1 个答案:

答案 0 :(得分:1)

您只需要将profileName移到describe之外,这样它就位于全局范围内。然后,可以从测试文件中的任何位置访问它。

在此处了解有关Javascript范围的更多信息(它将增强您的Cypress体验):https://www.w3schools.com/js/js_scope.asp

类似的事情应该起作用:

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