Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
How can I continue in same tab?

I am dealing with a pretty tricky button that is supposed to open a new document. Per default it opens the document in a new tab, but I need to use it so it opens the URL in the same page. Unfortunately this is no href element for which there are several ways to deal with in Cypress. Instead the button looks like this:

<button class="header-add-button k-button">
    <svg class="svg-inline--fa fa-plus fa-w-14" aria-hidden="true" focusable="false" data-prefix="fad" data-icon="plus" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" data-fa-i2svg="">
        <g class="fa-group">
            <path class="fa-secondary" fill="currentColor" d="M176 448a32 32 0 0 0 32 32h32a32 32 0 0 0 32-32V304h-96zm64-416h-32a32 32 0 0 0-32 32v144h96V64a32 32 0 0 0-32-32z"></path>
            <path class="fa-primary" fill="currentColor" d="M448 240v32a32 32 0 0 1-32 32H32a32 32 0 0 1-32-32v-32a32 32 0 0 1 32-32h384a32 32 0 0 1 32 32z"></path>
        </g>
    </svg>
    <!-- <i class="fad fa-plus"></i> -->
    <span class="span_hover_effect" style="top: 8.9375px; left: 8.28125px;"></span>
</button>

I can click this button in Cypress, but using the methods for dealing with opening href links in the same tab do not apply here.

This is the code I tried applying one of the methods I found when searching for continuing in the same tab.

cy.get('.header-add-button').invoke('removeAttr', 'target', '_blank').click()

It clicks the button, but the page still opens in a new tab.

{edit} According to the dev the button redirects you via AJAX call. Not sure what to make of it, but that's all the answer I got.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can check your the function is called when you click on that button. You'll have to look into the javascript to see the name for your app. I'm not sure how you could get the url if you need to do more testing on the new tab, but basically it will look like this:

cy.visit('./index.html') 
cy.window().then((win) => {
  // spy on 'open' function, yours might be called differently
  cy.spy(win, 'open').as('redirect');
  // or stub on 'open' function so no actually redirect occurs
  // cy.stub(win, 'open').as('redirect');
})



// click on button with ajax call

cy.get('@redirect')
  .should('be.calledWith', '_blank', '/about');

References:

filip Hric blog

cypress recipe example

about 4 years ago · Juan Pablo Isaza Report

0

Combining jjhelguero's answer with this link I found the solution!

    it('add contract ', function () {
      cy.window().then(win => {
         cy.stub(win, 'open').as('open')
      })
      cy.get('.header-add-button').click()
      cy.get('@open').should('be.calledWith', Cypress.sinon.match.string).then(stub => {
         cy.visit(Cypress.env('url') + stub.args[0][0]);
         stub.restore;
      })
   })

This opens the desired link in the same tab so I can continue testing :)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!