In cypress, we can also create classes and how to share the contents with the help of inheritance. It is an important topic because it is mostly used in the automation framework. Let us understand how to create and inherit classes in cypress with the help of a simple program.
Example program:
class BasePage {
static loadHomePage() {
Cypress.on("uncaught:exception", (err, runnable) => {
// returning false here prevents Cypress from
// failing the test
return false;
});
cy.visit(
"https://chercher.tech/protractor/dropdowns-protractor#javascript-method-select"
);
}
static wait(number) {
cy.wait(number);
}
}
class HomePage extends BasePage {
static scrollToBottom() {
cy.get("#create-select-class").scrollIntoView();
}
static scrollToTop() {
cy.get("#dropdowns-protractor-select").scrollIntoView();
}
}
describe("Abstraction with classes", () => {
it("should scroll down and up the page", () => {
HomePage.loadHomePage();
HomePage.scrollToBottom();
HomePage.wait(5000);
HomePage.scrollToTop();
HomePage.wait(3000);
});
});
Output :
In cypress, we can also decide what test should be performed before all the testcases or before each test case and even after all the test cases or after each of the test cases.There are four types of hooks in cypress namely,
Let us understand the hooks with the help of an example.
class BasePage {
static loadHomePage() {
Cypress.on("uncaught:exception", (err, runnable) => {
// returning false here prevents Cypress from
// failing the test
return false;
});
cy.visit(
"https://chercher.tech/protractor/dropdowns-protractor#javascript-method-select"
);
}
static wait(number) {
cy.wait(number);
}
}
class HomePage extends BasePage {
static scrollToBottom() {
cy.get("#create-select-class").scrollIntoView();
}
static scrollToTop() {
cy.get("#dropdowns-protractor-select").scrollIntoView();
}
}
describe("Abstraction with classes", () => {
before(function () {
//runs before all tests inside describe
//example:setup the test data or seed/reset the database
HomePage.loadHomePage();
});
after(function () {
//runs after all tests inside describe block
//example: Test cleanup or clean cookies or local storage
//not used often
Cypress.Cookies.debug(true);
// clear cookies again after visiting
});
beforeEach(function () {
//runs before each it block in the describe
HomePage.wait(5000);
});
afterEach(function () {
//runs after each it block in the describe
HomePage.wait(3000);
});
it("should scroll down and up the page", () => {
HomePage.scrollToBottom();
HomePage.scrollToTop();
});
Output :
Cypress also provides two types of tags namely .skip and .only.
Let us learn how to use these tags with the help of a simple example.
class BasePage {
static loadHomePage() {
Cypress.on("uncaught:exception", (err, runnable) => {
// returning false here prevents Cypress from
// failing the test
return false;
});
cy.visit("https://chercher.tech/protractor/dropdowns-protractor");
}
static wait(number) {
cy.wait(number);
}
}
class HomePage extends BasePage {
static scrollToBottom() {
cy.get("#create-select-class").scrollIntoView();
}
static scrollToTop() {
cy.get("#dropdowns-protractor-select").scrollIntoView();
}
}
describe("Abstraction with classes", () => {
before(function () {
//runs before all tests inside describe
//example:setup the test data or seed/reset the database
HomePage.loadHomePage();
});
after(function () {
//runs after all tests inside describe block
//example: Test cleanup or clean cookies or local storage
//not used often
cy.clearLocalStorage();
// clear cookies again after visiting to remove
// any 3rd party cookies picked up such as cloudflare
});
beforeEach(function () {
//runs before each it block in the describe
HomePage.wait(5000);
});
afterEach(function () {
//runs after each it block in the describe
HomePage.wait(3000);
});
it.skip("should scroll down and up the page", () => {
HomePage.scrollToBottom();
HomePage.scrollToTop();
});
it.only("2nd IT block", () => {
HomePage.scrollToBottom();
HomePage.wait(5000);
HomePage.scrollToTop();
HomePage.wait(3000);
});
it("3rd IT block", () => {
HomePage.scrollToBottom();
});
});
Output :