Cypress: set attribute value

后端 未结 1 1535
死守一世寂寞
死守一世寂寞 2021-01-17 13:02

I have just started exploring Cypress and came across such a problem: is it possible to select a concrete attribute and change its value like in Selenium with the help of

1条回答
  •  北恋
    北恋 (楼主)
    2021-01-17 13:39

    Yes. Anything you can do in JavaScript is possible within Cypress tests. For the html above, you could do this within Cypress using .invoke():

    cy.get('input[test]')
      .invoke('attr', 'test', 'my new value')
      .should('have.attr', 'test', 'my new value')
    

    Cypress uses jQuery under the hood, so you can invoke any function in jQuery like this. You could alternatively use plain JavaScript after yielding the input using .then():

    cy.get('input[test]').then(function($input){
        $input[0].setAttribute('test', 'my new value')
      })
      .should('have.attr', 'test', 'my new value')
    

    0 讨论(0)
提交回复
热议问题