Thanksgiving Week Special – Get 50% OFF on FileTranz

Claim Your Offer

Salesforce CLI & Playwright: Not the Silver Bullet

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

By Gurumoorthy Manickam
Lead Tester

The Tempting Solution That Almost Worked

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.

How Salesforce CLI Actually Manages Login

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:

  1. Run sfdx auth:web:login pointing to your Salesforce org
  2. Complete the login in your browser (SSO, MFA, the works)
  3. SFDX stores the authentication details locally
  4. Now any SFDX command can talk to your org using that stored token

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.

The Bitter Reality: CLI Tokens and Browser Sessions Are Worlds Apart

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:

  • SFDX stores a token on your machine. That token can be used to make API calls to Salesforce (create records, run queries, deploy metadata, etc.)
  • Playwright, on the other hand, drives an actual browser—the kind a human uses to click buttons and fill out forms
  • Salesforce sees these as two different clients with two different authentication paths

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.

What SFDX Is Actually Good At (And What It's Not)

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:

  • Setting up test data before your tests run (creating accounts, contacts, etc.)
  • Cleaning up after tests finish
  • Deploying metadata or configuration changes
  • Running one-off admin tasks as part of your CI/CD pipeline
  • Anything that doesn't require a human clicking around in a browser

SFDX is terrible for UI login automation because:

  • Browser sessions require their own authentication flow
  • There's no bridge between API tokens and UI sessions
  • Salesforce's session management treats them as completely separate
  • It introduces unnecessary complexity without solving the actual problem

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.

The Real Role of CLI in a Balanced Test Strategy

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.

Why I Initially Missed This Obvious Solution

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.

The Hierarchy of Solutions for Salesforce Testing

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 Moment I Understood the Real Design Pattern

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:

  • SFDX is for APIs and backend automation
  • Playwright is for UI testing and browser automation
  • Storage state bridges the gap for browser login
  • These three work beautifully together

...everything clicked into place. The design became simpler, more maintainable, and honestly, a lot faster.

What This Means for Your Test Strategy

If you're building Salesforce test automation with Playwright, here's the pattern:

  1. Use SFDX to prepare your test environment – Create fresh data, deploy any configs, set up prerequisites
  2. Use Playwright with storage state to test the UI – Log in once, run all your scenarios efficiently
  3. Use SFDX to clean up – Remove test data, reset settings, leave everything tidy

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.

Key Takeaways

  • SFDX is powerful but purpose-built – It excels at API and backend automation, not UI testing
  • Authentication paths are separate for a reason – CLI tokens and browser sessions are intentionally kept distinct
  • Don't force tools to solve problems they weren't designed for – Use the right tool for each job
  • The best solutions combine multiple tools – SFDX + Playwright + storage state work together beautifully
  • Simplicity comes from understanding limitations – Knowing what each tool can't do is as important as knowing what it can

free-consultation