0
0
Selenium Pythontesting~20 mins

Test functions and classes in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Selenium Test Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple Selenium test function
What will be the test result when running this Selenium test function using pytest?
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By

def test_google_title():
    driver = webdriver.Chrome()
    driver.get('https://www.google.com')
    title = driver.title
    driver.quit()
    assert 'Google' in title
ATest fails because the URL is incorrect
BTest fails because driver.quit() is called before getting the title
CTest raises a NoSuchElementException
DTest passes because the page title contains 'Google'
Attempts:
2 left
💡 Hint
Think about what driver.title returns after loading the page.
assertion
intermediate
1:30remaining
Correct assertion for checking element text
Which assertion correctly verifies that a web element's text is exactly 'Submit' in a Selenium test?
Selenium Python
element = driver.find_element(By.ID, 'submit-btn')
text = element.text
Aassert text is 'Submit'
Bassert 'Submit' in text
Cassert text == 'Submit'
Dassert text != 'Submit'
Attempts:
2 left
💡 Hint
Use the equality operator to check exact match.
🔧 Debug
advanced
2:30remaining
Identify the error in this Selenium test class
What error will occur when running this Selenium test class with pytest?
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By

class TestLogin:
    def setup_method(self):
        self.driver = webdriver.Chrome()

    def teardown_method(self):
        self.driver.quit()

    def test_login(self):
        self.driver.get('https://example.com/login')
        username = self.driver.find_element(By.ID, 'username')
        password = self.driver.find_element(By.ID, 'password')
        username.send_keys('user')
        password.send_keys('pass')
        self.driver.find_element(By.ID, 'submit').click()
        assert 'Dashboard' in self.driver.title
ANoSuchElementException if elements are not found
BAttributeError because setup_method is not named correctly
CNo error, test runs and passes if page loads correctly
DTypeError due to missing self parameter in test_login
Attempts:
2 left
💡 Hint
Consider what happens if the page elements are missing or IDs are incorrect.
framework
advanced
2:00remaining
Best practice for Selenium test class setup and teardown
Which code snippet correctly implements setup and teardown for a Selenium test class using pytest conventions?
A
class TestExample:
    def setUp(self):
        self.driver = webdriver.Chrome()
    def tearDown(self):
        self.driver.quit()
B
class TestExample:
    def setup_method(self):
        self.driver = webdriver.Chrome()
    def teardown_method(self):
        self.driver.quit()
C
class TestExample:
    def setup(self):
        self.driver = webdriver.Chrome()
    def teardown(self):
        self.driver.quit()
D
class TestExample:
    def before_all(self):
        self.driver = webdriver.Chrome()
    def after_all(self):
        self.driver.quit()
Attempts:
2 left
💡 Hint
Pytest uses setup_method and teardown_method for per-test setup/teardown.
🧠 Conceptual
expert
3:00remaining
Understanding test isolation in Selenium test classes
Why is it important to create a new WebDriver instance in the setup method before each test instead of reusing the same driver instance across multiple tests?
ATo ensure each test runs in a clean browser state, avoiding side effects from previous tests
BTo reduce the total test execution time by reusing browser sessions
CBecause WebDriver instances cannot be reused due to API limitations
DTo avoid memory leaks caused by closing the browser too often
Attempts:
2 left
💡 Hint
Think about how leftover data or state can affect test results.