playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Question] How to launch Playwright server without incognito mode

Open ankur-lt opened this issue 3 years ago • 5 comments

We want to launch the Playwright Server without incognito mode for user to connect to the browser remotely. The playwright server by default launches the browser in incognito mode. And hence, the browser extensions cannot be used in the incognito mode. How can I disable the incognito mode while launching the playwright server? As per I checked from the command used to launch the browser at chrome://version/, it does not contain the --incognito flag, which is used to launch the browser in incognito.

I can set the flag ignoreDefaultArgs: true and pass the required arguments to the args object for launchServer() to launch the browser without incognito mode. Is there any other way for launching the server with persistent context? And why is --incognito not passed to the command while launching the browser in incognito?

@mxschmitt

ankur-lt avatar Sep 06 '22 07:09 ankur-lt

@ankur-lt Could you please explain your usecase? See also issue #1523 for a similar discussion.

dgozman avatar Sep 06 '22 22:09 dgozman

@dgozman , we want the ability to load extensions and use them in the browser. We are using the launchServer() to launch the Playwright Server, for user to run their tests on the remote machine(https://www.lambdatest.com/support/docs/run-your-first-playwright-test/). This cannot be done in the incognito mode as the extensions are by default disabled in incognito mode. This will require the use of a persistent context(https://playwright.dev/docs/chrome-extensions), but the launchPersistentContext() method does not start the playwright server. So either:

  • launchPersistentContext() method should should accept a param to set the LaunchType to server, or
  • launchServer() should accept a param to start the server with a persistent storage.

ankur-lt avatar Sep 07 '22 00:09 ankur-lt

@dgozman , @mxschmitt , any suggestions on this?

ankur-lt avatar Sep 09 '22 06:09 ankur-lt

@ankur-lt Thank you for the explanation. This sounds like a possible scenario, but there are no plans to support it for now. I'll leave this feature request open for further prioritization.

dgozman avatar Sep 09 '22 15:09 dgozman

Hey @dgozman Would love to see persistent context with launchServer method! Any updates on this?

asambstack avatar Dec 07 '22 10:12 asambstack

Hello, any updates on this? I am having similar requirement to launch in non incognito mode

bengabp avatar Mar 09 '23 13:03 bengabp

Have you tried headless: false in opts and --no-incognito in args?

dustinbyershax avatar Jun 06 '23 22:06 dustinbyershax

@dustinbyershax, tried that flag, it still opens in incognito.

ankur-lt avatar Jun 07 '23 03:06 ankur-lt

We have also the problem that the browser is started in incognito-mode and that this cannot be configured. In our case a third-party library is not creating any cookies when the browser is running in incognito-mode. When the browser is started in normal mode (non-incognito) it will create cookies. This is sth. we validated manually. We need to validate the created cookies with a list of expected/approved cookies (legal relevance) and if that differs the test shall fail - as of now this scenario cannot be implemented because of the missing configuration to run a test in non-incognito mode. We've also tried changing the headless-flag without any success.

nhebling avatar Jul 14 '23 20:07 nhebling

is it working in selinium ?

sankar3003 avatar Jul 27 '23 06:07 sankar3003

You guys can use dolphin anty browser and then connect to it using cdp connection, here is my code for doing it. now you will have all the freedom of cookies and chrome extensions

from playwright.sync_api import sync_playwright
import requests

PROFILE_ID = "your browser id"
req_url = f'http://localhost:3001/v1.0/browser_profiles/{PROFILE_ID}/start?automation=1'
response = requests.get(req_url)
automation_settings = response.json()['automation']
port = automation_settings['port']
ws_endpoint = automation_settings['wsEndpoint']
cdp_url = f'ws://127.0.0.1:{port}{ws_endpoint}'

# initiate the browser
p = sync_playwright().start()
browser = p.chromium.connect_over_cdp(cdp_url)
context = browser.contexts[0]
page = context.new_page()

junaid9211 avatar Aug 26 '23 13:08 junaid9211

Also, it would be helpful to add a flag inside playwright config file to use either connect method or connectOverCDP while spawning the browser.

karanshah-browserstack avatar Sep 13 '23 10:09 karanshah-browserstack

ignoreDefaultArgs: ["--no-startup-window"] should work for spawning the browser. And then just use connectOverCDP

kaliiiiiiiiii avatar Oct 10 '23 18:10 kaliiiiiiiiii

I am facing the same issue. I have to test notifications in my webapp. But since playwright opens Chromium in incognito mode I am not able to update the setting which enables notifications for my website. Can anyone please suggest the solution in case they have faced similar challenge.

ritikaqure07 avatar Jan 18 '24 10:01 ritikaqure07

This is also an issue for a project I am working on. Third party cookies are being blocked in chrome, and are causing authentication to fail as a result. As there is no option to enable third party cookies via chrome options, at least that I am aware of, we can't log in to our application.

jwohlfahrt avatar Feb 23 '24 20:02 jwohlfahrt

You can use chromium.launchPersistentContext() to launch the Chromium browser with a persistent context. We specify the directory path where the browser data will be stored (in this case, ./reports). We pass additional options such as channel and headless as needed.

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

(async () => { // Launch the browser with a persistent context const browser: BrowserContext = await chromium.launchPersistentContext('./reports', { channel: 'chrome', headless: false });

const page: Page = await browser.newPage(); await page.goto("https://naveenautomationlabs.com/opencart/index.php?route=account/register");

})();`

naveenanimation20 avatar Feb 24 '24 14:02 naveenanimation20

for me it worked with this browser = playwright.chromium.launch_persistent_context(headless=False, user_data_dir= './dataDir') page = browser.new_page()

P.S: i'm using python

nelsonacalves avatar Mar 07 '24 13:03 nelsonacalves

Here's a complete working solution based on windows localStorage to store login cookie since Pw runs incognito. The remainder of the test uses msedge, hence userDataDir is set to MS Edge. This took it's while to figure out. Life would be easier if Pw had a non-incognito flag instead in playwright config.

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

const config = {
  "edgeUserDataPath": "C:\\Users\\devadmin\\AppData\\Local\\Microsoft\\Edge\\UserDa~1",
  "loginUrl": "https://www.example.com/login",
  "postLoginUrl": "https://www.example.com/postlogin",
  "username": "testuser",
  "password": "testpassword",
}

test('test', async () => {
  const browser = await chromium.launchPersistentContext(config.edgeUserDataPath, { channel: 'msedge' });
  const page = await browser.newPage();
  await page.goto(config.loginUrl);
  await page.getByPlaceholder('Username').fill(config.username);
  await page.getByPlaceholder('Password').fill(config.password);
  await page.getByPlaceholder('Login').press('Enter');
  await page.waitForURL(config.postLoginUrl);
  await browser.close();
});

akwasin avatar Mar 22 '24 14:03 akwasin

I am also facing the same issue of opening chrome://version/ in chrome browser above version 115 in incognito mode.

this is the same usecase i have as of above mentioned by @ankur-lt

@dgozman , we want the ability to load extensions and use them in the browser. We are using the launchServer() to launch the Playwright Server, for user to run their tests on the remote machine(https://www.lambdatest.com/support/docs/run-your-first-playwright-test/). This cannot be done in the incognito mode as the extensions are by default disabled in incognito mode. This will require the use of a persistent context(https://playwright.dev/docs/chrome-extensions), but the launchPersistentContext() method does not start the playwright server. So either:

launchPersistentContext() method should should accept a param to set the LaunchType to server, or launchServer() should accept a param to start the server with a persistent storage.

please share any update on this feature, if you are considering it.

HRanjan-11 avatar Jun 10 '24 15:06 HRanjan-11

I managed to get this working. The remote-debugging-port argument tells it to start and expose the debugging port in debug mode. Once the server is running, you can connect to it with a client script over CDP.

server_cdp.js

const { chromium } = require('playwright');

(async () => {
  const pathToExtension = require('path').join(__dirname, 'ublock');
  const userDataDir = 'user-data-dir';
  const browserContext = await chromium.launchPersistentContext(userDataDir, {
    headless: false,
    args: [
      `--headless=new`,
      `--disable-extensions-except=${pathToExtension}`,
      `--load-extension=${pathToExtension}`,
      `--remote-debugging-port=9222`
    ],
  });

  let [backgroundPage] = browserContext.backgroundPages();
  if (!backgroundPage)
    backgroundPage = await browserContext.waitForEvent('backgroundpage');
  await backgroundPage.waitForLoadState('networkidle');
  
  await new Promise(r => setTimeout(r, 1000*60*10));
  
  await browserContext.close();
})();

client_cdp.js

const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.connectOverCDP('http://localhost:9222');
  const defaultContext = browser.contexts()[0];
  const page = await defaultContext.newPage();

  await page.goto('https://example.com/');
  await page.waitForLoadState('networkidle');
  
  await page.close();
  await defaultContext.close();
  await browser.close();
})();

mikemosseri avatar Jul 21 '24 12:07 mikemosseri