Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch URL after click with Cypress (window.location changes)

I would like to know how to capture and read the URL after a click event on an <a> link.

On the onClick event our javascript does some string manipulation of the actual href of the clicked link and then a window.location.href = myNewReplacebleURL is done on the fly. The original href is not necessarily the location you get to after the onClick.

Here is how I started:

describe("Twitter", function() {

  it("Should assert that via value is set correctly in JS", function() {

    cy.server();
    cy.visit(Cypress.config("appUrl") + "/probes/sha/sha-via.html");
    cy.get("#v_test ul.share li a")
      .click();

  });

});

EDIT: What I would like is to catch the URL located at the "Page Load" step.

enter image description here

like image 932
Radu Chiriac Avatar asked Feb 01 '19 15:02

Radu Chiriac


People also ask

What does Cy URL return?

cy. url() exists because it's what most developers naturally assume would return them the full current URL.

How do you click on a button in Cypress?

click() is used to trigger the click event. When you trigger the click event using Cypress, the cypress tries to click on the center of the element by default however you can alter this default behavior by passing the position argument to click(). List of valid click() positions in Cypress is as follows: topLeft.


1 Answers

You should be able to use cy.on to perform your assertions inside the url:changed event callback:

cy.on("url:changed", (newUrl) => {
  expect(newUrl).to.contain("?magic=true")
})
like image 157
Guillaume Robbe Avatar answered Sep 18 '22 13:09

Guillaume Robbe