剧作家浏览器正在重新打开每个测试语句

Playwright browser is reopening every test statement

如何阻止浏览器在每次测试语句完成后重新打开?我的意思是下面的代码应该在 1 页中继续。为什么浏览器关闭,再次打开并执行第二个测试。如何防止这种情况?谢谢


  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();
  }); 
})```

对于共享同一页面并依赖于先前测试结果的测试,您需要将它们包装在 test.describe.serial and initialize the page in beforeAll, see this guide 中以获取更多信息。

您的示例如下所示:

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();
  }); 
});