Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Existing browser is closing when test is failed and new browser is opening for new test. Consumes time for executing before all code #34143

Closed
Vamsi5694 opened this issue Dec 26, 2024 · 4 comments

Comments

@Vamsi5694
Copy link

We need same page instance for all tests in spec even any case is failed also

  1. Sharing browser, context and page throughout spec file
  2. Worker is one only
  3. test 1 passed
    test 2 passed with same page
    test 3 failed and browser is closed
    test 4 execution with new browser
const {test, expect, chromium} = require('./fixtures/base_fixture.js');

let browser,context, page;

test.describe('My test cases', async() => {
    test.beforeAll(async({loginFixture, webPopup}, testInfo) => {
        testInfo.setTimeout(60000)

        // Open Chrome, their context and page
        browser = await chromium.launch();
        context = await browser.newContext();
        page = await context.newPage();

        // await page.clock.install({time: new Date("2025-01-01T12:00:00")})

        context.on('close', () => console.log('Context closed!'));
        page.on('close', () => console.log('Page closed!'));

        // Printing all API enpoint with methods if endpoint includes 'api/_/'
        page.on('request', request => {
            if(request.url().includes("api/_/")) {
                console.log(`Request URL: ${request.url()} Request Method: ${request.method()}`)
            }
        });

        // Modifying API endpoint
        await page.route("end point", async (route) => {
            let responseBody = await route.fetch();
            let originalBody = await responseBody.json();

            const modifiedBody = {
            ...originalBody,
            ticket: {
                ...originalBody.ticket,
                subject: 'Modified Subject',
                description: 'Modified Description',
                },
            };

            console.log("Response of the body:")
            console.log(originalBody)
            await route.fulfill({
                response: responseBody,
                body: JSON.stringify(modifiedBody),
                headers: {
                    ...responseBody.headers(),
                    'content-type': 'application/json',
                  },
            });
        });

        // Custom Fixtures for Login into application and closing popup
        await loginFixture(page);
        await webPopup(page);
        
        const verificationLocator = await page.locator("locator");
        await page.waitForSelector("locator");
        await expect(verificationLocator).toBeVisible();  
    })
    
    test.beforeEach(async() => {
          // some code
    })
    
    test('Test 1 Basic cases',{ tag: '@fast'}, async () => {
        //  test 1 code --> Passed
     });

     test('Test 2 Basic cases',{ tag: '@fast'}, async () => {
        //  test 2 code --> Passed with same page
     });

     test('Test 3 Basic cases',{ tag: '@fast'}, async () => {
        //  test 3 code --> Failed and browser closed
     });

     test('Test 4 Basic cases',{ tag: '@fast'}, async () => {
        //  test 4 code --> New browser opened
     });

})

In simple way,

import {test, expect, chromium} from '@playwright/test'

let browser, page, context;

test.describe("My cases", async() => {
    test.beforeAll(async() => {
        browser = await chromium.launch();
        context = await browser.newContext();
        page = await context.newPage();

        await page.goto("Your URL")
        console.log("First before all")
    })

    test("Basic case -1", async() => {
        console.log("basic case 1");
        await expect(page).toHaveTitle("Support : ele08staging2");
    })

    test("Basic case -2", async() => {
        console.log("basic case 2");
        await expect(page).toHaveTitle("Support : ele08staging2");
    })

    test("Basic case -3", async() => {
        console.log("basic case 3");
        await expect(page).toHaveTitle("Helpdesk : ele08staging2");
    })

    test("Basic case -4", async() => {
        console.log("basic case 4");
        await expect(page).toHaveTitle("Support : ele08staging2");
    })

})
@darkdoomrag3
Copy link

darkdoomrag3 commented Dec 26, 2024

The problem you’re encountering happens because Playwright’s default test runner automatically closes resources when a test fails or completes. To work around this, you can use a try-catch block for assertions. This approach ensures that the same browser and page remain open throughout all tests, even if one test fail.

import { test, expect, chromium } from '@playwright/test';

let browser; 
let context; 
let page;    

test.describe("My cases", () => {

  test.beforeAll(async () => {
    browser = await chromium.launch();
    context = await browser.newContext();
    page = await context.newPage();

    await page.goto("Your URL");
    console.log("Browser, context, and page initialized.");
  });


  test.afterAll(async () => {

    if (browser) {
      await browser.close();
      console.log("Browser closed after all tests.");
    }
  });

  test("Basic case -1", async () => {
    console.log("Executing test case 1");
    await expect(page).toHaveTitle("Support : ele08staging2");
  });

  test("Basic case -2", async () => {
    console.log("Executing test case 2");
    await expect(page).toHaveTitle("Support : ele08staging2");
  });

  test("Basic case -3", async () => {
    console.log("Executing test case 3");
    try {
      await expect(page).toHaveTitle("Helpdesk : ele08staging2 this test is the faild one");
    } catch (error) {
      console.error("Test case 3 failed:", error.message);
    }
  });

  test("Basic case -4", async () => {
    console.log("Executing test case 4");
    try {
        await expect(page).toHaveTitle("Helpdesk : ele08staging2 this test is the faild one");
      } catch (error) {
        console.error("Test case 3 failed:", error.message);
      }
  });

  test("Basic case -5", async () => {
    console.log("Executing test case 5");
    await expect(page).toHaveTitle("Support : ele08staging2");
  });

  test("Basic case -6", async () => {
    console.log("Executing test case 6");
    await expect(page).toHaveTitle("Support : ele08staging2");
  });
});

@dgozman
Copy link
Contributor

dgozman commented Dec 27, 2024

Playwright shuts down the worker process after test failure. Closing as a duplicate of #32803 that asks for a way to disable this.

@dgozman dgozman closed this as completed Dec 27, 2024
@Vamsi5694
Copy link
Author

Here page and browser is working for other tests but test is passed even assertion is failed. Its misleading test results

Adding try-catch for one test case is acceptable but what if we have 1000 tests among 100 test files. Its tough for adding into multiple files and tests. We have any option to execute same fixtures for new tests also?

Or can we create custom test runner to remain same page and browser throughout all tests in spec?

Please look into this issue

@Vamsi5694
Copy link
Author

@dgozman I am facing above issue. Please provide solution on this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants