cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
47.04k stars 3.19k forks source link

REMOVE ATTRIBUTE BLANK DOESN'T WORK #25330

Closed alessandrostassano closed 1 year ago

alessandrostassano commented 1 year ago

Current behavior

When i click a specific button to continue the navigation in our booking application, i use the ".invoke(remove attribute target)" function to denied to the button the possibility to open a new chrome tab. This solution doesn't work because the properties that open new tab is contained in a javascript function, despite the target:blank properties is correctly contained in html element. I put the screen of the chrome dev tools. Is there an alternative workaround to fix this behavior and switch off a javascript function contained the target blank?

Desired behavior

Switch off the blank properties and stay in the current chrome page.

Test code to reproduce

cy.xpath(e_PlpPageElements.PrimoBottoneProsegui).invoke("removeAttr","target").click()

image

In "e_PlpPageElements.PrimoBottoneProsegui" is contained the xpath of the button "Prosegui" that i have to click. image

The properties of Prosegui are these:

image

I switch off target, but it doesnt work. I have to switch off ng-click="openPacketDetails(hotel, ''). Is there a workaround?

Cypress Version

11.0.1

Node version

16.17.0

Operating System

Windows 10

Debug Logs

No response

Other

I attached the screen. The button "prosegui" has these properties, but the function _blank is contained in ng-click=""openPacketDetails"

Cattura
chrisbreiding commented 1 year ago

Can you share the contents of the openPacketDetails function?

If removing target="_blank" doesn't work, my best guess is that openPacketDetailscalls window.open() to open the page in a new tab. In that case, you should be able to patch window.open() to stop it from being called with a _blank target argument.

// after visit, but before click

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

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

// click the link
alessandrostassano commented 1 year ago

Can you share the contents of the openPacketDetails function?

If removing target="_blank" doesn't work, my best guess is that openPacketDetailscalls window.open() to open the page in a new tab. In that case, you should be able to patch window.open() to stop it from being called with a _blank target argument.

// after visit, but before click

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

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

// click the link

Hi, This is the content of the function. I'm not sure what i have to do: the parameters indicated have to be customized? image

chrisbreiding commented 1 year ago

As I suspected, the function uses window.open(). Can you try putting the following code before the relevant click() in your test and see if it fixes the issue?

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

  win.open = function (url, target, features) {
    return orig.call(this, url, '_self', features)
  }
})
alessandrostassano commented 1 year ago

As I suspected, the function uses window.open(). Can you try putting the following code before the relevant click() in your test and see if it fixes the issue?

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

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

It works, thanks a lot!!! What a great workaround!! So grateful

mohd-faisel commented 1 year ago

@chrisbreiding Hey chris, I am stuck at same problem since 3 days, can you help me, I tried above code but that didn't work for me

mohd-faisel commented 1 year ago

Here is my development code, in which they using window.open image

mohd-faisel commented 1 year ago

Here is what i tried in cypress image

mohd-faisel commented 1 year ago

@chrisbreiding please look into my problem, this is very important me to handle this. Thanks in advance

nagash77 commented 1 year ago

@mohd-faisel please open a new issue with a reproducible example and the Cypress team will be happy to investigate. I also want to level set that open source issues have no guarantee around when and even if we will be able to solve them.

If you are looking for other ways to get help using Cypress check out our community chat, it can be helpful for debugging or answering questions on how to use Cypress.