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.
|
String handle= driver.getWindowHandle();//Return a string of alphanumeric window handle
|
GetWindowHandles Command
Purpose: To get the window handle of all the current windows.
|
Set<String> handle= driver.getWindowHandles();//Return a set of window handle
|
SwitchTo Window Command
Purpose: WebDriver supports moving between named windows using the “switchTo” method.
|
driver.switchTo().window("windowName");
|
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:
|
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);}
|
Or
Switching between windows with Iterators:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
driver.findElement(By.id(“id of the link which opens new window”)).click();
//wait till two windows are not opened
waitForNumberofWindowsToEqual(2);//this method is for wait
Set handles = driver.getWindowHandles();
firstWinHandle = driver.getWindowHandle(); handles.remove(firstWinHandle);
String winHandle=handles.iterator().next();
if (winHandle!=firstWinHandle){
//To retrieve the handle of second window, extracting the handle which does not match to first window handle
secondWinHandle=winHandle; //Storing handle of second window handle
//Switch control to new window
driver.switchTo().window(secondWinHandle);
|
SwitchTo Frame Command
Purpose: WebDriver supports moving between named frames using the “switchTo” method.
|
driver.switchTo().frame("frameName");
|
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.
|
Alert alert = driver.switchTo().alert();
|
Practice Exercise 1
1) Launch new Browser
2) Open URL “http://www.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://www.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://www.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://www.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();
}
}
|
No comments:
Post a Comment