How we can make cypress scripts easily maintainable like POM in other tools like selenium

Viewed 1740

This is just a general clarification about building framework using cypress.io. In cypress can we write a test framework like page object model in selenium? These model make our life easy to maintain tests. For eg if ID or class of a particular element which is used across multiple tests /files has changed with a new version of Application-In cypress it is hard to go to multiple test files/tests and change the ID right? Can we follow the same page object model concept like declaring all elements as variables in each page and use the variable names in tests/functions? Also can we reuse these variables across different test .js files ? If yes - can you please give a sample Thanks

1 Answers

I have seen only a few people using POM concept while creating an automation framework using Cypress. Is that advisable to follow POM model, it depends on reading the following link from team. I would say this may depend upon automation tools/ architecture. According to Cypress team this is not recommendable, may be a debatable topic, read this: https://www.cypress.io/blog/2019/01/03/stop-using-page-objects-and-start-using-app-actions/#

We can declare the variable names in Cypress.env.json file or cypress.json file like below:

{
"weight": "85",
"height": "180",
"age": "35"
}

Then if you want to use them in a test-spec, create a new variable and receive it like below in test-spec.

const t_weight = Cypress.env('weight');
const t_height = Cypress.env('height');

Now you can use the variable in respective textbox input of pages as below:

cy.get('#someheighttextfieldID').type(t_weight);
cy.get('#someweighttextfieldID').type(t_height);

or receive it directly; cy.get('#someweighttextfieldID').type(Cypress.env('weight'));

example: /* declare varaibles in 'test-spec.js' file*/

const t_weight = Cypress.env('weight');
const t_height = Cypress.env('height');

//Cypress test - assume below test to test some action and receive the variable to text box

describe('Cypress test to receive variable', function(){
it('Cypress test to receive variable', function(){
    cy.visit('/')
    cy.get('#someweighttextfieldID').type(t_weight);
    cy.get('#someheighttextfieldID').type(t_height);  
    //even receive the variable straight away
  cy.get('#someweighttextfieldID').type(Cypress.env('weight'));

  })
});
Related