> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.doc-detective.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.doc-detective.com/_mcp/server.

# Sign in through single sign-on

> Test pages behind single sign-on (SSO) by signing in through the provider once and saving the session on your app's own site.

With single sign-on (SSO), your app sends the reader to an identity provider to sign in, and the provider sends them back. The provider might be your company's identity provider, or an OAuth 2.0 provider behind a **Sign in with** button. Your tests follow the same redirect and sign in on the provider's page. Then they save the session that your app creates on its own site.

## Before you begin

* A test user at the identity provider that a test can sign in with. Ask your engineering or QA team whether staging uses a test tenant or test users for SSO. See [Find a testable sign-in](/docs/test-docs/sign-in).
* If the provider asks for MFA, a factor a test can complete, such as an [authenticator app code](/docs/test-docs/authenticator-app-sign-in). Push approval and SMS codes can't be tested.

Public consumer providers often challenge automated browsers with a CAPTCHA or a device check. Prefer a test tenant that your team controls.

## How the session is stored

An SSO sign-in leaves two sessions, on two sites:

* The identity provider's session lives on the provider's site, such as `idp.example.com`.
* Your app's session lives on your app's site, such as `app.example.com`. The app creates it after the redirect back.

The browser exposes only the open site's cookies and storage, so one [`saveState`](/docs/actions/savestate) saves one site. Save one file per site.

## Save your app's session after the redirect

Most tests need only your app's session. Sign in through the provider, wait until your app shows a signed-in page, and save the state there:

**`tests/sign-in.spec.json`**

```json title="tests/sign-in.spec.json"
{
  "autoRecord": false,
  "tests": [
    {
      "testId": "sign-in-sso",
      "steps": [
        { "goTo": "https://app.example.com/login" },
        { "click": "Sign in with SSO" },
        { "find": { "selector": "#username", "click": true, "type": "$IDP_USER" } },
        { "find": { "selector": "#password", "click": true, "type": ["$IDP_PASSWORD", "$ENTER$"] } },
        { "find": "Dashboard" },
        { "saveState": { "path": "app.json", "directory": "../.auth", "overwrite": true } }
      ]
    }
  ]
}
```

The `find` for `Dashboard` waits for the redirect back, so `saveState` runs on your app's site, after the app sets its session. List the spec in `beforeAll` in your config.

In each test, load the state on your app's site:

**`settings.spec.json`**

```json title="settings.spec.json"
{
  "tests": [
    {
      "steps": [
        { "goTo": "https://app.example.com" },
        { "loadState": { "path": "app.json", "directory": "../.auth" } },
        { "goTo": "https://app.example.com/settings" },
        { "find": "Sign out" },
        { "screenshot": "settings.png" }
      ]
    }
  ]
}
```

## Save the provider's session too

Save a second file when a test needs to be signed in at the provider as well. For example, a test might screenshot the provider's own pages, or your app might send the reader back to the provider during the test. After the sign-in, open the provider's site and save its state to its own file:

**`tests/sign-in.spec.json`**

```json title="tests/sign-in.spec.json"
{
  "autoRecord": false,
  "tests": [
    {
      "testId": "sign-in-sso",
      "steps": [
        { "goTo": "https://app.example.com/login" },
        { "click": "Sign in with SSO" },
        { "find": { "selector": "#username", "click": true, "type": "$IDP_USER" } },
        { "find": { "selector": "#password", "click": true, "type": ["$IDP_PASSWORD", "$ENTER$"] } },
        { "find": "Dashboard" },
        { "saveState": { "path": "app.json", "directory": "../.auth", "overwrite": true } },
        { "goTo": "https://idp.example.com" },
        { "saveState": { "path": "idp.json", "directory": "../.auth", "overwrite": true } }
      ]
    }
  ]
}
```

Then load each file on its own site, before the test continues on your app:

**`reconnect.spec.json`**

```json title="reconnect.spec.json"
{
  "tests": [
    {
      "steps": [
        { "goTo": "https://idp.example.com" },
        { "loadState": { "path": "idp.json", "directory": "../.auth" } },
        { "goTo": "https://app.example.com" },
        { "loadState": { "path": "app.json", "directory": "../.auth" } },
        { "goTo": "https://app.example.com/settings/connections" },
        { "find": "Sign out" }
      ]
    }
  ]
}
```

Cookies stay in the browser when the test opens another site, so the provider's session is still there when your app redirects to it.

## Related

* [Sign in once and reuse the session](/docs/test-docs/reuse-a-session).
* [Troubleshoot sign-in tests](/docs/test-docs/sign-in-troubleshooting).