I'm writing a Cypress with POM. I want to put assert texts/messages in a textdata.json/txt so that i won't populate page class with too much texts/information. I have tried different approaches such as;
cy.get("element")
.readFile('cypress\fixtures\testData.json')
.should("have.text", variable name from json file)
or
cy.get("element") .should("have.text", this.data.variable name from json file)
with before-function but didn't work.
Any idea of how i can get this to please?
As per your requirement, you don't want to use page class
which I guess is fair when you are trying to find the right balance between DAMP and DRY principles. I can suggest the following minimal usage of texts/messages:
a. Use import instead of readFile
cypress/fixtures/testData.json:
{
"text": "variable name from json file"
}
cypress/integration/spec.js (test file):
import * as testData from '../fixtures/testData'
cy.get("element").should("have.text", testData.text)
b. Use fixtures instead of readFile
cypress/fixtures/testData.json:
{
"text": "variable name from json file"
}
cypress/integration/spec.js (test file):
cy.fixture('testData').then((data) => {
cy.get("element").should("have.text", data.text)
})
c. Use a customised POM / selector file: (I use this heaps on my projects)
cypress/pages/loginPage.js:
export const testDataModel = {
text: "variable name from json file"
}
cypress/integration/spec.js (test file):
import {testDataModel} from '../pages/loginPage'
cy.get("element").should("have.text", testDataModel.text)