Mastering P Selenium Elements like Actions, Links, and Selections
In today’s fast-paced development environment, ensuring the reliability and functionality of web applications is paramount. Python, with its simplicity and vast ecosystem, provides excellent tools for automated web testing, one of which is Selenium. Selenium is a powerful tool for automating web browsers, enabling developers to simulate user interactions with web pages. This lesson focuses on Python Selenium testing, covering several actions such as clicking links, selecting radio buttons and checkboxes, and handling select dropdowns. Let’s dive into how to perform these actions and validate the behavior of a web application.
Setting Up the Selenium Environment
First, ensure you have Selenium installed in your Python environment. You can install Selenium using pip:
pip install selenium
For this lesson, we’re going to use Chrome as our web browser. Make sure you have the Chrome WebDriver installed and properly configured in your PATH. Selenium requires a driver to interface with the chosen browser, and ChromeDriver is the link between your Selenium tests and the Chrome browser.
Writing Test Cases with Selenium
Let’s start by constructing our test environment and writing tests for different web elements including radio buttons, checkboxes, select boxes, and links.
1. Testing Radio Buttons
Selecting radio buttons is a common task in web form automation. Below is an example of how to navigate to a page with radio buttons, select different options, and assert the selections:
import pytest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class TestRadioButton:
@pytest.fixture
def driver(self):
chrome_options = Options()
chrome_options.add_argument("--start-maximized")
driver = webdriver.Chrome(options=chrome_options)
yield driver
driver.quit()
def navigate_to_radio_page(self, driver):
driver.get("https://practiceautomatedtesting.com/webelements")
elements_menu = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//summary[contains(., 'Elements')]"))
)
elements_menu.click()
radio_link = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//a[contains(., 'Radio Button')]"))
)
radio_link.click()
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, "[class*='componentContainer']"))
)
def test_radio_button_selection(self, driver):
self.navigate_to_radio_page(driver)
radio1 = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "radio1")))
radio2 = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "radio2")))
radio2.click()
message2 = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//*[contains(text(), 'You selected: Radio 2')]"))
)
assert message2.is_displayed()
radio1.click()
message1 = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//*[contains(text(), 'You selected: Radio 1')]"))
)
assert message1.is_displayed()
DO IT YOURSELF –> try to copy this test in your own repo in order to do a python test with radio buttons
2. Testing Checkboxes
Similar to radio buttons, testing checkboxes involves navigating to a page, locating the checkboxes, and interacting with them:
class TestCheckbox:
# Assuming the same driver setup as TestRadioButton
def navigate_to_checkbox_page(self, driver):
# Similar navigation code as navigate_to_radio_page
def test_checkbox_label_clicks(self, driver):
self.navigate_to_checkbox_page(driver)
checkbox1 = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "checkbox1"))
)
checkbox2 = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "checkbox2"))
)
label1 = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//label[contains(text(), 'Checkbox 1')]"))
)
label1.click()
assert checkbox1.is_selected()
# Similar assertions for checkbox2 and unchecking
DO IT YOURSELF –> try to copy this test in your own repo in order to do a python test with checkboxes, using the login which is defined in the radio button example
3. Testing Select Boxes
Handling select boxes requires identifying the select web element and using Selenium’s Select
class to interact with it:
from selenium.webdriver.support.ui import Select
class TestSelectBox:
# Assuming the same driver setup as TestRadioButton
def navigate_to_select_box_page(self, driver):
# Similar navigation code as navigate_to_radio_page
def test_select_box_interactions(self, driver):
self.navigate_to_select_box_page(driver)
select_element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.TAG_NAME, "select"))
)
select = Select(select_element)
select.select_by_value("apple")
assert select_element.get_attribute("value") == "apple"
# Similar assertions for other selection methods
DO IT YOURSELF –> try to copy this test in your own repo in order to do a python test with Selectbox, using the login which is defined in the radio button example
4. Testing Links
Testing links involves navigating to a page with links, clicking on them, and verifying the navigation was successful:
class TestLinks:
# Assuming the same driver setup as TestRadioButton
def navigate_to_links_page(self, driver):
# Similar navigation code as navigate_to_radio_page
def test_analyze_page_structure(self, driver):
self.navigate_to_links_page(driver)
links = WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located((By.CSS_SELECTOR, "[class*='componentContainer'] a"))
)
for link in links:
print(f"Link text: \"{link.text}\", href: \"{link.get_attribute('href')}\"")
def test_verify_regular_links_functionality(self, driver):
self.navigate_to_links_page(driver)
home_link = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//a[contains(., 'Home Page')]"))
)
assert home_link.get_attribute('href') == "https://practiceautomatedtesting.com/"
# Similar assertions for other links
DO IT YOURSELF –> try to copy this test in your own repo in order to do a python test with Selectbox, using the login which is defined in the radio button example
Conclusion
This lesson covered the essentials of testing web elements using Python and Selenium. We explored how to set up a test environment, navigate web pages, and interact with various web elements like radio buttons, checkboxes, select boxes, and links. Through these examples, you’ve learned how to automate web testing tasks, significantly improving your testing efficiency and coverage. Remember, the key to mastering Selenium testing is practice and exploration, so don’t hesitate to experiment with different web elements and scenarios.