Test Cases
Learn how to define test cases, steps, checks, and provide data using Magnitude’s testing API.
Magnitude tests are written in TypeScript using a familiar testing structure. You define test cases using the test
function, and within each test, you interact with the application using the agent
object provided in the test context.
Defining Test Cases
Test cases are the fundamental units of testing in Magnitude. You define them using the globally available test
function.
test(title, options?, testFn)
A descriptive title for your test case. This title is used in reports and logs.
Optional configuration specific to this test case.
Overrides the base URL defined in the global configuration for this specific test case.
An asynchronous function containing the logic for your test case. It receives an agent
object which provides:
The primary method for performing AI-driven actions.
The primary method for performing AI-driven visual assertions.
The Playwright Page
object, for direct Playwright interactions if needed.
The Playwright BrowserContext
object.
See Playwright Access for more details on agent.page
and agent.context
, and Low-Level AI Actions for other methods on the agent
object.
For organizing tests, see Test Groups.
Defining Actions with agent.act
The core of most tests involves defining actions using natural language descriptions. Magnitude interprets these descriptions and executes the corresponding browser interactions.
agent.act(description, options?)
A natural language description of the action(s) to perform in this step. Be descriptive and clear. You can reference data provided in the options
using curly braces (e.g., "Enter {username} in the field"
).
Optional parameters for the step, primarily used for providing data.
Provides data to be used within the step’s description
.
string
: A single string value. Useful for simple inputs.Record<string, string>
: A key-value object where keys correspond to placeholders in thedescription
(e.g.,{key}
). Values will be substituted during execution.
Providing Data to Actions
You can parameterize your actions by providing data through the options.data
argument.
Using a Data Object:
This is the most common way to provide multiple pieces of data. Keys in the data
object are referenced in the description
using {key}
syntax.
Using a String:
For actions requiring only a single piece of data, you can pass a string directly.
Magnitude treats data provided via the data
option as non-sensitive. For sensitive information like passwords or API keys, use environment variables or secure vaults and pass the values directly into the data
object (e.g., data: { password: process.env.USER_PASSWORD }
). Avoid hardcoding sensitive data.
Defining Checks with agent.check
After performing actions, you’ll often want to verify the application’s state. Use agent.check
with a natural language description of the expected outcome.
agent.check(description)
A natural language statement describing the condition to verify. The AI will evaluate this statement against the current state of the web page (DOM, visibility, text content, etc.).
Low-Level Actions (Optional)
While agent.act
is the primary way to define actions, Magnitude also provides lower-level methods for direct control when needed. This can be useful if the AI interpretation of a natural language action is ambiguous or if you need precise control over a specific interaction.
Complete Example
Here’s an example combining actions, data, and checks:
Best Practices
- Be Clear and Specific: Write action and check descriptions that are unambiguous. Instead of “Click the button”, try “Click the ‘Save Changes’ button”.
- Break Down Complex Actions: Decompose complex user flows into smaller, logical actions.
- Focus on User Intent: Describe what the user wants to achieve, not how (unless using low-level actions).
- Use Data for Parameterization: Make tests reusable by passing variable data via
options.data
. - Secure Sensitive Data: Use environment variables or other secure methods for credentials, not the
data
option directly in code. - Verify Key Outcomes: Use
agent.check
to assert critical application states after important actions.