Navigation commands are some which enable the user to navigate to some webpages and also to work with a history like back, forth, reload/refresh.
There are two types of navigation controls namely,
cy.go("back") OR: cy.go(-1): To navigate to the previous page.cy.go("forward") OR cy.go(1): To navigate to the very next page which is saved in the history of the browser. This command can be used only if we have already used the back command.cy.reload() : If we want to refresh or reload the page, cy.reload() command is used.Let us understand how thecy.go() command works with the help of a simple example.
Example program:
To make sure whether the back and the forward function visit the correct URL we are using the assertion along with the command cy.url().
cy.url() command gets the URL of the current webpageback function visits the correct link, we are checking whether the URL contains the substring "dropdown-select-class-in-selenium-webdriver"cy.url().should("include", "dropdown-select-class-in-selenium-webdriver");
cy.go("back");
forward function visits the correct link, we are checking whether the URL contains the substring "index-selenium-webdriver"cy.url().should("include", "index-selenium-webdriver");
cy.go("forward");
Example program :
describe("My Second Test Suite", function () {
it("Handling multiple pages", function () {
Cypress.on("uncaught:exception", (err, runnable) => {
// returning false here prevents Cypress from
// failing the test
return false;
});
cy.visit("https://chercher.tech/java/index-selenium-webdriver");
cy.get(":nth-child(17) > .alsosee > a").click();
cy.url().should("include", "dropdown-select-class-in-selenium-webdriver");
cy.go("back");
cy.url().should("include", "index-selenium-webdriver");
cy.go("forward");
});
});
It is used to reload/refresh the webpage.
Example program :
describe("My First Test suite", function () {
it("My First testcase", function () {
cy.visit("https://chercher.tech/");
cy.url().should("include", "chercher.tech");
cy.log("Before reload");
cy.reload();
cy.log("after reload!");
});
});
Output :