Lesson 16 — Loops & Arrays: One Test, Many Rows · Practical QA
LESSON VIEW
F-03 · BASIC · PRACTICAL QA

Lesson 16 — Loops & Arrays: One Test, Many Rows

Objective: Arrays and loops — one test, many data rows. The pricing table you will automate in LAB-3 is an array; the loop that runs it is ten lines. Learn to read and write both here, on FreshNest prices.

⏱ ≈6 min full lesson · Converted from the F-03 module deck · English edition — Español · العربية · বাংলা in translation

⚡ Key Points

  • Arrays are tables — An array is an ordered list in one box: [110, 140, 170].
  • The price table, as data — RUN IT: this is LAB-3's pricing data, weeks early.
  • One loop to write, three to read — for...of walks a list value by value — the loop you will write.
  • Looping what the page shows — RUN IT: locators can match many elements — .count() tells you how many, and a loop lets you check each.
  • The answer — Six tests: three Standard rows + three Deep rows, one test per row.
  • Spot the bug — SPOT THE BUG: this loop should print all three Standard prices.

📖 Read

In ~6 minutes: arrays and loops — one test, many data rows. The pricing table you will automate in LAB-3 is an array; the loop that runs it is ten lines. Learn to read and write both here, on FreshNest prices.

Arrays are tables: An array is an ordered list in one box: [110, 140, 170]. An array of OBJECTS is a table: each row { beds: 1, price: 110 } carries named facts. Test data wants to be a table — when a row fails, the failure message can say which bedroom count and which expected price, not 'row 2'. Index counting starts at 0: rows[0] is the FIRST row.

The price table, as data: RUN IT: this is LAB-3's pricing data, weeks early. Add a row for 4 bedrooms (Standard = 140 + 2×30 = 200) and re-run — the loop prints it with no other change. That is the entire data-driven idea: new case = new row, never new code. Code: const standardRows = [ | { beds: 1, price: 110 }, | { beds: 2, price: 140 }, | { beds: 3, price: 170 }, | ]; | for (const row of standardRows) { | console.log(row.beds + ' bed -> $' + row.price); | }

One loop to write, three to read: for...of walks a list value by value — the loop you will write. Two relatives you only need to READ for now: rows.forEach(row => ...) does the same walk in callback style (older suites use it), and rows.map(row => row.price) builds a NEW list from an old one ([110, 140, 170] from the table). while repeats until a condition fails — you will meet it in polling/waiting code; reading level is enough.

Looping what the page shows: RUN IT: locators can match many elements — .count() tells you how many, and a loop lets you check each. This loop reads every option of the bedrooms dropdown on the real form. Then change the fence to i < total - 1 and re-run: the last option silently vanishes — no error, just missing coverage. Loops plus assertions are how 'check every row' tests are built, and the fence is where they break. Code: // In a test, against the FreshNest booking form: | const options = page.locator('#beds option'); | const total = await options.count(); | console.log('bedroom options found:', total); | for (let i = 0; i < total; i++) { | console.log('option', i, await options.nth(i).textContent()); | }

The answer: Six tests: three Standard rows + three Deep rows, one test per row. In LAB-3 this generation is literal — Playwright declares a test inside the loop, titled by the row, so a failure reads 'deep · 3 bed · expects $255' instead of 'test 7 failed'. The loop is the test factory.

Spot the bug: SPOT THE BUG: this loop should print all three Standard prices. It prints only two. The loop bounds are the suspect.

The answer: The bug is the − 1: i < length is already the correct fence. Off-by-one errors in test loops don't crash — they quietly delete coverage. You have finished this module when: you can turn a requirements table into an array of rows, walk it with for...of, and state exactly how many tests a given table generates.

Complete the loop: COMPLETE the loop body so each Deep row is checked against the page's estimate text. The page object's assertion method is expectEstimate(text).

The answer: It runs the data as given — loops are obedient, not wise. Data quality is the tester's job, which is why every row in this course traces to a requirements line. Done when: your price tables (Standard + Deep) generate the right test count, your loop asserts rather than prints, and you can spot the off-by-one before it deletes a case.

Build the Standard and Deep price tables as arrays, generate one check per row with for...of, and state the test count before running.