I have a test case in which I have a link which opens in a new tab. Since Cypress doesn’t support multiple tabs, I want to get the href
attribute of that link and then open it in the same tab. I’m trying to do it this way, but for some reason it doesn’t work.
it('Advertise link should refer to Contact page', () => {
var link = document.querySelector("div.footer-nav > ul > li:nth-child(2) > a").href;
cy.visit(link);
cy.title().should('include', 'Text');
});
The code below should do what you’re trying to achieve. There is also an entire recipe with suggestions on how to test links that open in new tabs.
I would also suggest reading through the Cypress document on the best ways to assign and work with variables: https://on.cypress.io/variables-and-aliases
Found a solution, This works to check a count of items:
This does not work with the
Cypress.$()
method of notation.Reference: https://docs.cypress.io/guides/references/assertions.html#Length