Switching between browser tabs and windows

How to switch between browser tabs?
In your selenium test automation, you might need to work on multiple tabs on your browser. Following example illustrates how we can switch between two tabs and take required actions.

Example 1: To open new tab in browser
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");

Example 2: To switch previous tab
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");

Example 3: For navigating left to right side
Actions action= new Actions(driver);
action.keyDown(Keys.CONTROL).sendKeys(Keys.TAB).build().perform();

Example 4: For navigating right to left side
Actions action= new Actions(driver);
action.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).sendKeys(Keys.TAB).build().perform();


Switching between browser tabs and windows

Switch between browser windows:
Some web applications have multiple windows. Dealing with multiple windows in Automation Testing has always been a little tricky and require an extra effort.

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.

Few situations when we are likely to deal with multiple windows.
  • Filling forms may require to select the date from a separately opened window.
  • Clicking on some link/button can kick-off yet another window
  • Handling Advertisement window.
Now let us motion ourselves towards the challenge we face under above situations. The most particular of all is switching the focus from one window to another. Let us understand the same in the following way:
Switching between browser tabs and windows
From the above figure, the entire process can be fundamentally segregated into following steps:

Step 1 : Clicking on Link_1 on Window A. New Window B is opened.

Step 2 : Move Focus from Window A to Window B. Window B is active now.

Step 3 : Perform Actions on Window B. Complete the entire set of Actions.

Step 4 : Move Focus from Window B to Window A. Window A is active now

The above scenario is simple and pretty clear. Now let's re-consider the same scenario in more details.

Step 1 : Clicking on Link1 on Window A. New Window B is opened.

Step 2 : Save reference for Window A.

Step 3 : Create reference for Window B.

Step 4 : Move Focus from Window A to Window B. Window B is active now.

Step 5 : Complete the set of actions on Window B .

Step 6 : Move Focus from Window B to Window A. Window A is active now.

Let us understand the same in coding example.

 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
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Demo {

 public static void main(String[] args) {
  System.setProperty("webdriver.gecko.driver", "MyProject/driver/geckodriver.exe");
  WebDriver driver = new FirefoxDriver();
  driver.get("https://www.example.com/");

  // 01_Clicking on Link1 on Window A. New Window B is opened.
  driver.findElement(By.id("Link_1")).click();

  // Storing parent window reference into a String Variable
  String parent = driver.getWindowHandle();

  // Set S1 will store number of windows opened by Webdriver
  Set<String> s1 = driver.getWindowHandles();

  // Now we will iterate using Iterator
  Iterator<String> I1 = s1.iterator();

  while (I1.hasNext()) {
   // Create reference for Window B
   String child_window = I1.next();

   // Here we will compare if parent window is not equal to child window then we
   // will close

   if (!parent.equals(child_window)) {
    // Move Focus from Window A to Window B. Window B is active now
    driver.switchTo().window(child_window);

    // Complete the set of actions on Window B .
    System.out.println(driver.switchTo().window(child_window).getTitle());
   }
  }
  // Move Focus to Window A. Window A is active now
  driver.switchTo().window(parent);
  driver.close();
} }


<-- Previous || Next -->

No comments:

Post a Comment