javascript - How to use variables in Cypress - Stack Overflow

I'm trying to store Cypress element in variable and re-use it instead of calling get() again.My fi

I'm trying to store Cypress element in variable and re-use it instead of calling get() again.


My first unsuccessful attempt:

// works
const wrap = cy.get(`wrapSelector`)
wrap.should('be.visible')

// works
const input = wrap.find(`input`)
input.should('be.focused');
input.type(string);

// wrap is now `input` (line 6) instead of `wrapSelector` (line 2)
const button = wrap.find('button').click();
// @error: button not found in `input`

So I've read Cypress closures and I've tried using then(). But it returns jQuery object.

cy.get(`someSelector`).then($wrap=>{
    // now I would like to test $wrap 
    // but $wrap is now jQuery object so I can't do this:
    $wrap.should('be.visible')
    // ... some code
})

It could be working if I would use whole selector with children but I am not sure if it's best practice

cy.get(`wrapSelector`).should('be.visible')
cy.get(`wrapSelector input`).should('be.focused')
cy.get(`wrapSelector input`).type(string);
cy.get(`wrapSelector button`).click()
// works

I know that I can test jQuery object too (for example with expect()) but I would like to keep using should(). What is the best practice when I would like to re-use some already fetched elements? Ideal solution on this example would be the best.

I'm trying to store Cypress element in variable and re-use it instead of calling get() again.


My first unsuccessful attempt:

// works
const wrap = cy.get(`wrapSelector`)
wrap.should('be.visible')

// works
const input = wrap.find(`input`)
input.should('be.focused');
input.type(string);

// wrap is now `input` (line 6) instead of `wrapSelector` (line 2)
const button = wrap.find('button').click();
// @error: button not found in `input`

So I've read Cypress closures and I've tried using then(). But it returns jQuery object.

cy.get(`someSelector`).then($wrap=>{
    // now I would like to test $wrap 
    // but $wrap is now jQuery object so I can't do this:
    $wrap.should('be.visible')
    // ... some code
})

It could be working if I would use whole selector with children but I am not sure if it's best practice

cy.get(`wrapSelector`).should('be.visible')
cy.get(`wrapSelector input`).should('be.focused')
cy.get(`wrapSelector input`).type(string);
cy.get(`wrapSelector button`).click()
// works

I know that I can test jQuery object too (for example with expect()) but I would like to keep using should(). What is the best practice when I would like to re-use some already fetched elements? Ideal solution on this example would be the best.

Share asked Feb 27, 2020 at 14:17 Jax-pJax-p 8,6636 gold badges33 silver badges67 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

You should be using aliases as mentioned by Cypress here: https://docs.cypress.io/guides/core-concepts/variables-and-aliases.html#Sharing-Context

because of the use of promises, Cypress methods do not return values directly. Aliasing allows you to:

cy.get(element).as("myAlias")
this.myAlias // works
cy.get("@myAlias") // works

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745043512a4607954.html

相关推荐

  • javascript - How to use variables in Cypress - Stack Overflow

    I'm trying to store Cypress element in variable and re-use it instead of calling get() again.My fi

    8小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信