Playwright for Beginners. Install and run tests using the CLI
Table of Contents
Introduction
This tutorial will guide you through the basics of using Playwright, a powerful tool for browser automation. You will learn how to install Playwright, run tests, and explore its features using the command line interface (CLI). Whether you are a beginner or looking for a refresher, this guide will help you get started effectively.
Step 1: Install Playwright
To start using Playwright, you need to install it on your system. Follow these steps:
-
Open your terminal or command prompt.
-
Navigate to your project directory where you want to set up Playwright.
-
Run the following command to install Playwright via npm:
npm install playwright -
Verify the installation by checking the current version with:
npx playwright --version
Practical Tip
Ensure you have Node.js installed on your machine, as Playwright requires it to function.
Step 2: Run Your First Test
After installation, it’s time to run your first test. Here’s how to do it:
-
Create a new JavaScript file, for example
test.js. -
Write a simple test script. Here’s an example:
const { chromium } = require('playwright'); (async () => { const browser = await chromium.launch(); const page = await browser.newPage(); await page.goto('https://example.com'); console.log(await page.title()); await browser.close(); })(); -
Save the file.
-
Execute the test by running:
node test.js
Common Pitfall
Ensure that your script does not have syntax errors, as these will prevent it from running correctly.
Step 3: Run Tests in Headed Mode
Watching your tests run can help with debugging. To run your tests in headed mode:
-
Modify the launch function in your test script:
const browser = await chromium.launch({ headless: false }); -
Save your changes and run the test again. You should see the browser window open as the test executes.
Step 4: Target Specific Browsers
Playwright allows you to specify which browser to use. You can run your tests in Chrome, Firefox, or WebKit. Here’s how:
-
Change the browser instance in your script:
const { firefox } = require('playwright'); -
Replace
chromiumwithfirefoxorwebkitdepending on the browser you want to test. -
Run your test again to see the changes.
Step 5: Running Specific Tests
If you have multiple tests in a file and want to run a specific one, you can do so by:
-
Using the
test.onlymethod:test.only('my specific test', async () => { // your test code here }); -
Execute your tests, and only the specified test will run.
Step 6: View Playwright HTML Report
To analyze your test results:
-
Add the following to your test file to generate a report:
await page.screenshot({ path: 'screenshot.png' }); -
After running your tests, check the directory for the generated HTML report.
Step 7: Run Tests in UI Mode
For a more interactive experience, you can run tests in UI mode:
-
Use the command:
npx playwright test --ui -
This launches a user interface that allows you to interact with your tests visually.
Step 8: Update Playwright
Keeping Playwright up to date is crucial for using the latest features and improvements. To update Playwright:
-
Run the following command in your terminal:
npm update playwright -
Verify the update by checking the version again:
npx playwright --version
Conclusion
You have now learned how to install Playwright, run tests, and explore its various features through the command line interface. With these steps, you can start automating your browser tasks effectively. For further learning, consider exploring more advanced features and integrations within Playwright. Happy testing!