Building an Internal Playwright Testing CLI

Modern development workflows increasingly rely on automation, especially for testing web applications across browsers. While Playwright already provides a powerful command-line interface (CLI), many teams benefit from building a custom internal CLI tailored to their testing environments, use cases, and automation pipelines.

In this blog, we’ll explore how to build an internal Playwright testing CLI that can run tests, manage configurations, generate reports, and integrate seamlessly into CI/CD systems.


🚀 Why Build a Custom Playwright CLI?

Although Playwright’s built-in CLI is robust, creating your own CLI allows:

  • Simplified commands for non-technical users or QA teams
  • Pre-configured setups for different environments
  • Integration with custom logic, like Slack alerts or test data seeding
  • Enhanced reporting or logging features
  • Consistency across local and CI runs

By abstracting repetitive tasks, your internal CLI ensures cleaner workflows and fewer errors.


🛠️ Step-by-Step: Building the CLI

1. Set Up Your Playwright Project

Start by initializing a Playwright project if you haven’t already:

bash

npx playwright init

This creates a basic structure with examples and Playwright test configuration.


2. Install CLI Tools

You can build the CLI using Node.js and the commander or yargs library to handle arguments and options.

Install commander:

bash

npm install commander

Create a new file: cli.js


3. Create CLI Commands

Here's a basic cli.js using commander:

javascript


#!/usr/bin/env node

const { exec } = require('child_process');

const { Command } = require('commander');

const program = new Command();


program

  .name('playwright-cli')

  .description('Internal CLI for Playwright Test Automation')

  .version('1.0.0');


program

  .command('test')

  .description('Run all Playwright tests')

  .option('-e, --env <environment>', 'Set the test environment')

  .action((options) => {

    const env = options.env || 'dev';

    console.log(`Running tests in ${env} environment...`);

    exec(`npx playwright test --project=${env}`, (error, stdout, stderr) => {

      if (error) {

        console.error(`Error: ${stderr}`);

      } else {

        console.log(stdout);

      }

    });

  });


program

  .command('report')

  .description('Generate HTML test report')

  .action(() => {

    exec('npx playwright show-report', (error, stdout, stderr) => {

      if (error) {

        console.error(`Report error: ${stderr}`);

      } else {

        console.log(stdout);

      }

    });

  });

program.parse(process.argv);

Make it executable:

bash

chmod +x cli.js

Now you can run your internal CLI using:

bash

./cli.js test --env=staging

./cli.js report


⚙️ Extend the CLI Functionality

You can enhance the CLI with:

  • seed: Command to populate test data into your database
  • cleanup: Delete test artifacts after each run
  • alert: Integrate with Slack or email for test result notifications
  • debug: Launch browser in debug mode for failed tests

Each command can wrap scripts, APIs, or environment-specific setups.


📦 Integrate with CI/CD

Use the internal CLI in your CI pipelines (e.g., GitHub Actions, GitLab, Jenkins):

yaml

- name: Run Playwright Tests

  run: ./cli.js test --env=ci

This abstracts environment complexity from the CI config and promotes consistency across local and remote executions.


✅ Best Practices

  • Modularize your CLI functions for scalability
  • Use .env files for environment-specific configs
  • Log test summaries and exit codes clearly
  • Keep documentation for team members
  • Validate command inputs to prevent errors

📝 Conclusion

Building an internal Playwright testing CLI empowers your QA and development teams with a user-friendly, consistent, and powerful tool. It simplifies complex testing workflows, bridges the gap between manual and automated testing, and ensures test automation becomes a seamless part of your development lifecycle. With just a little Node.js scripting, you can create a robust CLI that fits perfectly into your project’s needs.

Learn Playwright Testing Training
Read More: Measuring Performance Timings in Playwright


Visit IHUB Talent Institute Hyderabad
Get Direction

Comments

Popular posts from this blog

How to Use Tosca's Test Configuration Parameters

Using Hibernate ORM for Fullstack Java Data Management

Creating a Test Execution Report with Charts in Playwright