I am currently working on Cypress tests and encountered an issue to which I cannot find the answer. Hopefully someone can help.
So, in ReactJs I have the following component that I want to test:
<Link
to={`/example/${exampleId}/subexample/create`}
className={classes.linkIcon}
title="Create subexample"
data-test="new-subexample"
>
To test this piece, in Cypress I am first creating a new example
and that works fine, but then to test the subexample
gives me problems because data-test="new-subexample"
is not doing what I am expecting (i.e. to click on the /example/:exampleId/subexample/create
link). Here is some code:
cy.get("[data-test=new-subexample]").click()
const subexample = faker.lorem.paragraph().substring(0, 200);
......
cy.get('[data-test="submit-subexample"]').click();
cy.get(".toast--success").should("be.visible");
});
Did anyone encountered this before and how did you manage to click on the links where the id is always changing?
Also, maybe one approach would be to grab the :exampleId
after example
has been created (also via Cypress), but I cannot find anywhere how to get the element from the URL using Cypress.
Thanks in advance.
UPDATE: The problem was the redirect which took some time and never completed in time for the test to pass. I added a wait (using cy.wait()) time before the redirect and now the test passed.