Playwright Test Mode
Auto-Browse seamlessly integrates with Playwright’s testing framework, allowing you to combine natural language commands with Playwright’s powerful testing capabilities.
Basic Integration
Here’s how to use Auto-Browse in your Playwright tests:
import { test, expect } from "@playwright/test";
import { auto } from "auto-browse";
test("basic test example", async ({ page }) => {
await page.goto("https://example.com");
// Use natural language commands with Auto-Browse
const headerText = await auto("get the header text");
expect(headerText).toBeTruthy();
// Mix with regular Playwright commands
await auto('type "Hello World" in the search box');
await page.keyboard.press("Enter");
});
Page Context
When using Auto-Browse with Playwright tests, you can explicitly pass the page context:
test("with explicit page context", async ({ page }) => {
await page.goto("https://example.com");
// Pass page context explicitly
await auto("click the login button", { page });
// Auto-Browse will use the provided page context
await auto('type "user@example.com" in the email field', { page });
});
Auto-Detection Mode
Auto-Browse can automatically detect the current page context:
test("using auto-detection", async ({ page }) => {
await page.goto("https://example.com");
// No need to pass page parameter
await auto("click the login button");
await auto('type "user@example.com" in the email field');
await auto("click Submit");
});
Test Organization
Test Fixtures
Create reusable login fixtures:
import { test as base } from "@playwright/test";
import { auto } from "auto-browse";
// Define a custom test fixture
type AutoBrowseFixtures = {
loginAsUser: () => Promise<void>;
};
const test = base.extend<AutoBrowseFixtures>({
loginAsUser: async ({ page }, use) => {
const login = async () => {
await auto("click the login button");
await auto('type "user@example.com" in the email field');
await auto('type "password123" in the password field');
await auto("click Submit");
await auto("verify dashboard is visible");
};
await use(login);
}
});
// Use the fixture in tests
test("user workflow", async ({ loginAsUser }) => {
await loginAsUser();
await auto("verify user is logged in");
});
Test Groups
Organize related tests:
test.describe("authentication flows", () => {
test("successful login", async ({ page }) => {
await auto("go to login page");
await auto('type "valid@user.com" in email');
await auto('type "correct-password" in password');
await auto("click login");
await auto("verify dashboard is shown");
});
test("invalid credentials", async ({ page }) => {
await auto("go to login page");
await auto('type "invalid@user.com" in email');
await auto('type "wrong-password" in password');
await auto("click login");
await auto("verify error message is shown");
});
});
Visual Testing
Combine Auto-Browse with Playwright’s visual testing:
test("visual regression", async ({ page }) => {
await auto("go to homepage");
await auto("scroll to bottom of page");
// Use Playwright's snapshot feature
await expect(page).toHaveScreenshot("homepage.png");
// Use Auto-Browse for interactive elements
await auto("open mobile menu");
await expect(page).toHaveScreenshot("mobile-menu.png");
});
Example of form testing with validation:
test("form submission", async ({ page }) => {
await auto("go to registration form");
// Fill form with invalid data
await auto('type "invalid-email" in email field');
await auto("click submit");
await auto("verify email error message is shown");
// Fill form with valid data
await auto('type "valid@email.com" in email field');
await auto('type "John Doe" in name field');
await auto('select "USA" from country dropdown');
await auto("click submit");
// Verify success
await auto("verify success message is shown");
await auto("verify confirmation email text is present");
});
Best Practices
- Prefer Natural Language
// Good - clear and maintainable
await auto("click the submit button in the form");
// Less maintainable
await page.click("#submit-btn");
- Combine with Playwright when needed
// Use Auto-Browse for high-level actions
await auto("log in as admin user");
// Use Playwright for specific checks
await expect(page.locator(".admin-panel")).toBeVisible();
- Descriptive Test Names
test("user can complete checkout process", async () => {
await auto("add item to cart");
await auto("proceed to checkout");
await auto("fill shipping information");
await auto("complete payment");
await auto("verify order confirmation");
});
Debugging Tips
- Take Snapshots
test("debug example", async () => {
await auto("take a snapshot before action");
await auto("perform complex action");
await auto("take a snapshot after action");
});
- Use Debug Mode
test("with debug", async () => {
// Enable debug mode for verbose logging
process.env.AUTO_BROWSE_DEBUG = "true";
await auto("perform action with detailed logs");
});
Next Steps