Bird
Raised Fist0
Selenium Pythontesting~20 mins

pytest with Selenium setup in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Selenium pytest Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of pytest fixture with Selenium WebDriver
What will be the output when running this pytest test with the given fixture?
Selenium Python
import pytest
from selenium import webdriver

@pytest.fixture
async def driver():
    driver = webdriver.Firefox()
    yield driver
    driver.quit()

def test_title(driver):
    driver.get('https://example.com')
    assert 'Example Domain' in driver.title
    print('Test Passed')
ATest Passed printed and test passes
BSyntaxError due to async fixture
CTest Passed printed but test fails with AssertionError
DRuntimeError because driver.quit() is not called
Attempts:
2 left
💡 Hint
pytest fixtures should not be async unless using pytest-asyncio plugin
❓ assertion
intermediate
1:30remaining
Correct assertion for page title in Selenium test
Which assertion correctly verifies the page title contains 'Welcome' in a Selenium test using pytest?
Selenium Python
def test_page_title(driver):
    driver.get('https://mysite.com')
    # Choose the correct assertion below
Aassert driver.title == 'Welcome to MySite'
Bassert driver.title.index('Welcome') >= 0
Cassert driver.title.contains('Welcome')
Dassert 'Welcome' in driver.title
Attempts:
2 left
💡 Hint
Use Python's 'in' keyword to check substring presence
❓ locator
advanced
2:00remaining
Best locator strategy for a button with dynamic ID
You want to locate a button with a dynamic ID that changes every page load but has a stable class 'submit-btn' and visible text 'Submit'. Which locator is best for Selenium?
Adriver.find_element('id', 'btn12345')
Bdriver.find_element('css selector', 'button.submit-btn')
Cdriver.find_element('xpath', "//button[contains(text(),'Submit') and contains(@class,'submit-btn')]")
Ddriver.find_element('name', 'submit')
Attempts:
2 left
💡 Hint
Use XPath to combine text and class attributes for stable selection
🔧 Debug
advanced
2:30remaining
Identify the cause of flaky Selenium test failure
This pytest Selenium test intermittently fails with NoSuchElementException. What is the most likely cause?
Selenium Python
def test_login(driver):
    driver.get('https://mysite.com/login')
    driver.find_element('id', 'username').send_keys('user')
    driver.find_element('id', 'password').send_keys('pass')
    driver.find_element('id', 'submit').click()
    assert 'Dashboard' in driver.title
AThe test runs too fast before page elements load; needs explicit wait
BThe element IDs are incorrect and do not exist on the page
CThe assertion is wrong; 'Dashboard' is not in the title
Ddriver.quit() is missing causing stale element errors
Attempts:
2 left
💡 Hint
Web pages may take time to load elements; Selenium needs waits
❓ framework
expert
2:00remaining
pytest Selenium parallel test execution setup
Which pytest command correctly runs Selenium tests in parallel across 4 browser instances using pytest-xdist?
Apytest -n 4 --browser=firefox
Bpytest --parallel=4 --browser=firefox
Cpytest -n 4 --driver=firefox
Dpytest --workers=4 --driver=firefox
Attempts:
2 left
💡 Hint
pytest-xdist uses -n option to specify number of parallel workers

Practice

(1/5)
1. What is the main purpose of using pytest fixtures in Selenium tests?
easy
A. To set up and tear down the browser before and after tests
B. To write assertions inside the test functions
C. To locate elements on the web page
D. To generate test reports automatically

Solution

  1. Step 1: Understand pytest fixtures role

    Fixtures in pytest are used to prepare the test environment and clean up after tests, such as opening and closing browsers.
  2. Step 2: Match fixture purpose with Selenium tests

    In Selenium tests, fixtures typically launch the browser before a test and close it after, ensuring clean test runs.
  3. Final Answer:

    To set up and tear down the browser before and after tests -> Option A
  4. Quick Check:

    Fixtures manage setup/cleanup = A [OK]
Hint: Fixtures handle setup and cleanup in tests [OK]
Common Mistakes:
  • Confusing fixtures with assertions
  • Thinking fixtures locate elements
  • Assuming fixtures generate reports
2. Which of the following is the correct way to define a pytest fixture that opens a Chrome browser using Selenium?
easy
A. def browser(): driver = webdriver.Chrome() yield driver driver.quit()
B. @pytest.fixture def browser(): driver = webdriver.Chrome() yield driver driver.quit()
C. @pytest.test def browser(): driver = webdriver.Chrome() yield driver driver.quit()
D. @fixture def browser(): driver = webdriver.Chrome() yield driver driver.quit()

Solution

  1. Step 1: Identify correct pytest fixture decorator

    The correct decorator to define a fixture is @pytest.fixture.
  2. Step 2: Check function structure for setup and teardown

    The function creates a driver, yields it for test use, then quits the driver after the test.
  3. Final Answer:

    @pytest.fixture\ndef browser():\n driver = webdriver.Chrome()\n yield driver\n driver.quit() -> Option B
  4. Quick Check:

    Use @pytest.fixture for fixtures = A [OK]
Hint: Always use @pytest.fixture decorator for fixtures [OK]
Common Mistakes:
  • Missing @pytest.fixture decorator
  • Using wrong decorator like @pytest.test
  • Forgetting to quit the driver
3. Given the following pytest test code, what will be the output when running the test?
import pytest
from selenium import webdriver

@pytest.fixture
def browser():
    driver = webdriver.Chrome()
    yield driver
    driver.quit()


def test_google_title(browser):
    browser.get('https://www.google.com')
    assert 'Google' in browser.title
medium
A. Test passes if the browser opens Google and title contains 'Google'
B. Test fails because browser.title is empty
C. SyntaxError due to missing import
D. RuntimeError because driver.quit() is called too early

Solution

  1. Step 1: Analyze fixture and test function

    The fixture opens Chrome, yields the driver, and quits after test. The test navigates to Google and asserts title contains 'Google'.
  2. Step 2: Understand expected behavior

    Since Google page title contains 'Google', the assertion passes and test completes successfully.
  3. Final Answer:

    Test passes if the browser opens Google and title contains 'Google' -> Option A
  4. Quick Check:

    Title contains 'Google' means pass = B [OK]
Hint: Check page title contains expected text for pass [OK]
Common Mistakes:
  • Assuming title is empty before page load
  • Thinking driver.quit() runs before test
  • Missing import statements
4. Identify the error in this pytest Selenium setup code:
import pytest
from selenium import webdriver

@pytest.fixture
def browser():
    driver = webdriver.Chrome()
    return driver
    driver.quit()


def test_example(browser):
    browser.get('https://example.com')
    assert 'Example' in browser.title
medium
A. Assertion syntax is incorrect
B. Missing @pytest.test decorator on test_example
C. driver.quit() is never called because it is after return
D. webdriver.Chrome() is not imported correctly

Solution

  1. Step 1: Check fixture function flow

    The fixture returns driver before calling driver.quit(), so quit() never runs.
  2. Step 2: Understand impact on test

    Without quitting, browser stays open, causing resource leaks and flaky tests.
  3. Final Answer:

    driver.quit() is never called because it is after return -> Option C
  4. Quick Check:

    Code after return is ignored = D [OK]
Hint: Put cleanup code after yield, not after return [OK]
Common Mistakes:
  • Placing driver.quit() after return
  • Confusing @pytest.test with @pytest.fixture
  • Ignoring proper assertion syntax
5. You want to run a Selenium test with pytest that opens Firefox, navigates to a page, and checks a button is clickable. Which pytest fixture setup is best practice to ensure browser opens and closes properly?
hard
A. @pytest.fixture def browser(): driver = webdriver.Firefox() driver.quit() yield driver
B. def browser(): driver = webdriver.Firefox() return driver driver.quit()
C. @pytest.fixture def browser(): driver = webdriver.Firefox() yield driver
D. @pytest.fixture def browser(): driver = webdriver.Firefox() yield driver driver.quit()

Solution

  1. Step 1: Review proper fixture structure for setup and teardown

    Best practice is to create driver, yield it for test use, then quit after test finishes.
  2. Step 2: Evaluate each option

    @pytest.fixture def browser(): driver = webdriver.Firefox() yield driver driver.quit() correctly uses yield and calls quit after test. def browser(): driver = webdriver.Firefox() return driver driver.quit() quits after return (never runs). @pytest.fixture def browser(): driver = webdriver.Firefox() driver.quit() yield driver quits before yield (browser closed too early). @pytest.fixture def browser(): driver = webdriver.Firefox() yield driver never quits browser.
  3. Final Answer:

    @pytest.fixture\ndef browser():\n driver = webdriver.Firefox()\n yield driver\n driver.quit() -> Option D
  4. Quick Check:

    Yield before quit ensures proper cleanup = C [OK]
Hint: Yield driver, then quit after test for clean browser [OK]
Common Mistakes:
  • Calling quit before yield
  • Not quitting browser at all
  • Using return instead of yield in fixture
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