问题
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 JavascriptExecutor? For example lets take this simple piece of code
input id="mapsearch" type="textbox" class="form-control" name="address" test=""
Is it possible to get the test attribute and assign my own value?
回答1:
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')
来源:https://stackoverflow.com/questions/47131842/cypress-set-attribute-value