Error trying to get attribute from element in Cypress

前端 未结 2 1985
野的像风
野的像风 2020-12-16 10:34

I have this HTML element:



        
相关标签:
2条回答
  • 2020-12-16 11:22

    invoke() calls a jquery function on the element. To get the value of an input, use the function val():

    cy.get('input').invoke('val').should('contain', 'mytext')
    

    This is not the same as getting the value attribute which will not update with user input, it only presets the value when the element renders. To get an attribute, you can use the jquery function attr():

    cy.get('input').invoke('attr', 'placeholder').should('contain', 'username')
    
    0 讨论(0)
  • 2020-12-16 11:32

    Now there is a plugin for your need.

    https://github.com/Lakitna/cypress-commands/blob/develop/docs/attribute.md

    With this, you'll be able to do :

    cy.get('input').attribute('placeholder').should('contain', 'username');
    
    0 讨论(0)
提交回复
热议问题