DEV Community

Styrow.dev
Styrow.dev

Posted on Originally published at styrow.dev

How do you effectively test an application's reliance on browser history (back/forward buttons) and history API state ma

๐Ÿคฏ SPA Navigation Nightmares? 99% of Devs Miss This Playwright Trick!
How do you truly test your Single Page Application's reliance on browser history (back/forward) and History API state without full page reloads?

๐Ÿ“Œ Problem Statement
SPAs use History API (pushState) for client-side navigation. Back/forward buttons don't always trigger full reloads. Verifying only the URL is insufficient. You must confirm the application's internal state and rendered UI. Traditional tests often fail here.

๐Ÿ’ก Solution & Code Walkthrough
Playwright's page.goBack() and page.goForward() mimic browser button clicks. These async methods await navigation completion, allowing assertions on both URL and dynamic UI. For deeper validation, page.evaluate(() => history.state) can inspect raw History API state.

import { test, expect } from '@playwright/test';

test('should handle browser history navigation in an SPA', async ({ page }) => {
  // 1. Navigate to initial product list
  await page.goto('http://localhost:3000/products');
  await expect(page).toHaveURL(/products$/);
  await expect(page.locator('h1')).toHaveText('Product List');

  // 2. Navigate to a product detail page
  await page.locator('a:has-text("Product A")').click();
  await expect(page).toHaveURL(/products\/product-a$/);
  await expect(page.locator('h1')).toHaveText('Product A Detail');

  // 3. Simulate browser back button
  await page.goBack();
  await expect(page).toHaveURL(/products$/);
  await expect(page.locator('h1')).toHaveText('Product List');

  // 4. Simulate browser forward button
  await page.goForward();
  await expect(page).toHaveURL(/products\/product-a$/);
  await expect(page.locator('h1')).toHaveText('Product A Detail');
  // Consider checking history.state for advanced SPA logic
  // const state = await page.evaluate(() => history.state);
  // expect(state).toHaveProperty('productViewed', 'product-a');
});
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”‘ Key Takeaways
โ€ข Use page.goBack() & page.goForward() for authentic browser navigation.
โ€ข Always verify both URL (toHaveURL) and rendered UI content (toHaveText).
โ€ข Consider page.evaluate(() => history.state) for deep client-side state validation.
โŒ Relying solely on URL changes for SPA history tests is a common pitfall.

โ“ Quick Summary Q&A
Q: How to simulate back/forward in Playwright?
A: Use page.goBack() and page.goForward().
Q: What to verify in SPA history navigation?
A: URL, visible UI content, and potentially the history.state object.

TAGS: playwright, testing, spa, automation, javascript, webdev

โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
๐Ÿ‘‰ Master Playwright & Automation with our App!
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

๐Ÿ“ฒ ๐…๐‘๐„๐„ ๐Œ๐Ž๐๐ˆ๐‹๐„ ๐€๐๐ โ€” ๐Ÿ”๐ŸŽ๐ŸŽ+ ๐’๐ƒ๐„๐“ ๐&๐€๐ฌ
Practice real-world interview scenarios offline on the free QA Automation & SDET Prep app:

๐Ÿค– ๐†๐จ๐จ๐ ๐ฅ๐ž ๐๐ฅ๐š๐ฒ (๐€๐ง๐๐ซ๐จ๐ข๐):
https://play.google.com/store/apps/details?id=com.app.seleniuminterviewquestions&referrer=utm_source%3Ddevto%26utm_medium%3Darticle%26utm_campaign%3Dselenium_20261113

๐ŸŽ ๐€๐ฉ๐ฉ ๐’๐ญ๐จ๐ซ๐ž (๐ข๐Ž๐’):
https://apps.apple.com/app/id6786760948?pt=128640464&ct=devto_selenium_20261113&mt=8

โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

Top comments (0)