Skip to content

Commit 8ea6e02

Browse files
Added example code to windows and modified text (#1754)[deploy site]
* added example code for windows and switched text order. modified some text also * fixed tab pane hugo error --------- Co-authored-by: Sri Harsha <[email protected]>
1 parent 2bd31eb commit 8ea6e02

File tree

5 files changed

+299
-352
lines changed

5 files changed

+299
-352
lines changed
Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,48 @@
11
package dev.selenium.interactions;
22

3-
import dev.selenium.BaseTest;
3+
import org.openqa.selenium.*;
4+
import org.openqa.selenium.chrome.ChromeDriver;
5+
import java.time.Duration;
6+
import org.junit.jupiter.api.Test;
7+
import static org.junit.jupiter.api.Assertions.*;
48

5-
public class WindowsTest extends BaseTest {
9+
public class WindowsTest {
610

7-
}
11+
@Test
12+
public void windowsExampleCode() {
13+
14+
WebDriver driver = new ChromeDriver();
15+
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
16+
// Navigate to Url
17+
driver.get("https://www.selenium.dev/selenium/web/window_switching_tests/page_with_frame.html");
18+
//fetch handle of this
19+
String currHandle=driver.getWindowHandle();
20+
assertNotNull(currHandle);
21+
22+
//click on link to open a new window
23+
driver.findElement(By.linkText("Open new window")).click();
24+
//fetch handles of all windows, there will be two, [0]- default, [1] - new window
25+
Object[] windowHandles=driver.getWindowHandles().toArray();
26+
driver.switchTo().window((String) windowHandles[1]);
27+
//assert on title of new window
28+
String title=driver.getTitle();
29+
assertEquals("Simple Page",title);
30+
31+
//closing current window
32+
driver.close();
33+
//Switch back to the old tab or window
34+
driver.switchTo().window((String) windowHandles[0]);
35+
36+
//Opens a new tab and switches to new tab
37+
driver.switchTo().newWindow(WindowType.TAB);
38+
assertEquals("",driver.getTitle());
39+
40+
//Opens a new window and switches to new window
41+
driver.switchTo().newWindow(WindowType.WINDOW);
42+
assertEquals("",driver.getTitle());
43+
44+
//quitting driver
45+
driver.quit(); //close all windows
46+
47+
}
48+
}

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

Lines changed: 67 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ current window by using:
1919

2020
{{< tabpane langEqualsHeader=true >}}
2121
{{< badge-examples >}}
22-
{{< tab header="Java" >}}driver.getWindowHandle();{{< /tab >}}
22+
{{< tab header="Java" text=true >}}
23+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L16-L20" >}}
24+
{{< /tab >}}
2325
{{< tab header="Python" >}}driver.current_window_handle{{< /tab >}}
2426
{{< tab header="CSharp" >}}driver.CurrentWindowHandle;{{< /tab >}}
2527
{{< tab header="Ruby" >}}driver.window_handle{{< /tab >}}
@@ -33,40 +35,15 @@ Clicking a link which opens in a
3335
<a href="https://seleniumhq.github.io" target="_blank"> new window</a>
3436
will focus the new window or tab on screen, but WebDriver will not know which
3537
window the Operating System considers active. To work with the new window
36-
you will need to switch to it. If you have only two tabs or windows open,
37-
and you know which window you start with, by the process of elimination
38-
you can loop over both windows or tabs that WebDriver can see, and switch
39-
to the one which is not the original.
40-
41-
However, Selenium 4 provides a new api [NewWindow](#create-new-window-or-new-tab-and-switch)
42-
which creates a new tab (or) new window and automatically switches to it.
38+
you will need to switch to it. For this, we fetch all window handles,
39+
and store them in an array. The array position fills in the order the
40+
window is launched. So first position will be default browser, and so on.
4341

4442
{{< tabpane langEqualsHeader=true >}}
4543
{{< badge-examples >}}
46-
{{< tab header="Java" >}}
47-
//Store the ID of the original window
48-
String originalWindow = driver.getWindowHandle();
49-
50-
//Check we don't have other windows open already
51-
assert driver.getWindowHandles().size() == 1;
52-
53-
//Click the link which opens in a new window
54-
driver.findElement(By.linkText("new window")).click();
55-
56-
//Wait for the new window or tab
57-
wait.until(numberOfWindowsToBe(2));
58-
59-
//Loop through until we find a new window handle
60-
for (String windowHandle : driver.getWindowHandles()) {
61-
if(!originalWindow.contentEquals(windowHandle)) {
62-
driver.switchTo().window(windowHandle);
63-
break;
64-
}
65-
}
66-
67-
//Wait for the new tab to finish loading content
68-
wait.until(titleIs("Selenium documentation"));
69-
{{< /tab >}}
44+
{{< tab header="Java" text=true >}}
45+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L22-L29" >}}
46+
{{< /tab >}}
7047
{{< tab header="Python" >}}
7148
from selenium import webdriver
7249
from selenium.webdriver.support.ui import WebDriverWait
@@ -204,60 +181,6 @@ wait.until(titleIs("Selenium documentation"))
204181
{{< /tab >}}
205182
{{< /tabpane >}}
206183

207-
### Create new window (or) new tab and switch
208-
Creates a new window (or) tab and will focus the new window or tab on screen.
209-
You don't need to switch to work with the new window (or) tab. If you have more than two windows
210-
(or) tabs opened other than the new window, you can loop over both windows or tabs that WebDriver can see,
211-
and switch to the one which is not the original.
212-
213-
__Note: This feature works with Selenium 4 and later versions.__
214-
215-
{{< tabpane langEqualsHeader=true >}}
216-
{{< badge-examples >}}
217-
{{< tab header="Java" >}}
218-
// Opens a new tab and switches to new tab
219-
driver.switchTo().newWindow(WindowType.TAB);
220-
221-
// Opens a new window and switches to new window
222-
driver.switchTo().newWindow(WindowType.WINDOW);
223-
{{< /tab >}}
224-
{{< tab header="Python" >}}
225-
# Opens a new tab and switches to new tab
226-
driver.switch_to.new_window('tab')
227-
228-
# Opens a new window and switches to new window
229-
driver.switch_to.new_window('window')
230-
{{< /tab >}}
231-
{{< tab header="CSharp" >}}
232-
// Opens a new tab and switches to new tab
233-
driver.SwitchTo().NewWindow(WindowType.Tab)
234-
235-
// Opens a new window and switches to new window
236-
driver.SwitchTo().NewWindow(WindowType.Window)
237-
{{< /tab >}}
238-
{{% tab header="Ruby" text=true %}}
239-
Opens a new tab and switches to new tab:
240-
{{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L9" >}}
241-
242-
Opens a new window and switches to new window:
243-
{{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L15" >}}
244-
{{% /tab %}}
245-
{{< tab header="JavaScript" text=true >}}
246-
Opens a new tab and switches to new tab
247-
{{< gh-codeblock path="examples/javascript/test/interactions/windows.spec.js#L70" >}}
248-
249-
Opens a new window and switches to new window:
250-
{{< gh-codeblock path="examples/javascript/test/interactions/windows.spec.js#L75" >}}
251-
{{< /tab >}}
252-
{{< tab header="Kotlin" >}}
253-
// Opens a new tab and switches to new tab
254-
driver.switchTo().newWindow(WindowType.TAB)
255-
256-
// Opens a new window and switches to new window
257-
driver.switchTo().newWindow(WindowType.WINDOW)
258-
{{< /tab >}}
259-
{{< /tabpane >}}
260-
261184
### Closing a window or tab
262185

263186
When you are finished with a window or tab _and_ it is not the
@@ -268,13 +191,9 @@ handle stored in a variable. Put this together and you will get:
268191

269192
{{< tabpane langEqualsHeader=true >}}
270193
{{< badge-examples >}}
271-
{{< tab header="Java" >}}
272-
//Close the tab or window
273-
driver.close();
274-
275-
//Switch back to the old tab or window
276-
driver.switchTo().window(originalWindow);
277-
{{< /tab >}}
194+
{{< tab header="Java" text=true >}}
195+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L31-L34" >}}
196+
{{< /tab >}}
278197
{{< tab header="Python" >}}
279198
#Close the tab or window
280199
driver.close()
@@ -318,14 +237,68 @@ window will leave WebDriver executing on the now closed page, and will
318237
trigger a **No Such Window Exception**. You must switch
319238
back to a valid window handle in order to continue execution.
320239

240+
### Create new window (or) new tab and switch
241+
Creates a new window (or) tab and will focus the new window or tab on screen.
242+
You don't need to switch to work with the new window (or) tab. If you have more than two windows
243+
(or) tabs opened other than the new window, you can loop over both windows or tabs that WebDriver can see,
244+
and switch to the one which is not the original.
245+
246+
__Note: This feature works with Selenium 4 and later versions.__
247+
248+
{{< tabpane langEqualsHeader=true >}}
249+
{{< badge-examples >}}
250+
{{< tab header="Java" text=true >}}
251+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L36-L42" >}}
252+
{{< /tab >}}
253+
{{< tab header="Python" >}}
254+
# Opens a new tab and switches to new tab
255+
driver.switch_to.new_window('tab')
256+
257+
# Opens a new window and switches to new window
258+
driver.switch_to.new_window('window')
259+
{{< /tab >}}
260+
{{< tab header="CSharp" >}}
261+
// Opens a new tab and switches to new tab
262+
driver.SwitchTo().NewWindow(WindowType.Tab)
263+
264+
// Opens a new window and switches to new window
265+
driver.SwitchTo().NewWindow(WindowType.Window)
266+
{{< /tab >}}
267+
{{% tab header="Ruby" text=true %}}
268+
Opens a new tab and switches to new tab:
269+
{{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L9" >}}
270+
271+
Opens a new window and switches to new window:
272+
{{< gh-codeblock path="/examples/ruby/spec/interactions/windows_spec.rb#L15" >}}
273+
{{% /tab %}}
274+
{{< tab header="JavaScript" text=true >}}
275+
Opens a new tab and switches to new tab
276+
{{< gh-codeblock path="examples/javascript/test/interactions/windows.spec.js#L70" >}}
277+
278+
Opens a new window and switches to new window:
279+
{{< gh-codeblock path="examples/javascript/test/interactions/windows.spec.js#L75" >}}
280+
{{< /tab >}}
281+
{{< tab header="Kotlin" >}}
282+
// Opens a new tab and switches to new tab
283+
driver.switchTo().newWindow(WindowType.TAB)
284+
285+
// Opens a new window and switches to new window
286+
driver.switchTo().newWindow(WindowType.WINDOW)
287+
{{< /tab >}}
288+
{{< /tabpane >}}
289+
290+
291+
321292
### Quitting the browser at the end of a session
322293

323294
When you are finished with the browser session you should call quit,
324295
instead of close:
325296

326297
{{< tabpane langEqualsHeader=true >}}
327298
{{< badge-examples >}}
328-
{{< tab header="Java" >}}driver.quit();{{< /tab >}}
299+
{{< tab header="Java" text=true >}}
300+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/interactions/WindowsTest.java#L44-L45" >}}
301+
{{< /tab >}}
329302
{{< tab header="Python" >}}driver.quit(){{< /tab >}}
330303
{{< tab header="CSharp" >}}driver.Quit();{{< /tab >}}
331304
{{< tab header="Ruby" >}}driver.quit{{< /tab >}}

0 commit comments

Comments
 (0)