_python
TL;DR - Show Me The Code
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select as WebDriverSelect
@pytest.fixture
def driver_setup_teardown():
driver = webdriver.Firefox()
yield driver
driver.quit()
def test_example_1(driver_setup_teardown):
driver = driver_setup_teardown
driver.get('https://the-internet.herokuapp.com/dropdown')
dropdown_list = driver.find_element(By.ID , 'dropdown')
options = dropdown_list.find_elements(By.TAG_NAME, 'option')
for opt in options:
if opt.text == 'Option 1':
opt.click()
break
for opt in options:
if opt.is_selected():
selected_option = opt.text
break
assert selected_option == 'Option 1', "Selected option should be Option 1"
def test_example_2(driver_setup_teardown):
driver = driver_setup_teardown
driver.get('https://the-internet.herokuapp.com/dropdown')
dropdown = driver.find_element(By.ID , 'dropdown')
select_list = WebDriverSelect(dropdown)
select_list.select_by_visible_text('Option 1')
selected_option = select_list.first_selected_option.text
assert selected_option == 'Option 1', "Selected option should be Option 1"
Code Walkthrough
Importing Libraries, Setup and Teardown
The first lines are pulling in our requisite libraries our testing framework (e.g., import pytest
),
driving the browser with Selenium (e.g., from selenium import webdriver
), and a locator strategy to
find the elements (e.g. from selenium.webdriver.common.by import By
), and wire up some setup and
teardown methods for our tests.
Example 1
After visiting the example application we find the dropdown list by its ID and store it in a variable. We
then find each clickable element in the dropdown list (e.g., each option
) with find_elements(By.TAG_NAME, 'option')
.
Grabbing all options returns a collection that we iterate over and when the text matches what we want it will click on it.
We finish the test by performing a check to see that our selection was made correctly. This is done by reiterating over the dropdown options collection one more time. This time we're getting the text of the item that was selected, storing it in a variable, and making an assertion against it.
While this works, there is a simpler, built-in way to do this with Selenium. Let's give that a go.
Example 2
Similar to the first example, we are finding the dropdown list by its ID. But instead of iterating over its option
elements and clicking based on a conditional check, we are leveraging a built-in helper function of Selenium,
Select
, and its select_by_visibile_text
method to choose the item we want.
We then ask the select_list
what option was selected by using the first_selected_option
method. This
returns a Selenium Element that we grab the text from, storing it in a variable (e.g., selected_option
).
Then we perform our assertion against this variable (just like in the previous example).
In addition to selecting by text, you can also select by value.
select_list.select_by_value('1')
Executing the Test
Before executing the test, we need to install the required libraries. We can do this by running
pip install -r requirements.txt
from the command-line. The requirements.txt
file contains the libraries we
need to install.
Toggle to see the requirements.txt
file.
selenium==4.24.0
pytest
After installing the required libraries, we can run the test by executing pytest dropdown.py
from the command-line.