|
| 1 | +--- |
| 2 | +sidebar_position: 5 |
| 3 | +--- |
| 4 | + |
| 5 | +import YouTubeVideoEmbed from '@site/src/components/HomepageFeatures/YouTubeVideoEmbed'; |
| 6 | + |
| 7 | +# 🎥 Recording Actions |
| 8 | + |
| 9 | +Playwright MCP allows you to record your browser interactions and automatically generate test specifications. This feature helps you create automated tests without writing code manually. |
| 10 | + |
| 11 | +## Getting Started |
| 12 | + |
| 13 | +To start recording your actions and generate test files, you'll need to: |
| 14 | + |
| 15 | +1. Start a recording session |
| 16 | +2. Perform your actions using MCP tools |
| 17 | +3. End the session to generate the test file |
| 18 | + |
| 19 | +### Example |
| 20 | + |
| 21 | +Here's a complete example of recording actions and generating a test spec: |
| 22 | + |
| 23 | +1. First, start a new recording session by specifying: |
| 24 | + - Where to save the generated tests (`tests/generated` directory) |
| 25 | + - What to name your tests (using 'LoginTest' as a prefix) |
| 26 | + - Whether to include helpful comments in the generated code |
| 27 | + |
| 28 | +2. Then perform the test actions: |
| 29 | + - Navigate to the Sauce Demo website (https://www.saucedemo.com) |
| 30 | + - Enter "standard_user" in the username field |
| 31 | + - Enter "secret_sauce" in the password field |
| 32 | + - Click the login button |
| 33 | + |
| 34 | +3. Finally, end the recording session to generate your test file |
| 35 | + |
| 36 | +The generated test file will look something like this: |
| 37 | + |
| 38 | +```typescript |
| 39 | +import { test, expect } from '@playwright/test'; |
| 40 | + |
| 41 | +test('LoginTest_2024-03-23', async ({ page }) => { |
| 42 | + // Navigate to the login page |
| 43 | + await page.goto('https://www.saucedemo.com'); |
| 44 | + |
| 45 | + // Fill in username |
| 46 | + await page.fill('#user-name', 'standard_user'); |
| 47 | + |
| 48 | + // Fill in password |
| 49 | + await page.fill('#password', 'secret_sauce'); |
| 50 | + |
| 51 | + // Click login button |
| 52 | + await page.click('#login-button'); |
| 53 | + |
| 54 | + // Verify successful login |
| 55 | + await expect(page).toHaveURL(/.*inventory.html/); |
| 56 | +}); |
| 57 | +``` |
| 58 | + |
| 59 | +## Configuration Options |
| 60 | + |
| 61 | +When starting a recording session, you can configure several options: |
| 62 | + |
| 63 | +- **outputPath**: Directory where the generated test files will be saved |
| 64 | +- **testNamePrefix**: Prefix for the generated test names |
| 65 | +- **includeComments**: Whether to include descriptive comments in the generated tests |
| 66 | + |
| 67 | +For example, you might configure your session to: |
| 68 | +- Save tests in a 'tests/generated' folder |
| 69 | +- Name tests with a 'MyTest' prefix |
| 70 | +- Include helpful comments in the generated code |
| 71 | + |
| 72 | +## Session Management |
| 73 | + |
| 74 | +You can manage your recording sessions using these tools: |
| 75 | + |
| 76 | +- **get_codegen_session**: Retrieve information about a recording session |
| 77 | +- **clear_codegen_session**: Clean up a recording session without generating a test |
| 78 | + |
| 79 | +These tools help you check the status of your recording or clean up if you want to start over without generating a test file. |
| 80 | + |
| 81 | +## Best Practices |
| 82 | + |
| 83 | +1. **Organize Tests**: Use meaningful test name prefixes to organize your generated tests |
| 84 | +2. **Clean Up**: Always end or clear your sessions after recording |
| 85 | +3. **Verify Actions**: Include verification steps in your recordings |
| 86 | +4. **Maintain Context**: Keep related actions in the same recording session |
| 87 | +5. **Documentation**: Add comments during recording for better test maintainability |
| 88 | + |
| 89 | +## Running Generated Tests |
| 90 | + |
| 91 | +To run your generated tests, use the Playwright test runner: |
| 92 | + |
| 93 | +```bash |
| 94 | +npx playwright test tests/generated/logintest_*.spec.ts |
| 95 | +``` |
| 96 | + |
| 97 | +:::tip |
| 98 | +You can modify the generated test files to add additional assertions, setup, or teardown code as needed. |
| 99 | +::: |
0 commit comments