• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

1.2K
Views
How to prevent the browser from closing while running code in Playwright Tests in Javascript

I'm trying to log the network calls in the browser. For my use case I require the browser to be in an open state and should be closed only with the scripts. I am currently using the page.pause() function to prevent the browser from automatically closing. Is there any other way to prevent the browser from closing automatically.

test('Verify home page Load event',async({page})=>{
//const browser = await chromium.launchPersistentContext("",{headless:false});
await page.goto("https://samplesite.com")


await page.on('request',req=>{
    const requestUrl = req.url();
    if(requestUrl.indexOf("google-analytics.com/collect")>-1){
        console.log("Intercepted:->"+requestUrl);
        
    }else{            
        req.continue
    }        
}) 

await page.pause();

})

I tried checking out this [link] (How to keep browser opening by the end of the code running with playwright-python?) for python but could not apply it to JS.

about 3 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Similar to what was described in the answer to the python question, you need to keep your script alive somehow. This answer describes a couple of ways to do that.

However, page.pause() is definitely the recommended approach- it exists precisely for this kind of situation where you need to inspect the browser while your script is executing. Your script also has some problems- as it stands when you encounter your target request you are logging something but not calling request.continue() (note that this a method, not a property). That will cause all requests to hang indefinitely until it is continued or aborted.

You probably want to do something like this:

await page.route('**/*', (route, request) => {
    const rurl = request.url();
    if (rurl.includes('google-analytics.com/collect')) {
        console.log(`Intercepted request to ${rurl}`);
        // Do other stuff?
    }
    route.continue();
});

It's not clear what you are trying to accomplish from your snippet- if you just need to wait for a particular request to fire, you can use either: page.waitForRequest or page.waitForResponse, and do away with worrying about keeping the browser open.

about 3 years ago · Juan Pablo Isaza Report

0

You can try await Task.Delay(-1)

about 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error