Mastering Selenium Testing with Python: A Practical Guide to Interactions
In the world of automated web testing, Selenium with Python stands out for its ability to automate browser actions, making it an indispensable tool for QA engineers and developers. Today, we’ll dive into a practical lesson on Selenium testing, focusing on interactions such as drag-and-drop, select boxes, and keypress events. Our playground will be the website https://practiceautomatedtesting.com, a resource designed for practicing automated testing techniques.
Setting Up the Environment
Before we begin, ensure you have Python and Selenium installed in your environment. Selenium requires a driver to interface with the chosen browser. For Chrome, you can use Chromedriver. Here’s a simplified setup using PyTest fixtures for Chrome:
@pytest.fixture
def driver():
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
# Set up Chrome options
chrome_options = Options()
chrome_options.add_argument("--start-maximized")
# Initialize the Chrome driver with automatic driver management
driver = webdriver.Chrome(options=chrome_options)
yield driver
# Clean up
driver.quit()
This fixture initializes the ChromeDriver with maximized window size and ensures clean-up after tests.
Testing Drag and Drop
One common web interaction is drag-and-drop. Let’s test this functionality using Selenium’s ActionChains
.
def navigate_to_drag_and_drop_page(driver):
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Navigate to the Interactions section
driver.get("https://practiceautomatedtesting.com/webelements")
interactions_menu = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//summary[contains(., 'Interactions')]"))
)
interactions_menu.click()
# Access the Drag and Drop page
drag_drop_link = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//a[contains(., 'Drag and Drop')]"))
)
drag_drop_link.click()
This function navigates to the drag-and-drop page. Now, let’s perform and test the drag-and-drop action:
def test_simple_drag_and_drop(driver):
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
navigate_to_drag_and_drop_page(driver)
# Identify elements
draggable = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "div[draggable='true']"))
)
drop_target = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, ".dropzone"))
)
# Perform drag and drop
ActionChains(driver).drag_and_drop(draggable, drop_target).perform()
# Verify success
assert drop_target.text == "😊"
This test simulates the drag-and-drop action and verifies its success by checking the drop zone’s text.
Testing Select Boxes
Interacting with select boxes is another common task. Let’s automate the selection of different options:
def navigate_to_select_box_page(driver):
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
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()
select_box_link = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//a[contains(., 'Select Box')]"))
)
select_box_link.click()
Using the Select
class from Selenium’s support.ui
module, we can easily interact with select elements:
def test_select_box_interactions(driver):
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
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 by value
select.select_by_value("apple")
assert select_element.get_attribute("value") == "apple"
This test demonstrates how to select an option by its value and verify the selection.
Testing Keypress Events
Keypress events can be simulated using the send_keys
method. Let’s test single and multiple keypresses, including special keys:
def navigate_to_keypress_page(driver):
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver.get("https://practiceautomatedtesting.com/webelements")
interactions_menu = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//summary[contains(., 'Interactions')]"))
)
interactions_menu.click()
keypress_link = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//a[contains(., 'Keypress')]"))
)
keypress_link.click()
Testing single and multiple keypresses:
def test_single_key_press(driver):
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
navigate_to_keypress_page(driver)
input_field = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CLASS_NAME, "KeyPresses_inputBox__-5die"))
)
input_field.send_keys('a')
result = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CLASS_NAME, "KeyPresses_keyPressed__k5B5a"))
)
assert result.text == "You pressed: a"
This guide covers testing key web interactions with Selenium and Python. Through practical examples, we’ve demonstrated how to set up tests, navigate pages, and interact with different web elements. Selenium’s power and flexibility make it an excellent tool for automating browser tasks, significantly improving testing efficiency and coverage.