【问题标题】:Playwright browser is reopening every test statementPlaywright 浏览器正在重新打开每个测试语句
【发布时间】:2021-10-15 06:43:10
【问题描述】:

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


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

【问题讨论】:

    标签: javascript playwright


    【解决方案1】:

    对于共享同一页面并依赖于之前的测试结果的测试,您需要将它们包装在test.describe.serial 中并在beforeAll 中初始化页面,有关详细信息,请参阅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();
      }); 
    });
    

    【讨论】:

    • 非常感谢你,伙计!我迟到了,但它有效!
    猜你喜欢
    • 2022-01-26
    • 2022-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    • 2023-01-16
    • 2021-04-13
    相关资源
    最近更新 更多