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