Simulating Incognito Mode in Chrome with Playwright

Hey everyone! I’m working on a project where I need to test different parts of my app using various login credentials. The tricky part is that my application uses Single Sign-On (SSO), which automatically redirects to the home page when I open it normally.

I’ve noticed that when I manually use incognito mode in Chrome, I can enter different credentials as needed. This is exactly what I want to achieve in my automated tests using Playwright.

I’ve tried a bunch of things like passing arguments and creating new contexts, but I’m still running into issues. Here’s a snippet of what I’ve attempted:

def create_private_browser(pw):
    browser_exe = "/path/to/chrome"
    
    browser = pw.chromium.launch(
        headless=False,
        executable_path=browser_exe,
        args=['--private-mode', '--no-first-run']
    )
    
    context = browser.new_context(ignore_https_errors=True)
    return context

Any ideas on how to make Playwright behave like Chrome’s incognito mode? I’m really stumped here and could use some expert advice. Thanks in advance!

As someone who’s battled with SSO and Playwright, I feel your pain. Here’s a trick that’s worked wonders for me:

Launch your browser with a custom user data directory for each test. It’s like giving each test its own ‘incognito’ profile:

import tempfile

def create_isolated_context(playwright):
    user_data_dir = tempfile.mkdtemp()
    browser = playwright.chromium.launch_persistent_context(
        user_data_dir,
        headless=False,
        args=['--disable-extensions', '--disable-plugins']
    )
    return browser

# Use it like this:
with create_isolated_context(playwright) as browser:
    page = browser.new_page()
    # Your test code here

This approach creates a fresh browser instance each time, bypassing SSO’s automatic login. It’s been rock-solid for me across different projects.

Just remember to clean up the temp directories after your tests to avoid cluttering your system. Hope this helps!

hey mate, have you tried using a persistent context? it’s been a lifesaver for me with SSO stuff. something like:

context = browser.new_persistent_context(
    user_data_dir='/path/to/user/data',
    ignore_https_errors=True
)

this way you can manage different profiles for each set of creds. just remember to clear the dir between tests to keep things fresh!

I’ve faced similar challenges with SSO and Playwright. Here’s what worked for me:

Instead of trying to emulate incognito mode directly, focus on creating isolated contexts for each test. Use browser.new_context() with specific options:

context = browser.new_context(
    storage_state=None,
    ignore_https_errors=True,
    proxy={
        'server': 'localhost:8080',
        'bypass': '127.0.0.1,localhost'
    }
)

This approach creates a fresh context each time, effectively mimicking incognito behavior. The proxy setting can help bypass some SSO mechanisms.

Also, clear cookies and localStorage before each test:

page.evaluate('() => { localStorage.clear(); sessionStorage.clear(); }')
await page.context.clear_cookies()

These steps should give you the isolation you need for testing with different credentials.