Skip to content

Commit d368a93

Browse files
authored
Move Python examples in Windows interactions docs (#2629)
* 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. * Move Python examples for closing window and switching to new window to test file Moves the inline Python code examples for closing a window and switching to a new window into examples/python/tests/interactions/test_windows.py and references it from the docs via gh-codeblock. * Fix indentation in python gh-codeblock for windows documentation example * Add windows markdown files for Japanese, Chinese, and Portuguese languages. * Fix window handle handling in switch and close tests - Wait for new window to open before reading window handles - Switch back to original window after driver.close() --------- Co-authored-by: Pallavi Sharma <write2pallavi@gmail.com> [deploy site]
1 parent 6e410b3 commit d368a93

5 files changed

Lines changed: 104 additions & 187 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,58 @@
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+
driver.get(url)
23+
wait = WebDriverWait(driver, 10)
24+
25+
original_window_handle = driver.current_window_handle
26+
27+
driver.find_element(By.LINK_TEXT, "Open new window").click()
28+
wait.until(EC.number_of_windows_to_be(2))
29+
30+
new_window_handle = (set(driver.window_handles) - {original_window_handle}).pop()
31+
driver.switch_to.window(new_window_handle)
32+
assert driver.current_window_handle == new_window_handle
33+
34+
def test_close_window(driver):
35+
driver.get(url)
36+
wait = WebDriverWait(driver, 10)
37+
38+
original_window_handle = driver.current_window_handle
39+
40+
driver.find_element(By.LINK_TEXT, "Open new window").click()
41+
wait.until(EC.number_of_windows_to_be(2))
42+
43+
new_window_handle = (set(driver.window_handles) - {original_window_handle}).pop()
44+
driver.switch_to.window(new_window_handle)
45+
46+
driver.close()
47+
driver.switch_to.window(original_window_handle)
48+
49+
assert driver.current_window_handle == original_window_handle
50+
assert len(driver.window_handles) == 1
51+
52+
def test_create_new_window(driver):
53+
# Opens a new tab and switches to new tab
54+
driver.switch_to.new_window('tab')
55+
assert driver.title == ""
56+
# Opens a new window and switches to new window
57+
driver.switch_to.new_window('window')
58+
assert driver.title == ""

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

Lines changed: 12 additions & 46 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 >}}
@@ -176,12 +148,8 @@ handle stored in a variable. Put this together and you will get:
176148
{{< tab header="Java" text=true >}}
177149
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L31-L34" >}}
178150
{{< /tab >}}
179-
{{< tab header="Python" >}}
180-
#Close the tab or window
181-
driver.close()
182-
183-
#Switch back to the old tab or window
184-
driver.switch_to.window(original_window)
151+
{{< tab header="Python" text=true >}}
152+
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L46-L46" >}}
185153
{{< /tab >}}
186154

187155
{{< tab header="CSharp" text=true >}}
@@ -230,12 +198,8 @@ __Note: This feature works with Selenium 4 and later versions.__
230198
{{< tab header="Java" text=true >}}
231199
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L36-L42" >}}
232200
{{< /tab >}}
233-
{{< tab header="Python" >}}
234-
# Opens a new tab and switches to new tab
235-
driver.switch_to.new_window('tab')
236-
237-
# Opens a new window and switches to new window
238-
driver.switch_to.new_window('window')
201+
{{< tab header="Python" text=true >}}
202+
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L53-L58" >}}
239203
{{< /tab >}}
240204

241205

@@ -278,7 +242,9 @@ instead of close:
278242
{{< tab header="Java" text=true >}}
279243
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L44-L45" >}}
280244
{{< /tab >}}
281-
{{< tab header="Python" >}}driver.quit(){{< /tab >}}
245+
{{< tab header="Python" text=true >}}
246+
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L13-L13" >}}
247+
{{< /tab >}}
282248

283249
{{< tab header="CSharp" text=true >}}
284250
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L45-L46" >}}

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

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ WebDriverは、ウィンドウとタブを区別しません。
2020
{{< tab header="Java" text=true >}}
2121
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L16-L20" >}}
2222
{{< /tab >}}
23-
{{< tab header="Python" >}}driver.current_window_handle{{< /tab >}}
23+
{{< tab header="Python" text=true >}}
24+
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L16-L18" >}}
25+
{{< /tab >}}
2426
{{< tab header="CSharp" text=true >}}
2527
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L17-L21" >}}
2628
{{< /tab >}}
@@ -41,39 +43,8 @@ WebDriverは、ウィンドウとタブを区別しません。
4143
{{< tab header="Java" text=true >}}
4244
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L22-L29" >}}
4345
{{< /tab >}}
44-
{{< tab header="Python" >}}
45-
from selenium import webdriver
46-
from selenium.webdriver.support.ui import WebDriverWait
47-
from selenium.webdriver.support import expected_conditions as EC
48-
49-
# Start the driver
50-
with webdriver.Firefox() as driver:
51-
# Open URL
52-
driver.get("https://seleniumhq.github.io")
53-
54-
# Setup wait for later
55-
wait = WebDriverWait(driver, 10)
56-
57-
# Store the ID of the original window
58-
original_window = driver.current_window_handle
59-
60-
# Check we don't have other windows open already
61-
assert len(driver.window_handles) == 1
62-
63-
# Click the link which opens in a new window
64-
driver.find_element(By.LINK_TEXT, "new window").click()
65-
66-
# Wait for the new window or tab
67-
wait.until(EC.number_of_windows_to_be(2))
68-
69-
# Loop through until we find a new window handle
70-
for window_handle in driver.window_handles:
71-
if window_handle != original_window:
72-
driver.switch_to.window(window_handle)
73-
break
74-
75-
# Wait for the new tab to finish loading content
76-
wait.until(EC.title_is("SeleniumHQ Browser Automation"))
46+
{{< tab header="Python" text=true >}}
47+
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L22-L32" >}}
7748
{{< /tab >}}
7849

7950
{{< tab header="CSharp" text=true >}}
@@ -169,12 +140,8 @@ wait.until(titleIs("Selenium documentation"))
169140
{{< tab header="Java" text=true >}}
170141
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L31-L34" >}}
171142
{{< /tab >}}
172-
{{< tab header="Python" >}}
173-
#Close the tab or window
174-
driver.close()
175-
176-
#Switch back to the old tab or window
177-
driver.switch_to.window(original_window)
143+
{{< tab header="Python" text=true >}}
144+
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L46-L46" >}}
178145
{{< /tab >}}
179146

180147
{{< tab header="CSharp" text=true >}}
@@ -220,12 +187,8 @@ __注意: この機能は、Selenium 4以降のバージョンで機能します
220187
{{< tab header="Java" text=true >}}
221188
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L36-L42" >}}
222189
{{< /tab >}}
223-
{{< tab header="Python" >}}
224-
# Opens a new tab and switches to new tab
225-
driver.switch_to.new_window('tab')
226-
227-
# Opens a new window and switches to new window
228-
driver.switch_to.new_window('window')
190+
{{< tab header="Python" text=true >}}
191+
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L53-L58" >}}
229192
{{< /tab >}}
230193

231194
{{< tab header="CSharp" text=true >}}
@@ -268,7 +231,9 @@ driver.switchTo().newWindow(WindowType.WINDOW)
268231
{{< tab header="Java" text=true >}}
269232
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L44-L45" >}}
270233
{{< /tab >}}
271-
{{< tab header="Python" >}}driver.quit(){{< /tab >}}
234+
{{< tab header="Python" text=true >}}
235+
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L13-L13" >}}
236+
{{< /tab >}}
272237
{{< tab header="CSharp" text=true >}}
273238
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L45-L46" >}}
274239
{{< /tab >}}

website_and_docs/content/documentation/webdriver/interactions/windows.pt-br.md

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ persistente em uma única sessão. Você pode pegar o identificador atual usando
2020
{{< tab header="Java" text=true >}}
2121
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L16-L20" >}}
2222
{{< /tab >}}
23-
{{< tab header="Python" >}}driver.current_window_handle{{< /tab >}}
23+
{{< tab header="Python" text=true >}}
24+
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L16-L18" >}}
25+
{{< /tab >}}
2426
{{< tab header="CSharp" text=true >}}
2527
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L17-L21" >}}
2628
{{< /tab >}}
@@ -47,39 +49,8 @@ que cria uma nova guia (ou) nova janela e muda automaticamente para ela.
4749
{{< tab header="Java" text=true >}}
4850
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L22-L29" >}}
4951
{{< /tab >}}
50-
{{< tab header="Python" >}}
51-
from selenium import webdriver
52-
from selenium.webdriver.support.ui import WebDriverWait
53-
from selenium.webdriver.support import expected_conditions as EC
54-
55-
# Start the driver
56-
with webdriver.Firefox() as driver:
57-
# Open URL
58-
driver.get("https://seleniumhq.github.io")
59-
60-
# Setup wait for later
61-
wait = WebDriverWait(driver, 10)
62-
63-
# Store the ID of the original window
64-
original_window = driver.current_window_handle
65-
66-
# Check we don't have other windows open already
67-
assert len(driver.window_handles) == 1
68-
69-
# Click the link which opens in a new window
70-
driver.find_element(By.LINK_TEXT, "new window").click()
71-
72-
# Wait for the new window or tab
73-
wait.until(EC.number_of_windows_to_be(2))
74-
75-
# Loop through until we find a new window handle
76-
for window_handle in driver.window_handles:
77-
if window_handle != original_window:
78-
driver.switch_to.window(window_handle)
79-
break
80-
81-
# Wait for the new tab to finish loading content
82-
wait.until(EC.title_is("SeleniumHQ Browser Automation"))
52+
{{< tab header="Python" text=true >}}
53+
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L22-L32" >}}
8354
{{< /tab >}}
8455

8556
{{< tab header="CSharp" text=true >}}
@@ -178,12 +149,8 @@ anterior armazenado em uma variável. Junte isso e você obterá:
178149
{{< tab header="Java" text=true >}}
179150
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L31-L34" >}}
180151
{{< /tab >}}
181-
{{< tab header="Python" >}}
182-
#Close the tab or window
183-
driver.close()
184-
185-
#Switch back to the old tab or window
186-
driver.switch_to.window(original_window)
152+
{{< tab header="Python" text=true >}}
153+
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L46-L46" >}}
187154
{{< /tab >}}
188155

189156
{{< tab header="CSharp" text=true >}}
@@ -230,12 +197,8 @@ __Nota: este recurso funciona com Selenium 4 e versões posteriores.__
230197
{{< tab header="Java" text=true >}}
231198
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L36-L42" >}}
232199
{{< /tab >}}
233-
{{< tab header="Python" >}}
234-
# Opens a new tab and switches to new tab
235-
driver.switch_to.new_window('tab')
236-
237-
# Opens a new window and switches to new window
238-
driver.switch_to.new_window('window')
200+
{{< tab header="Python" text=true >}}
201+
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L53-L58" >}}
239202
{{< /tab >}}
240203

241204
{{< tab header="CSharp" text=true >}}
@@ -279,7 +242,9 @@ em vez de fechar:
279242
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L44-L45" >}}
280243
{{< /tab >}}
281244

282-
{{< tab header="Python" >}}driver.quit(){{< /tab >}}
245+
{{< tab header="Python" text=true >}}
246+
{{< gh-codeblock path="/examples/python/tests/interactions/test_windows.py#L13-L13" >}}
247+
{{< /tab >}}
283248
{{< tab header="CSharp" text=true >}}
284249
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Interactions/WindowsTest.cs#L45-L46" >}}
285250
{{< /tab >}}

0 commit comments

Comments
 (0)