Skip to content

Commit 594eeb5

Browse files
committed
Move Python currrent window handle and switching window example to test file
Moves the inline Python examples for getting the current window handle and switching window into examples/python/tests/interactions/test_windows.py, and references it from the docs via gh-codeblock.
1 parent 203cffc commit 594eeb5

2 files changed

Lines changed: 35 additions & 33 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,32 @@
1+
import pytest
12
from selenium import webdriver
3+
from selenium.webdriver.support.ui import WebDriverWait
4+
from selenium.webdriver.common.by import By
5+
from selenium.webdriver.support import expected_conditions as EC
26

7+
url = "https://www.selenium.dev/selenium/web/window_switching_tests/page_with_frame.html"
8+
9+
@pytest.fixture()
10+
def driver():
11+
driver = webdriver.Chrome()
12+
yield driver
13+
driver.quit()
14+
15+
def test_current_window_handle(driver):
16+
driver.get(url)
17+
current_handle = driver.current_window_handle
18+
assert current_handle is not None
19+
20+
21+
def test_switch_to_window(driver):
22+
wait = WebDriverWait(driver, 10)
23+
driver.get(url)
24+
original_window_handles = set(driver.window_handles)
25+
assert len(original_window_handles) == 1
26+
driver.find_element(By.LINK_TEXT, "Open new window").click()
27+
wait.until(EC.number_of_windows_to_be(2))
28+
new_handles = set(driver.window_handles) - original_window_handles
29+
assert len(new_handles) == 1
30+
new_window_handle = new_handles.pop()
31+
driver.switch_to.window(new_window_handle)
32+
assert driver.current_window_handle == new_window_handle

website_and_docs/content/documentation/webdriver/interactions/windows.en.md

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ current window by using:
2222
{{< tab header="Java" text=true >}}
2323
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L16-L20" >}}
2424
{{< /tab >}}
25-
{{< tab header="Python" >}}driver.current_window_handle{{< /tab >}}
25+
{{< tab header="Python" text=true >}}
26+
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L16-L18" >}}
27+
{{< /tab >}}
2628
{{< tab header="CSharp" text=true >}}
2729
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L17-L21" >}}
2830
{{< /tab >}}
@@ -46,38 +48,8 @@ window is launched. So first position will be default browser, and so on.
4648
{{< tab header="Java" text=true >}}
4749
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L22-L29" >}}
4850
{{< /tab >}}
49-
{{< tab header="Python" >}}
50-
from selenium import webdriver
51-
from selenium.webdriver.support.ui import WebDriverWait
52-
from selenium.webdriver.support import expected_conditions as EC
53-
54-
with webdriver.Firefox() as driver:
55-
# Open URL
56-
driver.get("https://seleniumhq.github.io")
57-
58-
# Setup wait for later
59-
wait = WebDriverWait(driver, 10)
60-
61-
# Store the ID of the original window
62-
original_window = driver.current_window_handle
63-
64-
# Check we don't have other windows open already
65-
assert len(driver.window_handles) == 1
66-
67-
# Click the link which opens in a new window
68-
driver.find_element(By.LINK_TEXT, "new window").click()
69-
70-
# Wait for the new window or tab
71-
wait.until(EC.number_of_windows_to_be(2))
72-
73-
# Loop through until we find a new window handle
74-
for window_handle in driver.window_handles:
75-
if window_handle != original_window:
76-
driver.switch_to.window(window_handle)
77-
break
78-
79-
# Wait for the new tab to finish loading content
80-
wait.until(EC.title_is("SeleniumHQ Browser Automation"))
51+
{{< tab header="Python" text=true >}}
52+
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L22-L32" >}}
8153
{{< /tab >}}
8254

8355
{{< tab header="CSharp" text=true >}}

0 commit comments

Comments
 (0)