Search code examples
cypresscypress-cucumber-preprocessor

Cypress: Check if a newly opened tab has the correct URL


I am using Cypress Cucumber for test automation. I want to check that a newly opened tab has the expected URL.

The .feature file has been written out in the Cucumber framework, and looks like the text below.

When I click on "[Hyperlink on website]"
Then A new tab should be opened with the URL "[New tab URL string]"

In the .js file I currently have what is shown in the image below. This checks the URL of the current tab, but I want to check the URL of the newly opened tab. What do I need to add to the Cypress command to do this? CypressExample.js


Solution

  • Cypress doesn't support multiple tabs as mentioned here. We can get the href of the hyperlink and open the link in the same tab.

     cy.get(<Link locator>)
      .should('have.attr', 'href')
      .then((href) => {
        cy.visit(href)
     })
    

    Hope this helps!