DEV Community

Cover image for Testing in Incognito Mode with Playwright
Aswani Kumar
Aswani Kumar

Posted on

Testing in Incognito Mode with Playwright

Table of Contents

Introduction

Incognito mode, or private browsing, is a feature in modern browsers that allows users to browse without saving their history, cookies, or other session data. Testing in incognito mode is crucial for ensuring that your application behaves as expected for users who prefer private browsing. Playwright makes it easy to simulate and automate tests in incognito mode. In this post, we’ll explore how to effectively use Playwright for testing in incognito mode.

1. Why Test in Incognito Mode?

Testing in incognito mode is essential for several reasons:

  • No Stored Cookies or Cache: Ensure that your application works without relying on cached data or existing sessions.
  • Privacy-Centric Users: Validate functionality for users who prefer private browsing.
  • Session Isolation: Test scenarios where multiple sessions need to run independently. ## 2. Setting Up Incognito Mode with Playwright

Playwright provides a straightforward way to create browser contexts that simulate incognito mode.

a. Creating an Incognito Browser Context

An incognito context is created using browser.newContext() without sharing cookies, cache, or local storage with other contexts.

Example:

const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch();
  const context = await browser.newContext(); // Incognito mode
  const page = await context.newPage();

  await page.goto('https://example.com');
  console.log(await page.title());

  await browser.close();
})();
Enter fullscreen mode Exit fullscreen mode

3. Common Test Scenarios in Incognito Mode


a. Login Functionality

Test login workflows to ensure authentication works without stored cookies or session data.

await page.goto('https://example.com/login');
await page.fill('#username', 'testuser');
await page.fill('#password', 'password123');
await page.click('#login-button');
await expect(page).toHaveURL('https://example.com/dashboard');
Enter fullscreen mode Exit fullscreen mode


b. Cookie Consent Banners

Verify that cookie consent banners appear as expected when no prior preferences are stored.

await expect(page.locator('#cookie-banner')).toBeVisible();
Enter fullscreen mode Exit fullscreen mode


c. Cart and Checkout Flows

Simulate shopping experiences to validate cart persistence and checkout processes without pre-existing sessions.

4. Debugging Tests in Incognito Mode


a. Enable Debugging

Run Playwright tests in headed mode to visualize interactions:

npx playwright test --headed
Enter fullscreen mode Exit fullscreen mode


b. Use Playwright Inspector

Activate the Playwright Inspector for step-by-step debugging:

npx playwright test --debug
Enter fullscreen mode Exit fullscreen mode


c. Capture Traces

Generate trace files to diagnose issues:

await page.tracing.start({ screenshots: true, snapshots: true });
await page.goto('https://example.com');
await page.tracing.stop({ path: 'trace.zip' });
Enter fullscreen mode Exit fullscreen mode

5. Best Practices for Testing in Incognito Mode

  1. Isolate Tests: Use separate incognito contexts for each test to avoid state sharing.
  2. Verify Privacy Features: Ensure no data persists between sessions.
  3. Test Edge Cases: Simulate scenarios like blocked cookies or disabled JavaScript.
  4. Combine with Network Mocking: Use Playwright’s page.route to mock backend responses for private sessions. ## 6. Automating Incognito Mode in CI/CD Pipelines

To include incognito mode testing in your CI/CD pipelines:

  • Configure your Playwright tests to launch incognito contexts.
  • Integrate Playwright’s GitHub Action or other CI tools.
  • Generate HTML reports to analyze test results:
export default defineConfig({
  reporter: [['html', { outputFolder: 'playwright-report' }]],
});
Enter fullscreen mode Exit fullscreen mode

7. Sample Playwright Config for Incognito Mode

Here’s a sample configuration for running all tests in incognito mode:

import { defineConfig } from '@playwright/test';

export default defineConfig({
  use: {
    contextOptions: {
      ignoreHTTPSErrors: true,
      permissions: [],
    },
  },
});
Enter fullscreen mode Exit fullscreen mode

Conclusion

Testing in incognito mode is a critical aspect of ensuring privacy and reliability for all users. Playwright’s support for creating isolated browser contexts makes it an excellent tool for automating these tests. By adopting the practices outlined in this post, you can confidently validate your application’s functionality in private browsing scenarios.

Have you tried testing in incognito mode with Playwright? Share your experiences and tips in the comments below!

Top comments (2)

Collapse
 
emiroberti profile image
Emi Roberti

great article

Collapse
 
aswani25 profile image
Aswani Kumar

Thanks