microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
64.18k stars 3.49k forks source link

[Question] How to launch Playwright server without incognito mode #17120

Open ankur-lt opened 1 year ago

ankur-lt commented 1 year ago

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

dgozman commented 1 year ago

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

ankur-lt commented 1 year ago

@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:

ankur-lt commented 1 year ago

@dgozman , @mxschmitt , any suggestions on this?

dgozman commented 1 year ago

@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.

asambstack commented 1 year ago

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

bengabp commented 1 year ago

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

dustinbyershax commented 1 year ago

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

ankur-lt commented 1 year ago

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

nhebling commented 1 year ago

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.

sankar3003 commented 12 months ago

is it working in selinium ?

junaid9211 commented 10 months ago

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()
karanshah-browserstack commented 10 months ago

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

kaliiiiiiiiii commented 9 months ago

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

ritikaqure07 commented 6 months ago

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.

jwohlfahrt commented 4 months ago

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.

naveenanimation20 commented 4 months ago

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");

})();`

nelsonacalves commented 4 months ago

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

akwasin commented 4 months ago

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();
});
HRanjan-11 commented 1 month ago

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.