page.ai.assert()

Assert a natural-language condition about the current page, with optional retries for dynamic UIs.

Asserts that a natural-language condition about the current page holds true. If the condition is not met, the method throws and the test fails.

Signature

page.ai.assert(
  assertion: string,
  options?: {
    retries?: number;
    retryDelaySeconds?: number;
    gptClient?: GptClient | LanguageModel;
  }
): Promise<void>

Parameters

ParameterTypeDefaultDescription
assertionstringPlain-English description of the condition to check
options.retriesnumber0How many times to retry the assertion if it initially fails. Useful for dynamic UIs that need time to settle.
options.retryDelaySecondsnumber0Seconds to wait between retry attempts
options.gptClientGptClient | LanguageModelProject defaultOverride the AI provider for this call

How it works

The AI evaluates the assertion against:

  • The current page URL and title
  • A viewport screenshot
  • The page's text content

Each retry captures a fresh screenshot and page context, giving animations and async updates time to complete.

Basic usage

import { test } from 'donobu';

test('checkout confirmation', async ({ page }) => {
  await page.goto('https://shop.example.com/cart');
  await page.ai('Add the first item to the cart and purchase the item');

  await page.ai.assert('An order confirmation message is visible');
});

With retries

await page.ai.assert(
  'The loading spinner has disappeared and content is visible',
  {
    retries: 5,
    retryDelaySeconds: 2,
  },
);

When to use page.ai.assert vs. Playwright's expect

Use page.ai.assert when…Use Playwright's expect when…
The condition is subjective or involves understanding the page layoutYou are checking an exact text string, number, or URL
The relevant information spans multiple elementsYou already have a locator for the element you want to check
You want retries without writing a waitFor loopYou need to check a computed value extracted by page.ai.extract
// Good use of page.ai.assert — subjective layout condition
await page.ai.assert('The sidebar navigation is collapsed on mobile');

// Better as expect — exact string check
await expect(page.locator('h1')).toHaveText('Welcome back');

Throws

Throws ToolCallFailedException if the assertion fails after all retries are exhausted. This exception carries the following properties:

PropertyTypeDescription
toolNamestringThe name of the tool that failed (will be 'assert')
resultToolCallResultThe full result object from the failed assertion
messagestringA human-readable description including the serialised result

ToolCallResult has an isSuccessful field (which will be false) and a forLlm string field that contains a description of what the assertion checked and why it failed.