Skip to main content

Run Custom Code in Your Tests

Need custom logic? Drop in JavaScript anywhere in your test flow. Execute code directly in the browser with full control when you need it.

The JavaScript Step

Write any JavaScript you want—async/await, DOM manipulation, API calls—it all works. Your code runs directly in the page context with access to everything: window, document, and all browser APIs.
// Example: Wait for custom element and extract data
const element = await document.querySelector('.dynamic-content');
const price = element.textContent.match(/\$(\d+)/)[1];
window.setVariable('extracted_price', price);

Set and Use Variables

Store data for later with window.setVariable(). Perfect for capturing dynamic values you’ll need in future steps:
// Save user ID from the page
const userId = document.querySelector('[data-user-id]').dataset.userId;
window.setVariable('current_user', userId);
Use these variables anywhere in your test—in AI Magic steps, assertions, click, type, or any other step type. Learn more about Variables.

Built-in Data Generation

Need test data? We’ve got you covered with Faker and Falso built right in:
// Generate realistic test data
const email = window.faker.internet.email();
const phone = window.faker.phone.number();
const address = window.falso.randAddress();

window.setVariable('test_email', email);
window.setVariable('test_phone', phone);
No imports needed—just use window.faker or window.falso and generate whatever mock data you need.

Real Browser Context

Your code runs in the actual page, not some isolated sandbox. This means you can:
  • Manipulate the DOM directly
  • Call functions defined by your app
  • Access localStorage and sessionStorage
  • Trigger custom events
  • Interact with third-party libraries loaded on the page
// Interact with your app's code
window.myApp.clearCart();
localStorage.setItem('debug_mode', 'true');
document.dispatchEvent(new Event('custom-refresh'));

Power When You Need It

Most of the time, other step types handle everything. But when you need precise control—complex calculations, custom validations, or app-specific logic—JavaScript steps have your back. Mix and match AI-powered steps with custom code. Let the AI handle the routine stuff while you only take care of edge cases.
I