简体   繁体   中英

How to prevent opening new browser tab for the list of elements?

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?

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)
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM