This thread is trying to answer question "Is it possible to setup 1 DB connection which can be used in beforeEach and afterEach across test files?"
try to use the global setup to connect to your DB https://playwright.dev/docs/test-global-setup-teardown#setup
try like this: https://stackoverflow.com/questions/65208715/how-to-pass-object-retrieved-from-mongo-db-to-the-test-in-javascript
write your custom helper and use it anywhere ๐
try doing like this:
class DBContext {
constructor(connString: string) { }
/// some logic
openConnection()
{
}
close() {
}
create() {
}
read () {
}
update() {
}
delete() {
}
}
type FixturePages = {
dbContext: DBContext;
};
export const e2eTest = base.extend<FixturePages>({
dbContext: async ({}, use) => {
const dbcontext = new DBContext('someconnstring');
dbcontext.openConnection();
await use(dbcontext);
dbcontext.close();
}
});
e2eTest('dbContext', ({ dbContext }) => {
dbContext.create();
dbContext.read();
dbContext.update();
dbContext.delete();
})
Yes you are right.
try use beforeAll/afterAll hooks
define context to use the one instance of the dbContext between your tests
openConnection in the beforeAll and close it in afterAll ๐
it's sad that you cannot export anything from the spec file. U'd create a global setup and export the db instance it would be really nice
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].