> ## Documentation Index
> Fetch the complete documentation index at: https://docs.stably.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Auth with Google

> Authenticate with Google in your Playwright tests using the Stably SDK

## Background

You might want to authorize/login with Google in your tests, but doing so in your an automated web browser can have issues may be blocked by Google (as they might think this is bot traffic). This method exists to login in a way that won't trigger such a response by Google.

<Note>
  On rare occasions, Google may still prompt for a one-time passcode (OTP). You can handle this by generating a code from your `otpSecret`.
</Note>

## `context.authWithGoogle(options)`

Signs in a Google test account and applies the authenticated session to the current browser context. After this resolves, subsequent pages in the context are fully authenticated — no need to drive the login UI manually.

```typescript theme={null}
import { test } from "@stablyai/playwright-test";

test("authenticated flow", async ({ context, page }) => {
  await context.authWithGoogle({
    email: "qa@example.com",
    password: process.env.GOOGLE_TEST_PASSWORD!,
    otpSecret: process.env.GOOGLE_TEST_OTP_SECRET!,
  });

  await page.goto("https://myaccount.google.com");
  // context is now authenticated as qa@example.com
});
```

<Note>
  You can also use `authWithGoogle` stand-alone (pass your own `context`) if you import it directly:

  ```typescript theme={null}
  import { authWithGoogle } from "@stablyai/playwright-test";

  await authWithGoogle({ context, email, password, otpSecret });
  ```
</Note>

### Options

<ParamField body="email" type="string" required>
  Google account email address.
</ParamField>

<ParamField body="password" type="string" required>
  Google account password.
</ParamField>

<ParamField body="otpSecret" type="string" required>
  TOTP secret used to generate the Google 2FA code. This is the 32-character base32 secret Google shows when you click **Can't scan QR code** during authenticator setup. See [Setting up a Google test account](#setting-up-a-google-test-account) below for how to obtain this.
</ParamField>

<ParamField body="forceRefresh" type="boolean" default="false">
  Force a cache refresh server-side. Use this if you need a fresh session.
</ParamField>

<ParamField body="apiKey" type="string">
  Optional Stably API key override. Defaults to the configured API key.
</ParamField>

<Note>
  Your credentials are sent to Stably's servers to perform the Google sign-in. The authenticated session is cached server-side for reuse across test runs.
</Note>

***

## Setting up a Google test account

Before using `authWithGoogle`, you need a Google account with 2FA configured using an authenticator app. The key requirement is obtaining the **OTP secret** — the 32-character base32 string Google provides during authenticator setup.

<Tip>More detailed instructions are below in [Detailed walkthrough](#detailed-walkthrough)</Tip>

<Steps>
  <Step title="Get your Google secret">
    In Google account security settings, start authenticator setup and click **Can't scan QR code** to reveal the secret text.
  </Step>

  <Step title="Generate the verification code">
    Paste the secret into the [Stably OTP helper](https://app.stably.ai/utils/google-otp-secret) and copy the displayed 6-digit code.
  </Step>

  <Step title="Finish in Google">
    Return to Google's verification screen, paste the 6-digit code, and complete setup.
  </Step>

  <Step title="Store the secret">
    Save the OTP secret as an environment variable (e.g. `GOOGLE_TEST_OTP_SECRET`) for use in your tests.
  </Step>
</Steps>

### Detailed walkthrough

<Accordion title="Step-by-step with screenshots">
  <Steps>
    <Step title="Access Google Account Settings">
      Sign in to your test Google Workspace account at [accounts.google.com](http://accounts.google.com/).
    </Step>

    <Step title="Navigate to Security Settings">
      Select 'Security' from the left sidebar menu.

      <Frame>
        <img src="https://mintcdn.com/stablyai/gQcjGEzeMU3Scby6/images/google-auth/sidebar-security.png?fit=max&auto=format&n=gQcjGEzeMU3Scby6&q=85&s=bd94b84087b30ff9713b5a64952c081e" width="3232" height="3054" data-path="images/google-auth/sidebar-security.png" />
      </Frame>
    </Step>

    <Step title="Scroll to 'How you sign in to Google' and click 'Authenticator'">
      <Frame>
        <img src="https://mintcdn.com/stablyai/gQcjGEzeMU3Scby6/images/google-auth/authenticator.png?fit=max&auto=format&n=gQcjGEzeMU3Scby6&q=85&s=110690689cae92aca43ca223d725e0e6" width="3232" height="2562" data-path="images/google-auth/authenticator.png" />
      </Frame>
    </Step>

    <Step title="Click 'Add Authenticator' (or 'Change Authenticator App')">
      <Note>
        This will invalidate your previous Authenticator app setup.
      </Note>

      <Frame>
        <img src="https://mintcdn.com/stablyai/gQcjGEzeMU3Scby6/images/google-auth/add-authenticator.png?fit=max&auto=format&n=gQcjGEzeMU3Scby6&q=85&s=cdc2fffd46b117a89326503cd085a6e3" width="3232" height="2562" data-path="images/google-auth/add-authenticator.png" />
      </Frame>
    </Step>

    <Step title="Access OTP Secret">
      When the QR code pop-up appears:

      1. Click 'Can't scan QR code'
      2. This will reveal the OTP secret in text format

      <Note>You'll also want to store this to use as `otpSecret` with the `authWithGoogle` SDK method</Note>

      <Frame>
        <img src="https://mintcdn.com/stablyai/gQcjGEzeMU3Scby6/images/google-auth/cant-scan.png?fit=max&auto=format&n=gQcjGEzeMU3Scby6&q=85&s=e72d9efa38bbf808002a1a5cb7755343" width="3232" height="2562" data-path="images/google-auth/cant-scan.png" />
      </Frame>

      <Frame>
        <img src="https://mintcdn.com/stablyai/gQcjGEzeMU3Scby6/images/google-auth/plaintext-secret.png?fit=max&auto=format&n=gQcjGEzeMU3Scby6&q=85&s=9943b02b4f76838f2b4900dd1f2e2387" width="3232" height="2562" data-path="images/google-auth/plaintext-secret.png" />
      </Frame>
    </Step>

    <Step title="Verify OTP setup using Stably's Util">
      Open [Stably's OTP util](https://app.stably.ai/utils/google-otp-secret) in a new tab.
    </Step>

    <Step title="Paste the OTP secret into the Stably OTP Util" />

    <Step title="Copy the generated OTP">
      You'll see an OTP with a 30-second timer. Click 'Copy OTP' to copy it.
    </Step>

    <Step title="Finish authenticator setup in Google">
      Go back to the Google Accounts pop-up, click 'Next', paste the copied OTP, and click 'Verify'.

      <Frame>
        <img src="https://mintcdn.com/stablyai/gQcjGEzeMU3Scby6/images/google-auth/finish-authenticator.png?fit=max&auto=format&n=gQcjGEzeMU3Scby6&q=85&s=ea64ff15fd45f3e19922f23767a49bd5" width="3232" height="2562" data-path="images/google-auth/finish-authenticator.png" />
      </Frame>
    </Step>

    <Step title="Enable 2FA and validate second steps">
      Go back to the Google Accounts page, turn on 2FA, and under 'Second Steps', make sure only the Authenticator is enabled (you might have to sign out of the Google Account on mobile devices).
    </Step>

    <Step title="Use this Google OAuth account in your tests with the generated OTP secret" />
  </Steps>
</Accordion>
