Rayrun
โ† Back to Discord Forum

Is it possible to store a DB connection in global state and use across tests?

daniel_boone1990posted in #help-playwright
Open in Discord
daniel_boone1990
daniel_boone1990

I want to setup 1 DB connection which I can use in beforeEach and afterEach across test files. Someone has experience doing this?

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?"

15 replies

do you want to setup different DB connection for each test?

daniel_boone1990
daniel_boone1990

No I want to share 1 connection which I can use across my tests

daniel_boone1990
daniel_boone1990

I have tried to save in global variable but that doesn't work unfortunately

try to use the global setup to connect to your DB https://playwright.dev/docs/test-global-setup-teardown#setup

and you can creae a separate class such as dbContext connect to DB in the global setup and use the dbContext instance in any of your tests

daniel_boone1990
daniel_boone1990

like test.extend(DBhelper)?

daniel_boone1990
daniel_boone1990

So sharing between tests is not possible I guess

daniel_boone1990
daniel_boone1990

It's fine, I test file I use the DB a new connection and use it in the testfile

maybe you can do it like in a fixture

daniel_boone1990
daniel_boone1990

DB connection is not compatible with a playwright fixture I guess

try doing like this:

  1. define a custom class
class DBContext {

  constructor(connString: string) { }

  /// some logic
  openConnection()
  {

  }

  close() {

  }

  create() {

  }

  read () {

  }

  update() {

  }

  delete() {
    
  }
}
  1. in the fixture do the next:
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();
  }
});
  1. Now u r able to use the dbContext in your tests
e2eTest('dbContext', ({ dbContext }) => {
  dbContext.create();
  dbContext.read();
  dbContext.update();
  dbContext.delete();
})
image.png
image.png
image.png
daniel_boone1990
daniel_boone1990

this is working, but this will create a new connection I guess everytime you run a test

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

image.png
@daniel_boone1990: but the tests should be isolated between each other, therefore this is good that each suit u r connecting to the db, innit?

but the tests should be isolated between each other, therefore this is good that each suit u r connecting to the db, innit?

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

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 luc@ray.run.