| ← Back to Overview | Next Workshop: Copilot CLI → |
|---|
Set up Playwright (TypeScript), generate a first UI test with a background agent (Copilot Chat in VS Code), and run it locally.
Tailspin Games asks you to prevent regressions in the new T‑Rex runner. You'll draft smoke tests and let an agent help with boilerplate.
As a developer on the Tailspin Games team, you've been tasked with ensuring the quality of the newly developed T‑Rex Runner game. The team wants to prevent any regressions as new features are added, so you'll set up automated testing using Playwright. Rather than writing all the boilerplate yourself, you'll leverage GitHub Copilot as a background agent to help generate the initial test structure.
Before starting this workshop:
- Node.js 20+ installed
- VS Code installed
- GitHub Copilot and GitHub Copilot Chat extensions enabled
- T‑Rex Runner application downloaded and extracted
First, let's set up Playwright in the client application.
-
Open a terminal and navigate to the client directory:
cd trex-agents-workshop/apps/client -
Initialize Playwright with TypeScript support:
npm create @playwright/test@latest -- --yes
This command will:
- Install Playwright and its dependencies
- Set up configuration files
- Choose TypeScript as the default language
-
Install the required browsers:
npx playwright install
📚 Reference: For more details on Playwright's TypeScript setup, see the official Playwright TS docs.
The T‑Rex Runner consists of two parts: a backend API and a frontend UI. You'll need both running to test the application.
-
Terminal A - Start the backend server:
npm run dev -w apps/server
You should see output indicating the server is running (typically on port 3000).
-
Terminal B - Start the frontend client:
npm run dev -w apps/client
The frontend will typically run on http://localhost:5173
-
Open your browser and navigate to http://localhost:5173 to verify the app is running. You should see the T‑Rex Runner game.
Now comes the exciting part - using GitHub Copilot as a background agent to help generate your first test!
-
In VS Code, open the Copilot Chat panel (you can use the Copilot icon in the sidebar or press
Ctrl+Shift+I/Cmd+Shift+I). -
In Copilot Chat, enter the following prompt:
Create a Playwright test in TypeScript that opens http://localhost:5173, asserts the canvas is visible, performs one jump, waits until the score increases, and takes a trace. -
Review the generated code. Copilot should provide you with a test that:
- Imports necessary Playwright modules
- Sets up a test case
- Navigates to the application
- Locates the game canvas
- Simulates a jump action (typically space bar or click)
- Waits for the score to update
- Includes tracing configuration
-
Create a new file:
apps/client/tests/trex-smoke.spec.ts -
Copy the generated code into this file.
-
Add the following configuration at the top of your test file to enable tracing on the first retry:
test.use({ trace: 'on-first-retry' });
💡 Tip: The
trace: 'on-first-retry'configuration helps with debugging by recording a trace only when a test fails and is retried.
Now let's execute the test and view the results.
-
In the terminal (from the
apps/clientdirectory), run:npx playwright test -
View the HTML report:
npx playwright show-report
-
Review the test results:
- Check if the test passed
- Look at the execution time
- Examine any screenshots or traces if the test failed
For more resilient tests, it's best practice to use accessibility-based locators rather than CSS selectors.
-
Open
apps/client/index.html -
Find the canvas element and add an
aria-label:<canvas id="game-canvas" aria-label="game-canvas"></canvas>
-
Update your test to use the accessibility locator:
// Instead of: await page.locator('#game-canvas') await page.getByLabel('game-canvas')
-
Re-run your tests to ensure they still pass:
npx playwright test
📚 Why accessibility labels? Using
aria-labelandgetByLabel()creates tests that are:
- More resilient to UI changes
- Better for accessibility
- Easier to understand and maintain
In this workshop, you:
✅ Set up Playwright with TypeScript for modern browser testing
✅ Used a background agent (GitHub Copilot Chat) to generate test code
✅ Ran automated tests and viewed detailed reports
✅ Applied best practices for creating resilient, accessibility-friendly locators
✅ Understood the value of using AI to draft boilerplate test code while you validate and refine
- Background agents can accelerate test creation: Instead of writing boilerplate from scratch, use Copilot to generate the initial structure
- Always validate AI-generated code: Review and test the generated code to ensure it meets your requirements
- Accessibility-first locators are more resilient: Using ARIA labels makes tests more stable and improves your app's accessibility
- Tracing helps debugging: The
trace: 'on-first-retry'option provides valuable debugging information when tests fail
Test fails to find elements:
- Ensure both the server and client are running
- Check that the URL in the test matches your local development server
- Verify the canvas element exists by manually opening the browser
Playwright installation issues:
- Try running
npx playwright install --force - Check your Node.js version (should be 20+)
Browser doesn't open:
- Run with headed mode to see what's happening:
npx playwright test --headed - Check if ports 3000 and 5173 are available
Want to go further? Try these challenges:
-
Add more test scenarios:
- Test the pause/resume functionality
- Test the restart feature
- Verify high score persistence
-
Test different browsers:
- Configure Playwright to run tests in Chrome, Firefox, and Safari
- Compare execution times across browsers
-
Add visual regression testing:
- Use Playwright's screenshot comparison features
- Set up baseline images for key game states
-
Create a test helper:
- Extract common game interactions into reusable functions
- Build a Page Object Model for the T‑Rex Runner
Congratulations! You've set up your testing foundation and learned how to use a background agent to accelerate test creation.
In Workshop 2, you'll learn how to use the GitHub Copilot CLI to create reusable skills for test orchestration and iteratively improve your testing workflow.
- Playwright TypeScript Documentation
- GitHub Copilot Documentation
- Playwright Best Practices
- Web Accessibility Testing
| ← Back to Overview | Next Workshop: Copilot CLI → |
|---|