Thanksgiving Week Special – Get 50% OFF on FileTranz
Claim Your OfferSalesforce CLI seems like a solution for Playwright SSO, but it's not. Discover why CLI and browser auth are separate, and how to use them together effectively.
When I was exploring different ways to handle Salesforce authentication in Playwright, I stumbled upon an idea that felt almost too good to be true. The Salesforce CLI (SFDX) already has rock-solid login flows and token management built in. If you've ever used SFDX, you know it handles SSO, MFA, and org access like a pro.
So I thought: Why are we reinventing the wheel in Playwright? Can't we just steal the tokens from SFDX and use them in our browser tests?
Spoiler: it's more complicated than that. But the journey of figuring out why taught me a lot about how Salesforce's authentication actually works.
Salesforce CLI supports a few different ways to authenticate, but two stood out as potentially useful for testing:
Web login (sfdx auth:web:login) – This is the interactive route. You run the command, it opens your browser, you log in with SSO and MFA, and SFDX saves your org access details locally. The next time you run a command, it just uses the stored token. Super convenient.
JWT-based authentication (sfdx auth:jwt:grant) – This is the non-interactive route. You provide a key pair and a Connected App configuration, and SFDX gets you a token without any human clicking required. This sounded promising for automated testing.
I started with web login because it's closer to what a real user does. Here's what the flow looked like:
From a security standpoint, this felt clean. Your credentials aren't sitting in a script somewhere. Everything aligns with Salesforce's official authentication practices. No sketchy workarounds.
I was optimistic. Too optimistic, as it turned out.
Here's where my excitement ran into a brick wall:
SFDX authentication and Playwright browser authentication are completely separate systems. They don't talk to each other. At all.
Think about what's actually happening under the hood:
When Playwright's browser loads salesforce.mydomain.com, Salesforce doesn't know or care that SFDX has a valid token sitting somewhere on your machine. The browser still has to go through the full login flow: username, password, SSO, MFA, the whole nine yards.
And here's the kicker: there's no supported way to "transfer" SFDX's token into the browser's session and have Salesforce treat it as legitimate UI access.
I spent time looking for hacks. Session cookies? Salesforce doesn't work that way. Injecting headers? The browser UI doesn't support that. Using the token to pre-fill the login form? Nope, that's not how browser authentication works.
So there I was, staring at this perfectly valid SFDX token sitting in my local directory, completely useless for the thing I actually needed: skipping the login screen in Playwright.
After testing this thoroughly—and honestly, wasting some time that I'll never get back—I realized I was trying to force SFDX to solve the wrong problem.
SFDX is amazing for backend automation:
SFDX is terrible for UI login automation because:
When I finally got honest with myself, it was obvious: SFDX and Playwright are tools for different jobs. Trying to make them work together for UI login was like using a hammer to drive a screw—technically you might make it work, but you're going to have a bad time and everyone will judge you for it.
Once I stopped trying to force SFDX to be something it's not, I could see where it actually does shine.
Here's how I ended up using both tools together:
SFDX handles environment prep: Before your Playwright tests even start, SFDX can set up your test org. Create test accounts, populate data, deploy custom configs. Run whatever backend setup your tests need. This happens before your browser even loads.
Playwright handles UI testing: Your Playwright tests log in once using storage state (remember Blog 2?), then they run through all your UI scenarios. Login happens once. Tests run fast. No repeated authentication.
SFDX handles cleanup: After your tests finish, SFDX can wipe out the test data you created, reset org settings, or any other backend cleanup. You leave the org in a clean state for the next test run.
It's a clean separation of concerns. Each tool does what it's actually designed to do.
Looking back, I can see why I got excited about SFDX. If you're not familiar with the internal architecture of how Salesforce authentication works, it feels like it should work. "The CLI can authenticate... the browser needs to authenticate... surely there's some way to share that?"
But Salesforce's architecture is intentionally strict about separating API authentication from UI authentication. It's a security feature, not a limitation. They're not going to let you sneak a token from the command line into the browser without going through proper authentication channels.
I also think I was looking for a universal solution—one tool that would solve everything. That's a trap I fall into sometimes. In reality, the best solutions use multiple tools, each one solving the specific problem it was designed for.
After working through all this, here's how I'd rank the approaches:
Best: Saved authentication state + SFDX for prep/cleanup Use storage state to log in once and stay logged in through all your tests. Use SFDX to set up data before tests and clean up after. Fast, secure, scalable, and each tool works at what it's actually designed for.
Good: Persistent profiles locally + SFDX for environment setup If you're just doing local development and debugging, persistent profiles are fine. SFDX can still handle your backend setup and cleanup needs.
Don't bother: Trying to use SFDX tokens directly in Playwright This doesn't work in any supported way, and you'll waste time trying to make it work.
Definitely don't do: Hardcoding credentials or trying to hack Salesforce's session system I know I didn't cover this in the blog series, but it's worth saying clearly: don't do this.
The breakthrough came when I stopped thinking about "how do I make authentication a one-size-fits-all solution" and started thinking about "what is each tool genuinely good at?"
Once I accepted that:
...everything clicked into place. The design became simpler, more maintainable, and honestly, a lot faster.
If you're building Salesforce test automation with Playwright, here's the pattern:
You get the best of both worlds: efficient browser testing and powerful backend automation, without trying to make either tool do something it wasn't designed for.