Selenium WebDriver Switch Window Commands
Some web applications have many frames or multiple windows. Selenium WebDriver assigns an alphanumeric id to each window as soon as the WebDriver object is instantiated. This unique alphanumeric id is called window handle. Selenium uses this unique id to switch control among several windows. In simple terms, each unique window has a unique ID, so that Selenium can differentiate when it is switching controls from one window to the other.
GetWindowHandle Command
Purpose: To get the window handle of the current window.
GetWindowHandles Command
Purpose: To get the window handle of all the current windows.
SwitchTo Window Command
Purpose: WebDriver supports moving between named windows using the “switchTo” method.
Or
Alternatively, you can pass a “window handle” to the “switchTo().window()” method. Knowing this, it’s possible to iterate over every open window like so:
Or
Switching between windows with Iterators:
SwitchTo Frame Command
Purpose: WebDriver supports moving between named frames using the “switchTo” method.
SwitchTo PopUp Command
Purpose: WebDriver supports moving between named PopUps using the “switchTo” method. After you’ve triggered an action that opens a popup, you can access the alert and it will return the currently open alert object. With this object you can now accept, dismiss, read its contents or even type into a prompt. This interface works equally well on alerts, confirms, and prompts.
Practice Exercise 1
1) Launch new Browser
2) Open URL “http://toolsqa.com/automation-practice-switch-windows/”
3) Get Window name (Use GetWindowHandle command)
4) Click on Button “New Message Window”, it will open a Pop Up Window
5) Get all the Windows name ( Use GetWindowHandles command)
6) Close the Pop Up Window (Use Switch Command to shift window)
Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
package
practiceTestCases
;
import
java
.
util
.
Set
;
import
java
.
util
.
concurrent
.
TimeUnit
;
import
org
.
openqa
.
selenium
.
By
;
import
org
.
openqa
.
selenium
.
WebDriver
;
import
org
.
openqa
.
selenium
.
firefox
.
FirefoxDriver
;
public
class
PracticeSwitchWindow
{
public
static
WebDriver
driver
;
public
static
void
main
(
String
[
]
args
)
{
// Create a new instance of the Firefox driver
driver
=
new
FirefoxDriver
(
)
;
// Put an Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception
driver
.
manage
(
)
.
timeouts
(
)
.
implicitlyWait
(
10
,
TimeUnit
.
SECONDS
)
;
// Launch the URL
driver
.
get
(
"http://toolsqa.com/automation-practice-switch-windows/"
)
;
// Store and Print the name of the First window on the console
String
handle
=
driver
.
getWindowHandle
(
)
;
System
.
out
.
println
(
handle
)
;
// Click on the Button "New Message Window"
driver
.
findElement
(
By
.
name
(
"New Message Window"
)
)
.
click
(
)
;
// Store and Print the name of all the windows open
Set
handles
=
driver
.
getWindowHandles
(
)
;
System
.
out
.
println
(
handles
)
;
// Pass a window handle to the other window
for
(
String
handle1
:
driver
.
getWindowHandles
(
)
)
{
System
.
out
.
println
(
handle1
)
;
driver
.
switchTo
(
)
.
window
(
handle1
)
;
}
// Closing Pop Up window
driver
.
close
(
)
;
// Close Original window
driver
.
quit
(
)
;
}
}
|
Practice Exercise 2
1) Launch new Browser
2) Open URL “http://toolsqa.com/automation-practice-switch-windows/”
3) Click on Button “Alert Box”, it will open a Pop Up Window generated by JavaScript
4) Switch to Alert window (Use ‘SwitchTo()Alert() command)
5) Close the Pop Up Window (Use Accept command)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
package
practiceTestCases
;
import
java
.
util
.
Set
;
import
java
.
util
.
concurrent
.
TimeUnit
;
import
org
.
openqa
.
selenium
.
Alert
;
import
org
.
openqa
.
selenium
.
By
;
import
org
.
openqa
.
selenium
.
WebDriver
;
import
org
.
openqa
.
selenium
.
firefox
.
FirefoxDriver
;
public
class
PracticeSwitchWindow
{
public
static
WebDriver
driver
;
public
static
void
main
(
String
[
]
args
)
{
// Create a new instance of the Firefox driver
driver
=
new
FirefoxDriver
(
)
;
// Put an Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception
driver
.
manage
(
)
.
timeouts
(
)
.
implicitlyWait
(
10
,
TimeUnit
.
SECONDS
)
;
// Launch the URL
driver
.
get
(
"http://toolsqa.com/automation-practice-switch-windows/"
)
;
// Click on the Button "Alert Box"
driver
.
findElement
(
By
.
name
(
"Alert Box"
)
)
.
click
(
)
;
// Switch to JavaScript Alert window
Alert
myAlert
=
driver
.
switchTo
(
)
.
alert
(
)
;
// Accept the Alert
myAlert
.
accept
(
)
;
// Close Original window
driver
.
close
(
)
;
}
}
|