Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Docker Compose gets new features for building and running agents

      July 10, 2025

      Why Enterprises Are Choosing AI-Driven React.js Development Companies in 2025

      July 10, 2025

      Unmasking The Magic: The Wizard Of Oz Method For UX Research

      July 10, 2025

      Newest LF Decentralized Trust Lab HOPrS identifies if photos have been altered

      July 9, 2025

      Fed up with AI scraping your content? This open-source bot blocker can help – here’s how

      July 10, 2025

      It’s Ubisoft’s most polished game in years — Assassin’s Creed Shadows on Xbox Series X has a fantastic Amazon Prime Day discount

      July 10, 2025

      I’ve accepted that Adobe subscriptions are part of my creative life — but with these discounts, it stings much less than it used to

      July 10, 2025

      ChatGPT falls for a “dead grandma” scam and generates Microsoft Windows 7 activation keys — but they’re useless

      July 10, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      NativePHP for Mobile v1.1: Smaller, Smarter, and Ready to Scale

      July 10, 2025
      Recent

      NativePHP for Mobile v1.1: Smaller, Smarter, and Ready to Scale

      July 10, 2025

      Custom Object Casting in Laravel Models

      July 10, 2025

      PHP 8.5 Introduces an INI Diff Option

      July 10, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Mozilla VPN Linux App is Now Available on Flathub

      July 10, 2025
      Recent

      Mozilla VPN Linux App is Now Available on Flathub

      July 10, 2025

      It’s Ubisoft’s most polished game in years — Assassin’s Creed Shadows on Xbox Series X has a fantastic Amazon Prime Day discount

      July 10, 2025

      I’ve accepted that Adobe subscriptions are part of my creative life — but with these discounts, it stings much less than it used to

      July 10, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Playwright Fixtures in Action : Create Reusable and Maintainable Tests

    Playwright Fixtures in Action : Create Reusable and Maintainable Tests

    May 21, 2025

    Setting up and tearing down test environments can be a repetitive and error-prone process in end-to-end testing. This is especially true when dealing with complex workflows or multiple test configurations. Enter Playwright Fixtures a built-in feature of Playwright Test that allows testers to define modular, reusable, and maintainable setup and teardown logic. Fixtures streamline your test code, eliminate redundancy, and ensure consistency across test runs. Whether you’re initializing browsers, setting up authentication states, or preparing test data, fixtures help you keep your test environment under control. In this blog, we’ll explore how Playwright Fixtures work, their built-in capabilities, how to create and override custom fixtures, automatic fixtures and fixture timeouts. You’ll leave with a comprehensive understanding of how to leverage fixtures to build robust and maintainable Playwright test suites.

    Related Blogs

    Playwright Visual Testing: A Comprehensive Guide to UI Regression

    Playwright Report Portal Integration Guide

    What Are Playwright Fixtures?

    Playwright Fixtures are reusable components in the @playwright/test framework used to define the setup and teardown logic of your test environment. Think of them as the building blocks that ensure your browser contexts, authentication sessions, and test data are ready to go before each test begins.

    Fixtures help manage:

    • Browser and context initialization
    • Login sessions and cookies
    • Data preparation and cleanup
    • Consistent configuration across tests

    By centralizing these operations, fixtures reduce boilerplate and boost code clarity. They prevent duplication of setup logic, reduce test flakiness, and make the tests more scalable and maintainable. To better illustrate the practical benefits of Playwright Fixtures, let’s dive into a realistic scenario that many testers frequently encounter validating the checkout flow in an e-commerce application.

    Challenges in Repetitive Test Setup

    Repeatedly preparing test conditions such as initializing browser contexts, logging in users, and setting up shopping carts for each test case can lead to redundant, bloated, and error-prone test scripts. This redundancy not only slows down the testing process but also increases maintenance efforts and potential for errors.

    Streamlining Test Automation with Playwright Fixtures

    Playwright Fixtures significantly improve this situation by allowing testers to define modular and reusable setup and teardown procedures. Let’s explore how you can use Playwright Fixtures to simplify and streamline your e-commerce checkout testing scenario.

    Step 1: Define an Authenticated User Fixture

    This fixture handles user authentication once, providing an authenticated browser session for subsequent tests.

    
    import { test as base } from '@playwright/test';
    
    const test = base.extend({
      authenticatedPage: async ({ browser }, use) => {
        const context = await browser.newContext();
        const page = await context.newPage();
        await page.goto('https://shop.example.com/login');
        await page.fill('#username', 'testuser');
        await page.fill('#password', 'password123');
        await page.click('#login');
        await page.waitForSelector('#user-profile'); // Confirm successful login
        await use(page);
        await context.close();
      },
    });
    
    

    Step 2: Define a Shopping Cart Setup Fixture

    This fixture prepares a pre-filled shopping cart environment, eliminating repetitive product selection and cart preparation.

    
    const testWithCart = test.extend({
      cartReadyPage: async ({ authenticatedPage }, use) => {
        await authenticatedPage.goto('https://shop.example.com/products/1');
        await authenticatedPage.click('#add-to-cart');
        await authenticatedPage.goto('https://shop.example.com/cart');
        await use(authenticatedPage);
      }
    });
    
    

    Step 3: Implementing the Checkout Test

    Leverage the prepared fixtures to execute your checkout validation effectively.

    
    testWithCart('Validate Checkout Flow', async ({ cartReadyPage }) => {
      await cartReadyPage.click('#checkout');
      await cartReadyPage.fill('#shipping-address', '123 Main St');
      await cartReadyPage.click('#confirm-order');
      await expect(cartReadyPage.locator('#confirmation-message'))
        .toHaveText('Thank you for your purchase!');
    });
    
    

    Using Playwright Fixtures, the previously cumbersome testing scenario now becomes straightforward and highly efficient:

    • Reduced Redundancy: Setup logic defined clearly once, reused effortlessly.
    • Enhanced Reliability: Consistent setup reduces flaky tests and ensures stability across test runs.
    • Accelerated Execution: Dramatically reduced execution time, beneficial for continuous integration and delivery pipelines.
    • Improved Maintainability: Modular approach simplifies updates and enhances readability.

    By incorporating Playwright Fixtures in scenarios like this, testers and developers alike can achieve more reliable, maintainable, and scalable test suites, significantly boosting the quality and efficiency of software testing practices.

    Built-in Fixtures in Playwright

    Playwright provides several built-in fixtures when using the @playwright/test package. These are automatically available in your test function parameters:

    Fixture – Description

    • page – A single browser tab; most commonly used for UI interaction
    • browser – A browser instance (Chromium, Firefox, or WebKit)
    • context – An isolated browser context for separate sessions
    • request – API RequestContext for making HTTP requests without a browser
    • browserName – A string representing the current browser being tested
    • baseURL – The base URL used in page.goto() or request.get()

    Playwright comes packed with a variety of built-in fixtures that simplify common testing tasks right out of the box. These fixtures help manage browser instances, contexts, pages, and even API requests, allowing testers to write cleaner, more maintainable tests without redundant setup logic. Below are some commonly used built-in fixtures show how they enhance the efficiency and reliability of test scripts.

    BrowserName Fixture

    Detects the current browser being used and adjusts logic accordingly, allowing for cross-browser support and conditional test behavior.

    
    import { test, expect } from '@playwright/test';
    
    test('Test for Built-in browserName fixture', async ({ page, browserName }) => {
      await page.goto('https://www.google.co.in/');
      if (browserName === 'firefox') {
        console.log('Running test in Firefox Browser');
      }
      await expect(page).toHaveTitle('Google');
    });
    
    

    Browser and page Fixtures

    Launches a browser in non-headless mode and opens a new page to verify the title of a website. Useful for visual debugging and testing in full UI mode.

    
    const base = require('@playwright/test');
    
    const test = base.test.extend({
      browser: async ({}, use) => {
        const browser = await base.chromium.launch({ headless: false });
        await use(browser);
        await browser.close();
      },
    });
    
    test('Open Facebook and check title', async ({ browser }) => {
      const page = await browser.newPage();
      await page.goto('https://www.facebook.com/');
      const fbTitle = await page.title();
      console.log(fbTitle);
    });
    
    

    Context Fixture

    Creates a new isolated browser context for each test to avoid shared cookies or storage, which ensures better test isolation and prevents data leakage.

    
    const base = require('@playwright/test');
    
    const test = base.test.extend({
      context: async ({ browser }, use) => {
        const context = await browser.newContext();
        await use(context);
        await context.close();
      },
    });
    
    test('Open Facebook in isolated context', async ({ context }) => {
      const page = await context.newPage();
      await page.goto('https://www.facebook.com/');
      await base.expect(page).toHaveTitle('Facebook - log in or sign up');
      await page.close();
    });
    
    

    Request Fixture

    Makes direct HTTP requests using Playwright’s request context, useful for API testing without launching a browser.

    
    const { test, expect } = require('@playwright/test');
    
    test('Make a GET request to ReqRes API', async ({ request }) => {
      const response = await request.get('https://reqres.in/api/users/2');
      expect(response.ok()).toBeTruthy();
      const body = await response.json();
    
      console.log(body);
      expect(body.data).toHaveProperty('id', 2);
    });
    
    

    Creating Custom Fixtures

    Custom fixtures are created using test.extend(). These are useful when:

    • You need reusable data (e.g., user credentials).
    • You want to inject logic like pre-login.
    • You want test-specific environment setup.

    Custom testUser Fixture

    Injects reusable test data like user credentials into the test. This promotes reusability and clean code.

    
    import { test as base } from '@playwright/test';
    
    const test = base.extend({
      testUser: async ({}, use) => {
        const user = {
          email: '123@gmail.com',
          password: 'securepassword123'
        };
        await use(user);
      }
    });
    
    test('Facebook login test using custom fixture', async ({ page, testUser }) => {
      await page.goto('https://www.facebook.com/');
      await page.fill("input[name='email']", testUser.email);
      await page.fill("input[id='pass']", testUser.password);
      await page.click("button[name='login']");
    });
    
    

    Custom Fixture Naming and Titles

    Assigns a descriptive title to the fixture for better traceability in test reports.

    
    import { test as base } from '@playwright/test';
    
    export const test = base.extend({
      innerFixture: [
        async ({}, use, testInfo) => {
          await use();
        },
        { title: 'my fixture' }
      ]
    });
    
    

    Overriding Fixtures

    Overrides the default behavior of the page fixture to automatically navigate to a base URL before each test.

    
    const test = base.extend({
      page: async ({ baseURL, page }, use) => {
        await page.goto(baseURL);
        await use(page);
      }
    });
    
    test.use({ baseURL: 'https://www.demo.com' });
    
    

    Automatic Fixtures

    Runs shared setup and teardown logic for all tests automatically, such as authentication or data seeding.

    
    const base = require('@playwright/test');
    
    const test = base.test.extend({
      authStateLogger: [
        async ({}, use) => {
          console.log('[Fixture] Logging in...');
          await new Promise(res => setTimeout(res, 1000));
          await use();
          console.log('[Fixture] Logging out...');
        },
        { auto: true }
      ]
    });
    
    

    Fixture Timeouts

    Ensures that long-running fixtures do not cause the test suite to hang by defining maximum allowable time.

    
    const base = require('@playwright/test');
    
    const test = base.test.extend({
      authStateLogger: [
        async ({}, use) => {
          console.log('[Fixture] Logging in...');
          await new Promise(res => setTimeout(res, 3000));
          await use();
          console.log('[Fixture] Logging out...');
        },
        { auto: true, timeout: 5000 }
      ]
    });
    
    
    Related Blogs

    Playwright Mobile Automation for Seamless Web Testing

    Playwright Reports: The Definitive Guide

    Benefits of Using Playwright Fixtures

    Benefit – Description

    • Modularity – Reuse logic across test files and suites
    • Maintainability – Centralized configuration means easier updates
    • Test Isolation – Prevents cross-test interference
    • Scalability – Clean, extensible structure for large suites
    • Performance – Reduces redundant setup

    Conclusion

    Playwright Fixtures are more than just setup helpers they’re the backbone of a scalable, clean, and maintainable test architecture. By modularizing your environment configuration, they reduce flakiness, improve performance, and keep your tests DRY (Don’t Repeat Yourself). Start simple, think modular, and scale with confidence. Mastering fixtures today will pay dividends in your team’s productivity and test reliability.

    Frequently Asked Questions

    • What is the main use of a Playwright Fixture?

      To manage reusable test setup and teardown logic.

    • Can I use multiple fixtures in one test?

      Yes, you can inject multiple fixtures as parameters.

    • How do automatic fixtures help?

      They apply logic globally without explicit inclusion.

    • Are custom fixtures reusable?

      Yes, they can be shared across multiple test files.

    • Do fixtures work in parallel tests?

      Yes, they are isolated per test and support concurrency.

    The post Playwright Fixtures in Action : Create Reusable and Maintainable Tests appeared first on Codoid.

    Source: Read More

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleLastOSLinux: Una Versione Ottimizzata di Linux Mint 22.1 per Utenti Windows
    Next Article Sampling Without Data is Now Scalable: Meta AI Releases Adjoint Sampling for Reward-Driven Generative Modeling

    Related Posts

    Artificial Intelligence

    Introducing Gemma 3

    July 10, 2025
    Artificial Intelligence

    Experiment with Gemini 2.0 Flash native image generation

    July 10, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    Chrome Update Alert: Two High-Severity Flaws (CVE-2025-6191, CVE-2025-6192) Patched

    Security

    CVE-2025-28025 – TOTOLINK Router Buffer Overflow Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    How Neurodiversity Shines at MongoDB

    Databases

    Craft CMS Zero-Day CVE-2025-32432 Exploited with Metasploit Module Now Public

    Security

    Highlights

    CVE-2025-4842 – D-Link DCS-932L Stack-Based Buffer Overflow Vulnerability

    May 17, 2025

    CVE ID : CVE-2025-4842

    Published : May 17, 2025, 11:15 p.m. | 1 hour, 14 minutes ago

    Description : A vulnerability was found in D-Link DCS-932L 2.18.01. It has been declared as critical. This vulnerability affects the function isUCPCameraNameChanged of the file /sbin/ucp. The manipulation of the argument CameraName leads to stack-based buffer overflow. The attack can be initiated remotely. The exploit has been disclosed to the public and may be used. This vulnerability only affects products that are no longer supported by the maintainer.

    Severity: 8.8 | HIGH

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    CVE-2025-5139 – Qualitor Office365 File Command Injection Vulnerability

    May 25, 2025

    CVE-2025-6284 – PHPGurukul Car Rental Portal Cross-Site Request Forgery Vulnerability

    June 19, 2025

    Conoscete la prima distribuzione costruita intorno al kernel Linux?

    April 24, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.