简体   繁体   English

在 Javascript 的 Playwright 测试中运行代码时如何防止浏览器关闭

[英]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.我目前正在使用 page.pause() 函数来防止浏览器自动关闭。 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.我尝试检查此 [链接]( 如何在使用 playwright-python 运行的代码结束时保持浏览器打开? )用于 python,但无法将其应用于 JS。

Similar to what was described in the answer to the python question, you need to keep your script alive somehow.与 python 问题的答案中描述的类似,您需要以某种方式使脚本保持活动状态。 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.但是, page.pause()绝对是推荐的方法——它的存在正是为了在脚本执行时需要检查浏览器的这种情况。 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).你的脚本也有一些问题——当你遇到你的目标请求时,你正在记录一些东西但没有调用request.continue() (注意这是一个方法,而不是一个属性)。 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.目前尚不清楚您要从您的代码片段中完成什么 - 如果您只需要等待特定请求触发,您可以使用: page.waitForRequestpage.waitForResponse ,而不必担心保持浏览器打开。

You can try await Task.Delay(-1)您可以尝试await Task.Delay(-1)

I tried to use await page.pause() and it doesn't work for me, but I found the tricky way, and it works well, just put at the end of your test:我尝试使用await page.pause()但它对我不起作用,但我发现了一种棘手的方法,而且效果很好,只需放在测试结束时:

await new Promise(() => {})

Reference on the link .参考链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM