Blog # 11: Conquer Web Tables & Pagination with Playwright: A Step-by-Step Quest ๐Ÿง™โ€โ™‚๏ธ

Published on April 1, 2025

Web automation becomes magical when you tackle challenges step-by-step. Ready for a clearly structured adventure with focused levels? Let's go! ๐Ÿš€  

๐Ÿ—บ๏ธ Quest Map  

1. Level 1: Validate Table Structure  
2. Level 2: Checkbox Wizardry  
3. Level 3: Single-Page Data Heist  
4. Level 4: Pagination Labyrinth  
5. Final Boss: Cross-Realm Data Analysis  

๐Ÿ› ๏ธ Setup: Prepare Your Weapons  

const { test, expect } = require('@playwright/test');

test('Webtable & Pagination Mastery', async ({ page }) => {
  await page.goto('https://testautomationpractice.blogspot.com/');
  const webtable = page.locator('#productTable');
  // ... Your code conquers here!
});

๐Ÿฐ Level 1: Validate the Table Fortress  

"First, know thy enemy's defenses!"  

Challenge: Verify columns and rows  

// Column Guardian
const columns = webtable.locator('thead tr th');
await expect(columns).toHaveCount(4); 
console.log(`๐Ÿ›ก๏ธ ${await columns.count()} columns standing guard!`);

// Row Soldiers
const firstPageRows = webtable.locator('tbody tr');
await expect(firstPageRows).toHaveCount(5);
console.log(`โš”๏ธ ${await firstPageRows.count()} rows on first page!`);

Pro Tip: Fail here? The table might be shape-shifting! ๐Ÿง™โ€โ™‚๏ธ  

๐Ÿ”ฎ Level 2: Checkbox Enigma  

"Activate the magical 'Wireless Earbuds' checkbox!"  

const targetRow = firstPageRows.filter({
  has: page.locator('td'),
  hasText: 'Wireless Earbuds' // Exact spell required!
});

await targetRow.locator('[type="checkbox"]').check();
console.log('๐Ÿ”‘ Checkbox activated! Portal to Level 3 opens...');
Danger: One typo = 100 angry checkbox goblins! ๐ŸงŒ  

๐Ÿ•ต๏ธ Level 3: First-Page Data Heist  

"Steal treasure from the CURRENT realm only!"  

let productCount = 1;

for (let i = 0; i < await firstPageRows.count(); i++) {
  const row = firstPageRows.nth(i);
  const cells = row.locator('td');
  
  const productName = await cells.nth(1).textContent();
  const price = await cells.nth(2).textContent();
  
  console.log(`๐Ÿ’ฐ #${productCount}: ${productName} (${price})`);
  productCount++;
}

๐Ÿงญ Level 4: Pagination Labyrinth  

"Now conquer ALL data realms!"  

  const pagination = page.locator('.pagination a');
  let productCount = 1;
  
  for (let p = 0; p < await pagination.count(); p++) {
    if (p > 0) {
      await pagination.nth(p).click();
      await page.waitForTimeout(2000); // Simple delay
    }

    const rows = page.locator('table#productTable tr');
    for (let i = 0; i < await rows.count(); i++) {
      const row = rows.nth(i);
      const name = await row.locator('td:nth-child(2)').textContent();
      const price = await row.locator('td:nth-child(3)').textContent();
      
      console.log(`${productCount}. Product: ${productName} | Price: ${price}`);
      productCount++; // Increment the counter
} }

๐Ÿ‰ Final Boss: Cross-Realm Data Dragon  

๐Ÿ† Boss Battle: Golden Product Gauntlet
 
"Prove your skills in 3...2...1... GO!"

Your Mission:

๐Ÿ”ฅ Find the MOST EXPENSIVE item across ALL pages

๐Ÿ”ฅ Log it like: ๐Ÿ† Ultimate Treasure: PRODUCT ($PRICE)

// STARTER CODE
let maxPrice = 0;
let goldenProduct = '';

/* Your magic here! */

console.log(`๐Ÿ† Ultimate Treasure: ${goldenProduct} ($${maxPrice})`);
Victory Reward: Eternal glory in the Console Hall of Fame! ๐Ÿ›๏ธ  

Got stuck? Found a cooler way? Comment below! Letโ€™s build the ultimate Playwright community. ๐Ÿ’ฌ

Happy Coding! ๐Ÿ‘ฉ๐Ÿ’ป๐Ÿ‘จ๐Ÿ’ป