cypress-blaat-base
v1.6.0
Published
Shared code for Cypress BLAATs
Readme
cypress-blaat-base
An npm package for shared code between Cypress BLAATs projects.
DB Seeding
This Node.js module is designed to facilitate database seeding in Cypress tests using XML files as a data source. It provides utilities to parse XML files, transform the data into a usable format, and seed the database before or during test execution.
Installation
npm install cypress-blaat-baseUsage
The functions exported by the module can be imported into your cypress tests. And called in as either cypress tasks or commands.
Example
This is the cypress.config.ts
import {defineConfig} from "cypress";
import {cleanUpDB, deleteData, deleteTestData, insertData} from "cypress-blaat-base";
export default defineConfig({
e2e: {
baseUrl: 'http://localhost:4200/your-app',
supportFile: "./cypress/support/commands.ts",
experimentalRunAllSpecs: true,
setupNodeEvents(on) {
on('task', {
/*DB Tasks*/
injectData: ({dataFile, env, credsPath, database}) => {
return insertData(dataFile, env, credsPath, database)
},
deleteData: ({table, field, value, env, credsPath, database}) => {
return deleteData(table, field, value, env, credsPath, database)
},
deleteTestData: ({table, field, env, credsPath, database}) => {
return deleteTestData(table, field, env, credsPath, database)
},
cleanUpData: ({dataFile, env, credsPath, database}) => {
return cleanUpDB(dataFile, env, credsPath, database)
},
log: (message) => {
console.log(message)
return null
}
})
}
},
env: {
/*These are the default values used for the credential location and could be overridden*/
credsPath: "C:/FBS/Apps/BLAATCreds.config",
dbEnv: "Local",
}
});This is the BLAATCreds.config. Should be commited to Gitea.
<appSettings>
<!--This is the key formats-->
<!--Env_YourDBUsername-->
<!--Env_YourDBPassword-->
<add key="Local_YourDBUsername" value=""/>
<add key="Local_YourDBPassword" value=""/>
<add key="Dev_YourDBUsername" value=""/>
<add key="Dev_YourDBPassword" value=""/>
<add key="Prod_YourDBUsername" value=""/>
<add key="Prod_YourDBPassword" value=""/>
</appSettings>This is the commands.ts
declare namespace Cypress {
interface Chainable {
insertVisitsDbData(file: string): Chainable<void>;
cleanUpVisitsDbData(file: string): Chainable<void>;
}
}
Cypress.Commands.addAll({
insertYourDbData(file: string) {
cy.task("injectData", {
dataFile: file,
env: Cypress.env("dbEnv"),
credsPath: Cypress.env("credsPath"),
database: "YourDB"
})
},
cleanUpYourDbData(file: string) {
cy.task("cleanUpData", {
dataFile: file,
env: Cypress.env("dbEnv"),
credsPath: Cypress.env("credsPath"),
database: "YourDB"
})
},
})Your data needs to be in the path cypress/e2e/resources/Data.xml. This is the Data.xml.
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<TABLE_NAME COLUMN="value"/>
</dataset>This is the test.cy.ts
describe("Your test", () => {
beforeEach(() => {
cy.insertYourDbData("Data.xml")
cy.cleanUpYourDbData("Data.xml")
});
it("Should do something", () => {
//*****
//*****
})
})