0
0
Selenium Pythontesting~20 mins

Test class using page objects in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Page Object Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a Selenium test using Page Object
What will be the output of this test run if the login page loads correctly and the username field is present?
Selenium Python
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webdriver import WebDriver

class LoginPage:
    def __init__(self, driver: WebDriver):
        self.driver = driver
        self.username_input = (By.ID, 'username')

    def is_username_displayed(self):
        return self.driver.find_element(*self.username_input).is_displayed()


class TestLogin:
    def test_username_field(self, driver):
        page = LoginPage(driver)
        assert page.is_username_displayed() is True
        print('Test Passed')
ATest Passed
BAssertionError
CSyntaxError
DNoSuchElementException
Attempts:
2 left
💡 Hint
Think about what happens if the element is found and is visible.
locator
intermediate
1:30remaining
Best locator strategy in Page Object
Which locator is the best practice to use in a Page Object for a login button with id 'loginBtn'?
ABy.ID, 'loginBtn'
BBy.XPATH, "//button[text()='Login']"
CBy.CSS_SELECTOR, 'button.login-button'
DBy.CLASS_NAME, 'btn'
Attempts:
2 left
💡 Hint
IDs are unique and fast to locate.
assertion
advanced
1:30remaining
Correct assertion for page title in test class
Which assertion correctly verifies the page title is exactly 'Dashboard' in a Selenium test class?
Selenium Python
def test_page_title(self, driver):
    driver.get('https://example.com/dashboard')
Aassert driver.title != 'Dashboard'
Bassert 'Dashboard' in driver.title
Cassert driver.title == 'Dashboard'
Dassert driver.title is 'Dashboard'
Attempts:
2 left
💡 Hint
Use equality operator for exact match.
🔧 Debug
advanced
2:00remaining
Identify error in test class using Page Object
What error will this test raise when run?
Selenium Python
from selenium.webdriver.common.by import By

class LoginPage:
    def __init__(self, driver):
        self.driver = driver
        self.password_input = (By.ID, 'password')

    def enter_password(self, pwd):
        self.driver.find_element(self.password_input).send_keys(pwd)

class TestLogin:
    def test_password_entry(self, driver):
        page = LoginPage(driver)
        page.enter_password('secret')
ANoSuchElementException
BTypeError: find_element() takes 2 positional arguments but 1 was given
CAttributeError: 'tuple' object has no attribute 'send_keys'
DNo error, test passes
Attempts:
2 left
💡 Hint
Check how find_element is called with tuple argument.
framework
expert
3:00remaining
Test class setup and teardown with Page Object
Which test class structure correctly uses setup and teardown methods with a Page Object in pytest style?
A
import unittest
from selenium import webdriver

class TestLogin(unittest.TestCase):
    def setup(self):
        self.driver = webdriver.Chrome()
        self.page = LoginPage(self.driver)

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

    def test_login(self):
        self.page.open()
        self.assertTrue(self.page.is_loaded())
B
import pytest
from selenium import webdriver

class TestLogin:
    def setup(self):
        self.driver = webdriver.Chrome()
        self.page = LoginPage(self.driver)

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

    def test_login(self):
        self.page.open()
        assert self.page.is_loaded()
C
import unittest
from selenium import webdriver

class TestLogin(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.page = LoginPage(self.driver)

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

    def test_login(self):
        self.page.open()
        self.assertTrue(self.page.is_loaded())
D
import pytest
from selenium import webdriver

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

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

    def test_login(self):
        self.page.open()
        assert self.page.is_loaded()
Attempts:
2 left
💡 Hint
pytest uses setup_method and teardown_method for per-test setup/teardown.