Frontend Development 3 min read

Data-Driven Testing with Playwright and openpyxl in Python

This tutorial explains how to perform data‑driven testing by installing Playwright, reading test data from Excel with openpyxl, simulating user actions, and writing reusable unittest test cases that drive browser interactions based on external data.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Data-Driven Testing with Playwright and openpyxl in Python

Data‑driven testing separates test data from scripts, making tests flexible, maintainable, and reusable. This guide shows how to install the required libraries, read test data from an Excel file using openpyxl , simulate user actions with Playwright, and write test cases using unittest .

Install the necessary packages:

pip install unittest-playwright
pip install playwright

Read Excel data:

from playwright.sync_api import Playwright, Browser, BrowserContext, Page
import openpyxl

def read_excel(file_path):
    # Load Excel file
    wb = openpyxl.load_workbook(file_path)
    # Get first sheet
    ws = wb.active
    data = []
    # Iterate rows
    for row in ws.rows:
        row_data = []
        for cell in row:
            row_data.append(cell.value)
        data.append(row_data)
    return data

Helper to input text on a page:

from playwright.sync_api import Playwright, Browser, BrowserContext, Page

def input_text(page: Page, selector: str, text: str):
    page.fill(selector, text)

Define the test case class:

import unittest

class Test(unittest.TestCase):
    def setUp(self):
        # Initialize Playwright
        self.playwright = Playwright()
        self.browser = self.playwright.chromium.launch(headless=False)

    def tearDown(self):
        # Close browser
        self.browser.close()

    def test(self):
        # Read test data
        data = read_excel('test.xlsx')
        # Open page
        page = self.browser.new_page()
        page.goto(data[0][0])
        # Input text
        input_text(page, data[0][1], data[0][2])
        # Wait for navigation
        page.waitForNavigation()
        # Verify element exists
        assert page.locator(data[0][3]) is not None

if __name__ == '__main__':
    unittest.main()

The example demonstrates using unittest‑playwright to manage test cases, openpyxl to load external data, and Playwright to drive browser actions, allowing you to adapt the data and steps for various testing scenarios.

PythonAutomationPlaywrightunittestopenpyxlData-Driven Testing
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.