0

I am developing Cypress test. And I have <select> element on web page which <option> elements open new page in new browser tab by means of the script containing on the page:

<script>
    $(function () {
        $('#lead-entry-url-dropdown').change(function () {
            var url = $(this).val();
            if (url != null && url != '') {
                window.open(url, "target='_blank'", "noopener");
            }
        });
    });
</script>

Is it possible to remove "target='_blank'" property from the script to avoid opening new browser tab? It is easy to do this for the single element:

cy.get('a').invoke('removeAttr', 'target')

But what about the script? Is it possible to modify it?

vitaliy4us
  • 483
  • 5
  • 21
  • `target='_blank'` *always* opens a new browser tab. Remove that snippet since you had no idea what it meant or what you were doing, in terms of your own words "to avoid opening new browser tab". – GetSet Mar 24 '22 at 22:51
  • That is not now you use a the target in window.open. It should not look like an attribute, it should just be a string. – epascarello Mar 24 '22 at 22:52
  • Are you just trying to override it for test??? – epascarello Mar 24 '22 at 22:54
  • https://stackoverflow.com/questions/51747901/cypress-stub-open-window – epascarello Mar 24 '22 at 22:55

1 Answers1

1

This describes what you want Stub window.open

const stub = cy.stub().as('open')
cy.on('window:before:load', (win) => {
  cy.stub(win, 'open').callsFake(stub)
})