web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :

Handling MFA in Dynamics 365 Regression Testing with Playwright and MCP

Edison Lai Profile Picture Edison Lai Microsoft Employee

Introduction

In my previous blog post, I introduced Playwright and Playwright MCP for regression testing in Dynamics 365. Below is the shortcut:

 

Dynamics 365 finance and operations Regression testing with Playwright

Playwright MCP - AI-Powered Testing for Dynamics 365 Finance and Operations apps

Running Dynamics 365 Playwright Tests in the Cloud with Playwright Workspaces

 

While working with Playwright and Playwright MCP, I have been asked always for a practical way to handle MFA when it is really required for Dynamics 365 Apps testing, no matter Finance and operations or Customer Engagement, or PP Apps. My usual recommendation for test accounts is to disable MFA where security policy permits, but this is not allowed under the security requirements for some organizations. The goal is therefore to reuse an approved authenticated session without turning MFA off.

One alternative I recommend is Playwright's saved authentication state, storageState. First, complete the approved sign-in and MFA interactively, then save the authenticated browser state for reuse in subsequent tests. This does not bypass MFA, reuse works only while the session remains valid and tenant policies allow it. If a fresh MFA challenge is required, authenticate again and capture a new state file before starting regression testing. In this follow-up, I will explain how to save authenticated browser state and reuse it with Playwright Test for repeatable regression testing and with Playwright MCP for AI-assisted browser interaction. 

 

What is storageState in Playwright

In Playwright, storageState is a snapshot of authentication-related browser storage. It is commonly saved as a JSON file and loaded when a new browser context is created. It includes cookies and local storage. 

 

Where it is used

Setting or command

Save state during recording

codegen --save-storage

Load state into a new recording

codegen --load-storage

Initialize a Playwright Test context

use.storageState

Initialize an isolated Playwright MCP session

--isolated with --storage-state

 

For Dynamics 365 Apps regression testing with StorageState, the main benefit is separating authentication from business validation. Prepare and check authentication first, then let the test focus on business scenarios or other application behavior. This reduces repeated sign-in code and makes failures easier to diagnose. The diagram below uses simplified user and project names. The walkthrough later in this article uses files such as userTestuser1.json.

 

Important: saved state does not bypass MFA or Conditional Access, guarantee a permanent login, or restore Dynamics 365 data. It does not reset orders, reverse an invoice, or guarantee the correct legal entity. Treat the state file like a credential.

Below is concept flow of the Storagestate used for the Dynamics 365 Apps regression testing.

 

How to capture storageState

 

Complete the pre-requisites

Before capturing storage state, complete the environment setup and Playwright installation described in my earlier post. Use a supported Node.js release, an initialized Playwright Test project with its dependencies installed, and the required browser. Most important is keeping authentication files in the saved place. Treat each state file as a credential and exclude it from ordinary logs, caches and published test artifacts.

 

Run codegen to generate the storage state

Run the following command from the project root. The --save-storage option specifies the destination for the state file, which Playwright writes when the recording browser is closed normally.

npx playwright codegen https://<yourdomain>.dynamics.com --save-storage=./tests/userTestuser1.json

 

The command uses Playwright's default Chromium browser and opens both a browser window and Playwright Inspector, which provides the recording controls. Stop code recording before entering credentials, then sign in with testing account and complete the normal Microsoft Entra MFA challenge using the approved method. The purpose is to save authenticated state, not passwords or MFA values.

 

 

After authentication, wait until Dynamics 365 is fully loaded and usable. Confirm the environment, account and intended legal entity. Then close the browser normally so Playwright writes the state file. Do not sign out before closing, and do not force-stop the capture process before the file is written.

Locate the generated JSON file

The project now contains userTestuser1.json. This file holds the captured cookies and supported browser storage used to initialize an authenticated context for Dynamics 365. It is not a copy of every session detail or of Dynamics 365 business data. The screenshot below illustrates the file structure. There are some ms-dync-* components are very important.

 

Repeat the capture process in a fresh browser session for each additional test user. Confirm that the intended account is selected, complete its MFA, wait for the application to be ready, and save to a separate file. Use distinct test accounts and state files for the identities required by your scenarios.

 

For example, replace userTestuser1.json in the capture command with userTestuser2.json or userTestuser3.json:

→ “.\tests\userTestuser1.json”

→ ".\tests\userTestuser2.json"

→ “.\tests\userTestuser3.json”

 

Assign the state files to separate Playwright projects for scenarios such as sales order creation, purchase order creation or a Customer Insights scenario. Do not overwrite one user's state with another's: changing the file name does not change the identity. Capture and validate state separately when a scenario uses a different Dynamics 365 application or environment. 

Saved state remains subject to expiry, revocation and tenant policy. If a fresh sign-in or MFA challenge is required, stop the test and repeat the approved capture process for the affected user.

 

Apply storageState in Playwright Test

Projects are one way to associate test accounts with reusable configurations. Declare them in playwright.config.ts. Each project can use a different storageState file. 

Merge the relevant settings into your existing configuration. In my example, the project is named chromium-testuser1 and uses Chromium with storageState pointing to ./tests/userTestuser1.json. Add a project for each additional account where appropriate, and match the browser used during capture. The essential project entry is shown below; it belongs inside the configuration's projects array:

 

{
      name: 'chromium-testuser1',
      use: { ...devices['Desktop Chrome']
        ,storageState: './tests/userTestuser1.json',
       },
    }

Each test receives a fresh browser context initialized from the selected file. Client-side storage is isolated between tests, but Dynamics 365 business data is not. For parallel state-changing tests, partition both accounts and test data. 

 

Validate authentication before business actions

For this validation, I disabled only the test helper that repeats the interactive login steps, not MFA or any tenant security control. In VS Code, select the newly configured chromium-testuser1 project/profile before running the test. Check that any global setup or fixture does not overwrite the loaded state, and retain unrelated environment or test-data setup.

 

I then commented out the login helper so the test could use the configured storageState rather than repeat the login steps. The test passed without displaying a sign-in or MFA prompt.

 

Once above validation is confirmed, run the business suite under the desired project. For example:

npx playwright test --project=chromium-testuser1

 

Use the exact project name declared in playwright.config.ts. This selects that project's configuration and saved identity, it does not automatically select a single business scenario. To run only one script, add its file name, for example: 

npx playwright test R001_CreateSalesOrderAndInvoicing.spec.ts --project=chromium-testuser1 

Alternatively, configure testMatch for each project. Omitting --project runs all configured projects.

 

Apply storageState in Playwright MCP

Playwright MCP is a Model Context Protocol server that lets an AI client interact with a browser through tools, including navigation and structured accessibility snapshots. It is useful for exploring the Dynamics 365 UI, identifying locators and developing or executing regression scenarios through natural-language instructions.

Unlike Playwright Test, Playwright MCP does not automatically read playwright.config.ts or inherit its selected project. Configure the MCP server separately with the state file for the intended account.

For an isolated MCP session, pass --isolated together with --storage-state and the path to the JSON file. The server reads the file when creating the browser context. Prefer an absolute path on the machine running the server. For example, write the Windows path in the JSON configuration as "C:\Playwright\tests\userTestuser1.json". If MCP uses installed Chrome or Edge, capture the state with the corresponding channel rather than assuming browser sessions are interchangeable.

 

 

After updating the configuration, restart the MCP server or recreate its browser context so it loads the new state. In my example, I use GitHub Copilot in VS Code with Playwright MCP to perform the explicitly authorized sales order scenario shown below. I provide instructions specifying the environment and actions, without including credentials in the prompt.

 

In this run, Copilot reported that all 8 steps were completed and that a new sales order (Order ID: 001537) was created without an additional authentication prompt. 

 

Conclusion

If MFA is required for Dynamics 365 Apps testing, then using storageState separates authentication from the business scenario. Complete the approved sign-in and MFA, save the state once the application is ready, and verify it in a fresh context before running a business scenario. Playwright script testing loads the state through its configuration. Playwright MCP loads it through an isolated session configured with --storage-state.

Saved state is a practical alternative to repeated interactive sign-in when disabling MFA is not permitted. It is not a substitute for a fresh MFA challenge. Used carefully, it makes both workflows easier to maintain while keeping identity controls in place. Protect the file, refresh it when required, and continue to enforce tenant security policies.

 

Comments

*This post is locked for comments