Stop 06 · Programming Fundamentals for AutomationTopics · 2/10

Variables, Loops & Conditionals

Part of the QA Testing Roadmap

In short

Almost every test script is three things on repeat: remember a value, go through a list, decide pass or fail. Learn these three and you can read most automation code.

The building blocks of any script — storing values, repeating actions, and branching logic — used constantly in test automation to iterate over test data and handle different scenarios.

The purple blob sorts mangoes into a ripe basket and a green basket while holding a tally counter.

Think of it like this

Sorting mangoes at the fruit stall

The fruitwala picks up each mango (loop), checks if it's ripe (conditional) and drops it in the right basket. He keeps a count of ripe ones on his tally clicker (variable). Your script does the same with test data.

Watch it happen

Looping over ages for a sign-up form

17
0
18
1
45
2
60
3
-1
4

1/6The rule: age must be 18 to 60. We loop over five test ages, one at a time.

The big ideas

What you need to know

Variables hold values

A variable is a named box: expected_title = 'Cart'. Change the box, not every line that uses it.

Loops repeat

A for loop runs the same steps for each item. Five test inputs, one block of code.

Conditionals decide

if, elif and else pick a path. That's how a script says pass, fail or skip.

== is not =

= stores a value. == compares two values. Mixing them up is the classic beginner bug.

See it in code

Walk through the code

python
1ages = [17, 18, 45, 60, -1]2accepted = 03 4for age in ages:5    if 18 <= age <= 60:6        print(age, "-> expect: accepted")7        accepted += 18    elif age < 0:9        print(age, "-> expect: invalid input error")10    else:11        print(age, "-> expect: age not allowed")12 13print("Total expected accepted:", accepted)

1/4A list of test ages and a counter that starts at zero.

One loop, one decision, one counter. This is the skeleton of data-driven testing.

Save yourself the pain

Common mistakes

Oops

Copy-pasting the same test block five times with different values.

Do this instead

Put the values in a list and loop. One fix then covers all five.

Oops

Writing if age > 18 when the rule says 18 is allowed.

Do this instead

Read the rule carefully. 'From 18' means >= 18. Boundaries are where bugs hide.

Your mission

Loop through test emails

~25 min

Quick check

Which line correctly checks that a page title equals 'Cart'?

Go deeper

  1. 1Write a simple script using a loop to iterate over a list of test inputs.
  2. 2Practice conditional logic to handle different expected outcomes based on input.
  3. 3Build fluency until these constructs feel automatic, not something you have to look up each time.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge