I am using the below feature file to perform the sql query
run using cy.task()
in latest Cypress version. This is working fine, but is there any other clear way to perform sql query to include more parameters and to make a generic feature file line/ step definition ? Can someone please advise ?
Cypress: 10.3.0
Cypress-Cucumber-Preprocsessor
//Feature file line:
Scenario: Run the sql query to verify the user details in the User table
Given I run the sql query to get "FirstName, LastName, Email, Address" for the user email "sam@test.com"
// databaseQuery.cy.js
Then('I run the sql query to get {string} for the user email {string}', (query, userEmail) => {
cy.task('queryDb', `SELECT ${query} FROM Users WHERE Email="${userEmail}"`).then((result) => {
expect(result[0].FirstName).to.equal("Sam");
expect(result[0].LastName).to.equal("Thinker");
expect(result[0].Email).to.equal(userEmail);
expect(result[0].Address).to.equal("455 Sydney Street");
});
});
One way I can think of is to have an object of properties and values you would want to validate the response against and then use the properties from this same object in your query.
Let's assume you have an object as below.
const user = {
FirstName: "Sam",
LastName: "Thinker",
Email: "sam@test.com",
Address: "455 Sydney Street"
}
Then your code can be altered as something like below:
Scenario: Run the sql query to verify the user details in the User table
Given I run the sql query to get "userdetails" for the user email "sam@test.com"
Then(
'I run the sql query to get {string} for the user email {string}',
(query, userEmail) => {
const queryParams =
query == 'userdetails' ? Object.keys(user).join(', ') : '*';
cy.task(
'queryDb',
`SELECT ${queryParams} FROM Users WHERE Email="${userEmail}"`
).then((result) => {
expect(result[0]).to.deep.equal(user);
});
}
);
This would also mean the query params are hidden from the cucumber feature file.