How to Run Playwright Tests in Bitbucket – Step-by-Step Guide
Last updated on: February 25, 2025
41 Views
Notesly Team
Working Professional
Share : 
 
1. Introduction
What is Playwright?
- Playwright is an open-source browser automation framework developed by Microsoft.
- It supports Chromium, Firefox, and WebKit for end-to-end testing, enabling cross-browser testing in a single API.
- Features include headless/headed execution, mobile emulation, network interception, and auto-waiting for elements.
Why Run Playwright Tests in Bitbucket?
- Bitbucket Pipelines provides native CI/CD integration, allowing automated test execution on every code commit.
- Combining Playwright with Bitbucket ensures consistent testing environments, faster feedback loops, and seamless collaboration across teams.
- Bitbucket’s YAML-based configuration simplifies defining workflows for running Playwright tests.
2. Prerequisites
Node.js and npm Installation
- Playwright requires Node.js (v14+) and npm (Node Package Manager).
- Guide students to install Node.js from nodejs.org and verify with:
node -v && npm -v
Playwright Setup
- Install Playwright in a project using:
npm init playwright@latest
- This command sets up a Playwright project, installs browsers, and creates sample tests.
Bitbucket Account and Repository
- Students need a Bitbucket account and a Git repository to host their code.
- Ensure they can push code to Bitbucket and enable Pipelines in repository settings.
3. Setting Up a Playwright Project
Initialize a Playwright Project
- Walk through the Playwright initialization process:
- Choose TypeScript/JavaScript.
- Select browsers to test (default: all).
- Configure folder structure (e.g., tests/,playwright.config.ts).
Writing a Sample Playwright Test
- Create a simple test (e.g., navigate to a page and assert a title):

- Explain test structure: test blocks, page fixtures, and assertions.
4. Integrating Playwright with Bitbucket Pipelines
Introduction to Bitbucket Pipelines
- Bitbucket Pipelines uses a YAML file (bitbucket-pipelines.yml) to define CI/CD workflows.
- Pipelines run in isolated Docker containers, triggered by commits or pull requests.
Configuring bitbucket-pipelines.yml
- Example configuration for Playwright:

- Caching: Speeds up future runs by reusing node_modules.
- Artifacts: Save test reports for later review.
Installing Dependencies in the Pipeline
- Use npx playwright install --with-depsto install browsers and OS dependencies in CI.
5. Running Playwright Tests in CI/CD
Configuring Headless vs. Headed Mode
- Playwright runs in headless mode by default (no UI). To run headed:
npx playwright test --headed
- In CI, headless is recommended for speed and resource efficiency.
Parallel Test Execution
- Split tests across workers using the --workersflag:
npx playwright test --workers 4
- Configure parallelism in playwright.config.ts:
export default { workers: 4 };
Handling Browser Binaries in CI
- Bitbucket’s Docker environment may lack browser dependencies. Use:
npx playwright install-deps
- to install system-level dependencies (e.g., libnss3for Chromium).
6. Artifacts and Reporting
Generating Playwright Test Reports
- Playwright auto-generates an HTML report in playwright-report/after tests run.
- Customize reports with --reporter(e.g.,line,dot,html).
Storing Reports as Pipeline Artifacts
- Define artifactsinbitbucket-pipelines.ymlto save reports:
artifacts:
- playwright-report/**
- Students can download reports from the Pipelines UI post-execution.
7. Troubleshooting Common Issues
Debugging Pipeline Failures
- Check Pipeline logs for errors (e.g., missing dependencies, test failures).
- Reproduce issues locally with npx playwright test.
Handling Timeouts and Flaky Tests
- Increase timeouts in playwright.config.ts:
export default { timeout: 60000 }; // 60 seconds
- Use test.slow()to mark slow tests or retries with--retries.
Managing Browser Dependencies
- Ensure Docker image includes required libraries (e.g., use ubuntu:latestas the base image).
8. Best Practices
Caching Dependencies
- Cache node_modulesand Playwright browsers:
caches:
- node
- playwright
Using Environment Variables
- Store secrets (e.g., credentials) in Bitbucket Repository Variables and access them in tests via process.env.
Optimizing Test Execution
- Split tests into shards for parallel runs:
npx playwright test --shard=1/3
1. Trigger Playwright Tests Using Bitbucket API
Why Use the API?
- Run Playwright tests on-demand (e.g., via scripts or external tools).
- Integrate testing into custom workflows (e.g., nightly runs, post-deployment checks).
Steps to Implement:
- Bitbucket REST API Basics:
- Authentication: Use App Passwords or OAuth 2.0.
- Endpoint: POST /2.0/repositories/{workspace}/{repo_slug}/pipelines/
- Trigger Pipelines via cURL:

- Replace {workspace},{repo}, and branch/pattern as needed.
- Webhooks for Event-Driven Testing:
- Configure Bitbucket webhooks to trigger tests on events like pull requests or pushes.
2. Generating and Accessing Playwright Reports
Playwright’s Built-in Reports:
- HTML Report: Interactive dashboard with logs, screenshots, and traces.
npx playwright test --reporter=html
- JUnit/JSON Reports: For integration with tools like Jenkins or Azure DevOps.
npx playwright test --reporter=junit
Custom Reporters:
- Use third-party reporters (e.g., Allure) or build custom ones.
- Example in playwright.config.ts:
export default {
reporter: [['line'], ['json', { outputFile: 'results.json' }]]
};
Automating Report Sharing:
- Upload reports to cloud storage (e.g., AWS S3).
- Use Bitbucket Notifications to email reports.
3. Debugging Failed Tests
Playwright Trace Viewer:
- Capture traces for failed tests:
test('Login test', async ({ page }) => {
await page.context().tracing.start({ screenshots: true, snapshots: true });
// Test steps...
await page.context().tracing.stop({ path: 'trace.zip' });
});
- View traces locally:
npx playwright show-trace trace.zip
CI Debugging Tips:
- Run tests in headed mode temporarily in CI (add --headed).
- Use timeout overrides for slow CI environments:
test('Slow test', async ({ page }) => {
test.slow();
// Test steps...
});
Analyzing CI Logs:
- Check Bitbucket Pipeline logs for errors like missing dependencies or browser crashes.
4. Maintaining and Updating Tests
Version Compatibility:
- Keep Playwright updated:
npm update @playwright/test
- Use npx playwright installto update browsers.
Refactoring Tests:
- Use the Page Object Model (POM) to organize code.
- Example:
// models/HomePage.ts
export class HomePage {
constructor(private page: Page) {}
async navigate() {
await this.page.goto('https://example.com');
}
}
Handling Flaky Tests:
- Enable retries in playwright.config.ts:
export default { retries: 2 }; // Retry failed tests twice
- Use test.describe.configure({ retries: 3 })for specific test suites.
5. Advanced Topics
Testing in Multiple Environments:
- Use environment variables to switch configurations:
# bitbucket-pipelines.yml
- step:
script:
- npx playwright test --config=environments/staging.config.ts
Security Best Practices:
- Store secrets (e.g., passwords) in Bitbucket Repository Variables (not in code).
- Restrict Pipeline permissions to least privilege.
Integrating with Test Management Tools:
- Export results to tools like TestRail or Xray using custom scripts.
Additional Considerations
- Performance Testing: Use Playwright to measure page load times or Lighthouse metrics.
- Visual Regression Testing: Integrate tools like Percy or Happo.
- Cross-Browser Testing: Configure multiple browsers in playwright.config.ts.
Conclusion
- Recap steps: setup Playwright, configure Bitbucket Pipelines, run tests in CI, and analyze reports.
- Emphasize benefits: automated regression testing, cross-browser compatibility, and faster releases.
Additional Resources
- Playwright Docs: playwright.dev
- Bitbucket Pipelines: Bitbucket CI/CD Guide
Key Takeaways for Students
- Playwright simplifies cross-browser testing with a unified API.
- Bitbucket Pipelines automates test execution, ensuring code quality before deployment.
- Use artifacts, caching, and parallelization to optimize CI/CD workflows.
