Blog #27: ๐๐ป A Tale of Dynamic Pages & Adaptive Code - Helping a Friend Debug a Playwright Test
The other day, a friend messaged me, a bit stuck on a Playwright test: “I’m trying to add all 3 products to the cart, but only 2 are getting added, and then it crashes! ๐คฏ” I couldn’t resist helping out and it turned into a fun little debugging adventure. Let me share how we cracked it! ๐ The Scenario It was a practice website with exactly 3 products. My friend’s mission: ๐ Log in ๐ Add all 3 items to the cart ๐ Validate that everything worked smoothly. Sounds simple, right? But the page had a dynamic twist! ๐ The Initial Problem Here’s what he was using at first: const addToCartButtons = page . locator ( 'button:has-text("Add to Cart")' ); for ( let i = 0 ; i < 3 ; i ++ ) { await addToCartButtons . nth ( i ). click (); } But… it would only add 2 items and then fail with a timeout error! Turns out, Playwright was trying to click a button that no longer existed after the page updated. ๐ What Was Happening? When yo...