_python
TL;DR - Show Me The Code
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
@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/windows')
driver.find_element(By.CSS_SELECTOR , '.example a').click()
driver.switch_to.window(driver.window_handles[0])
assert driver.title == "The Internet", "title should be The Internet"
driver.switch_to.window(driver.window_handles[-1])
# Waiting for the new window to have a title, then we consider it is loaded
WebDriverWait(driver, 2).until(lambda d: d.title != "")
assert driver.title == "New Window", "title should be New Window"
def test_example_2(driver_setup_teardown):
driver = driver_setup_teardown
driver.get('https://the-internet.herokuapp.com/windows')
first_window = driver.window_handles[0]
driver.find_element(By.CSS_SELECTOR , '.example a').click()
all_windows = driver.window_handles
for window in all_windows:
if window != first_window:
new_window = window
driver.switch_to.window(first_window)
assert driver.title == "The Internet", "title should be The Internet"
driver.switch_to.window(new_window)
# Waiting for the new window to have a title, then we consider it is loaded
WebDriverWait(driver, 2).until(lambda d: d.title != "")
assert driver.title == "New Window", "title should be New Window"
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
), Selenium's wait functionality
(e.g., from selenium.webdriver.support.ui import WebDriverWait
), and wire up some setup and
teardown methods for our tests.
Example 1
Line 13 shows a test that exercises new window functionality from an application. In this case, we'll be using the new window example from the-internet.
After loading the page we click the link which spawns a new window. We then grab the window handles (a.k.a. unique identifier strings which represent each open browser window) and switch between them based on their order (assuming that the first one is the originating window, and that the second one is the new window). We round this test out by performing a simple check against the title of the page to make sure Selenium is focused on the correct window.
While this may seem like a good approach it can present problems later because the order of the window handles is not consistent across all browsers. Some return it in the order opened, others alphabetically.
Here's a more resilient approach. One that will work across all browsers.
Example 2
Line 24 shows that after loading the page we store the window handles in a variable (e.g., first_window
)
and then proceed with clicking the new window link.
Now that we have two windows open we grab all the window handles again (e.g., windowHandlesAfter
) and search
through them to find the new window handle (e.g., the handle that's in the new window handle collection but not the
initial one). We store the result in another variable (e.g., new_window
) and then switch between the windows each
time checking the page title to make sure the correct window is in focus.
For more information about switching windows (and tabs) visit the official Selenium documentation page.
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.26.1
pytest
After installing the required libraries, we can run the test by executing pytest windows.py
from the command-line.