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

jba*_*065 6 javascript ui-automation node.js playwright

我正在尝试在浏览器中记录网络调用。对于我的用例,我要求浏览器处于打开状态,并且只能使用脚本关闭。我目前正在使用 page.pause() 函数来防止浏览器自动关闭。有没有其他方法可以防止浏览器自动关闭。

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();
Run Code Online (Sandbox Code Playgroud)

})

我尝试检查Python的[链接](How to keep browser opening by the end of the code running with playwright-python?),但无法将其应用于JS。

小智 1

你可以试试await Task.Delay(-1)