kevthedevv
kevthedevv

Reputation: 39

Playwright browser is reopening every test statement

How to stop browser from reopening every time the the every test statement is done? I mean the code below is supposedly in 1 page continuing. Why is it the browser closes, open again and execute the second test. How to prevent that? Thanks


  test('When user logs in', async ({page}) => {
    const commonAction = new CommonAction();
    await commonAction.gotoPage(page);
    await expect(page).toHaveURL('https://uat.mercator.createit.dev/login');
    await commonAction.login( page, janEmail, janPasswd );
  });

  test('Then user is in my account page', async ({page}) => {
    const navigationAction = new NavigationAction();
    await navigationAction.verifyAccountPage(page);  
  });
  
  test('When user goes to newsletter subscriptions', async ({page}) => {
    const navigationAction = new NavigationAction();
    await navigationAction.goToNewsSubscription(page);  
  });

  test('Then user is in Newsletter subscription page', async ({page}) => {
    const navigationAction = new NavigationAction();
    await navigationAction.verifyNewsletterPage(page);  
  });

  test('When user updates subscription', async ({page}) => {
    const newsletterAction = new NewsletterAction();
    newsletterAction.subscribe(page);
  });

  test('Then user is redirected to My Account page after subscription updates', async ({page}) => {
    const navigationAction = new NavigationAction();
    await navigationAction.verifyAccountPage();
  }); 
})```

Upvotes: 2

Views: 3278

Answers (1)

Yury Semikhatsky
Yury Semikhatsky

Reputation: 2354

For the tests that share the same page and rely on the previous test results you need to wrap them in test.describe.serial and initialize the page in beforeAll, see this guide for more information.

Your example would look like this:

const { test } = require('@playwright/test');

test.describe.serial('use the same page', () => {
  /** @type {import('@playwright/test').Page} */
  let page;

  test.beforeAll(async ({ browser }) => {
    page = await browser.newPage();
  });

  test.afterAll(async () => {
    await page.close();
  });

  test('When user logs in', async ({}) => {
    const commonAction = new CommonAction();
    await commonAction.gotoPage(page);
    await expect(page).toHaveURL('https://uat.mercator.createit.dev/login');
    await commonAction.login( page, janEmail, janPasswd );
  });

  test('Then user is in my account page', async ({}) => {
    const navigationAction = new NavigationAction();
    await navigationAction.verifyAccountPage(page);  
  });
  
  test('When user goes to newsletter subscriptions', async ({}) => {
    const navigationAction = new NavigationAction();
    await navigationAction.goToNewsSubscription(page);  
  });

  test('Then user is in Newsletter subscription page', async ({}) => {
    const navigationAction = new NavigationAction();
    await navigationAction.verifyNewsletterPage(page);  
  });

  test('When user updates subscription', async ({}) => {
    const newsletterAction = new NewsletterAction();
    newsletterAction.subscribe(page);
  });

  test('Then user is redirected to My Account page after subscription updates', async ({}) => {
    const navigationAction = new NavigationAction();
    await navigationAction.verifyAccountPage();
  }); 
});

Upvotes: 8

Related Questions