Bird
Raised Fist0
Selenium Pythontesting~20 mins

Parameterized tests in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Parameterized Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of parameterized Selenium test with multiple inputs
Consider this parameterized Selenium test using pytest. What will be the output when running this test suite?
Selenium Python
import pytest
from selenium.webdriver.common.by import By

@pytest.mark.parametrize('search_term', ['apple', 'banana'])
def test_search(browser, search_term):
    browser.get('https://example.com')
    search_box = browser.find_element(By.NAME, 'q')
    search_box.send_keys(search_term)
    search_box.submit()
    assert search_term in browser.title
ABoth tests pass if page title contains 'apple' and 'banana' respectively
BTests run but assertion always fails due to wrong locator
CBoth tests fail because 'browser' fixture is not defined
DOnly the first test runs because parameterization is incorrect
Attempts:
2 left
💡 Hint
Think about what is missing for the test to run properly with Selenium.
❓ assertion
intermediate
2:00remaining
Correct assertion in parameterized test for form validation
You have a parameterized test that submits a form with different inputs. Which assertion correctly verifies that the error message appears when input is invalid?
Selenium Python
import pytest
from selenium.webdriver.common.by import By

@pytest.mark.parametrize('input_value, expect_error', [('', True), ('valid', False)])
def test_form_validation(browser, input_value, expect_error):
    browser.get('https://example.com/form')
    input_field = browser.find_element(By.ID, 'input')
    input_field.clear()
    input_field.send_keys(input_value)
    submit_button = browser.find_element(By.ID, 'submit')
    submit_button.click()
    error_elements = browser.find_elements(By.CLASS_NAME, 'error')
    # Which assertion is correct?
Aassert (len(error_elements) > 0) == expect_error
Bassert error_elements.text == expect_error
Cassert error_elements.is_displayed() == expect_error
Dassert error_elements.exists() == expect_error
Attempts:
2 left
💡 Hint
Remember that find_elements returns a list, not a single element.
❓ locator
advanced
2:00remaining
Best locator strategy for parameterized test with dynamic IDs
In a parameterized test, you need to locate a button whose ID changes dynamically but always starts with 'btn_'. Which locator is best to use?
ABy.CLASS_NAME, 'btn_'
BBy.ID, 'btn_' + dynamic_part
CBy.XPATH, '//button[contains(@id, "btn_")]'
DBy.CSS_SELECTOR, 'button[id^="btn_"]'
Attempts:
2 left
💡 Hint
Look for a locator that matches the start of the ID attribute.
🔧 Debug
advanced
2:00remaining
Debugging flaky parameterized Selenium test
A parameterized Selenium test intermittently fails with 'ElementNotInteractableException' on clicking a button. What is the most likely cause?
Selenium Python
import pytest
from selenium.webdriver.common.by import By

@pytest.mark.parametrize('page', ['home', 'about'])
def test_click_button(browser, page):
    browser.get(f'https://example.com/{page}')
    button = browser.find_element(By.ID, 'start')
    button.click()
    assert 'Started' in browser.page_source
AThe parameterization syntax is invalid causing test failure
BThe button is not visible or ready when click is attempted
CThe locator ID 'start' is incorrect on some pages
DThe assertion is wrong and causes false failures
Attempts:
2 left
💡 Hint
Consider timing and element readiness in Selenium tests.
❓ framework
expert
3:00remaining
Implementing parameterized tests with pytest and Selenium for multiple browsers
You want to run the same parameterized test on Chrome and Firefox browsers using pytest and Selenium. Which pytest fixture setup correctly supports this?
AUse @pytest.mark.parametrize('browser_name', ['chrome', 'firefox']) and a fixture that initializes WebDriver based on browser_name
BCreate separate test functions for each browser without parameterization
CUse a global variable to switch browsers inside the test
DUse Selenium Grid without any pytest parameterization
Attempts:
2 left
💡 Hint
Think about combining pytest parameterization with fixtures for browser setup.

Practice

(1/5)
1. What is the main benefit of using @pytest.mark.parametrize in Selenium Python tests?
easy
A. It allows running the same test with multiple sets of data easily.
B. It speeds up the browser loading time.
C. It automatically finds locators on the page.
D. It generates test reports without extra code.

Solution

  1. Step 1: Understand the purpose of parameterized tests

    Parameterized tests run the same test logic multiple times with different input data sets.
  2. Step 2: Identify what @pytest.mark.parametrize does

    This decorator allows easy setup of multiple data inputs for one test function, reducing repeated code.
  3. Final Answer:

    It allows running the same test with multiple sets of data easily. -> Option A
  4. Quick Check:

    Parameterized tests = multiple data sets [OK]
Hint: Think: same test, many inputs, less code [OK]
Common Mistakes:
  • Confusing parameterization with browser speed optimization
  • Thinking it auto-finds locators
  • Assuming it auto-generates reports
2. Which of the following is the correct syntax to parameterize a test function with two parameters username and password using pytest?
easy
A. @pytest.mark.param('username', 'password', [('user1', 'pass1'), ('user2', 'pass2')])
B. @pytest.parametrize(['username', 'password'], [('user1', 'pass1'), ('user2', 'pass2')])
C. @pytest.mark.parametrize(('username', 'password'), ['user1', 'pass1', 'user2', 'pass2'])
D. @pytest.mark.parametrize('username, password', [('user1', 'pass1'), ('user2', 'pass2')])

Solution

  1. Step 1: Recall pytest parameterize syntax

    The correct syntax uses @pytest.mark.parametrize with a string of comma-separated parameter names and a list of tuples for values.
  2. Step 2: Compare options

    @pytest.mark.parametrize('username, password', [('user1', 'pass1'), ('user2', 'pass2')]) matches the correct syntax exactly. Others have wrong decorator names or wrong argument formats.
  3. Final Answer:

    @pytest.mark.parametrize('username, password', [('user1', 'pass1'), ('user2', 'pass2')]) -> Option D
  4. Quick Check:

    Correct decorator and tuple list = @pytest.mark.parametrize('username, password', [('user1', 'pass1'), ('user2', 'pass2')]) [OK]
Hint: Remember: mark.parametrize with string params and list of tuples [OK]
Common Mistakes:
  • Misspelling 'parametrize' as 'parametrize' or 'param'
  • Using list instead of tuple for parameter values
  • Passing parameters as list instead of string
3. What will be the output when running this parameterized test snippet?
import pytest

@pytest.mark.parametrize('num, expected', [(1, 2), (2, 3), (3, 4)])
def test_increment(num, expected):
    assert num + 1 == expected
medium
A. Syntax error due to wrong decorator usage.
B. All tests fail due to assertion errors.
C. All tests pass successfully.
D. Only the first test passes, others fail.

Solution

  1. Step 1: Analyze the test logic and parameters

    Each tuple provides num and expected. The test asserts num + 1 == expected. For (1,2), 1+1=2; for (2,3), 2+1=3; for (3,4), 3+1=4.
  2. Step 2: Check if assertions hold for all cases

    All assertions are true, so all tests pass.
  3. Final Answer:

    All tests pass successfully. -> Option C
  4. Quick Check:

    num + 1 equals expected for all [OK]
Hint: Check each input-output pair matches assertion [OK]
Common Mistakes:
  • Assuming some tests fail without checking values
  • Confusing parameter order
  • Thinking decorator syntax is wrong
4. Identify the error in this parameterized test code:
import pytest

@pytest.mark.parametrize('input, expected')
def test_double(input, expected):
    assert input * 2 == expected

@pytest.mark.parametrize('input, expected', [(2, 4), (3, 6)])
medium
A. Wrong assertion operator used.
B. Missing data list for parameterize decorator on test_double.
C. Parameters names should be a list, not a string.
D. Test function name is invalid.

Solution

  1. Step 1: Check the decorator usage on test_double

    The decorator @pytest.mark.parametrize('input, expected') is missing the second argument: the list of data tuples.
  2. Step 2: Verify other parts

    Assertion and function name are correct. Parameter names as string is valid syntax.
  3. Final Answer:

    Missing data list for parameterize decorator on test_double. -> Option B
  4. Quick Check:

    Decorator needs data list argument [OK]
Hint: Always provide data list with parametrize decorator [OK]
Common Mistakes:
  • Forgetting the data list argument
  • Using list instead of string for parameter names (which is allowed but less common)
  • Misreading assertion syntax
5. You want to test a login form with multiple username and password pairs using Selenium and pytest parameterization. Which approach correctly combines parameterized tests with Selenium WebDriver to test login functionality?
hard
A. Use @pytest.mark.parametrize to pass username and password, then inside the test open the browser, fill fields, submit, and assert login success.
B. Write separate test functions for each username and password pair without parameterization.
C. Use parameterization only for URLs, not for form inputs like username and password.
D. Use @pytest.mark.parametrize but do not open the browser inside the test function.

Solution

  1. Step 1: Understand parameterized test purpose in Selenium context

    Parameterized tests allow running the same test logic with different input data, such as username and password pairs.
  2. Step 2: Combine parameterization with Selenium actions

    Inside the test function, open the browser, fill the login form with parameters, submit, and check results. This is the correct approach.
  3. Final Answer:

    Use @pytest.mark.parametrize to pass username and password, then inside the test open the browser, fill fields, submit, and assert login success. -> Option A
  4. Quick Check:

    Parameterize inputs + Selenium actions inside test [OK]
Hint: Parameterize inputs, then automate browser steps inside test [OK]
Common Mistakes:
  • Writing separate tests instead of parameterizing
  • Not opening browser inside test function
  • Limiting parameterization to URLs only
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard