admin管理员组

文章数量:1287580

This is my cypress code:

 cy.get(`${sitestovisit.searchBoxFormID} > form`)
   .should('have.attr', 'target')<br>
   .invoke('removeAttr', 'target')

sitestovisit.searchBoxFormId contains data from JSON and its working properly but it shows that there is a target attribute in form, but when I tried to remove it, it is not working.

and when i replace above code with:

 cy.get('#booking_search > form')
  .invoke('removeAttr', 'target')

it works fine, what's the problem? i can't use directly cause I need it in loop Here's the output

This is my cypress code:

 cy.get(`${sitestovisit.searchBoxFormID} > form`)
   .should('have.attr', 'target')<br>
   .invoke('removeAttr', 'target')

sitestovisit.searchBoxFormId contains data from JSON and its working properly but it shows that there is a target attribute in form, but when I tried to remove it, it is not working.

and when i replace above code with:

 cy.get('#booking_search > form')
  .invoke('removeAttr', 'target')

it works fine, what's the problem? i can't use directly cause I need it in loop Here's the output

Share Improve this question edited May 2, 2023 at 11:37 Haast 1817 bronze badges asked Dec 2, 2019 at 16:11 Biki MaharjanBiki Maharjan 3981 gold badge3 silver badges11 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8

Is failing because you have this assertion .should('have.attr', 'target') before this invocation .invoke('removeAttr', 'target').

The .should('have.attr', 'target') changes the subject from the element to the attribute, but .invoke('removeAttr', 'target') requires the subject to be the element in order to work.

this will work

 cy.get(`${sitestovisit.searchBoxFormID} > form`)
   .invoke('removeAttr', 'target')

And.. if you need to see if the target attribute exist before deleting it I would do this.

  cy
      .get(`${sitestovisit.searchBoxFormID} > form`)
      .should('have.attr', 'target')

  cy
       .get(`${sitestovisit.searchBoxFormID} > form`)
       .invoke('removeAttr', 'target')

I´ll would use a then statement on the element yield by cy.get:

cy.get(`${sitestovisit.searchBoxFormID} > form`)
  .then( $elem => {
    $elem[0].removeAttribute('target');
  })

Got this solution in Github thread. It should be added before you click the link.

cy.window().then((win) => {
  const orig = win.open

  win.open = function (url, target, features) {
    return orig.call(this, url, '_self', features)
  }
})

本文标签: javascriptcypress invoke(39removeAttr3939target39) not workingStack Overflow