Saturday, September 14, 2019

Post#59.Handling Window or Tab in Selenium


Switching among windows or tabs
There will be times when you will require performing some testing, where the testing operations open a new browser/tab, test case may require you to perform some tasks on the newly opened browser window/tab and return back to original window to perform the remaining tasks.

Even if the window/tab is currently on focus but still it is not an active window, so to perform some tasks you need to switch to new browser window/tab in WebDriver.


Commands will have effect on base window unless we switch our control to new window/tab.


Selenium WebDriver assigns an alphanumeric id to each window as soon as the WebDriver object is instantiated. This unique alphanumeric id is called windowhandle. 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.
So Selenium has following methods to get window handles.
String parent=driver.getWindowHandle();
// It will return the current window name as a String
Set<String> names=driver.getWindowHandles();

// This will return the number of windows opened by WebDriver and will return Set of Strings. Once we got the set of window names then we can iterate using Iterator.
The set is part of Java collection which allows us to handle multiple sets of data dynamically.

Consider the following scenario:
Window 1 has link “Click Here for Signup Form” and we need to click on the link.
As soon as we click on the link Window 2 (Signup Form) displays in different window and we perform some actions on Signup Form.


If you click on the link “Click Here for Signup Form”, The Signup Form page (as shown in below image) will open in a new window.


Program to handle above Scenario:
package pack3;
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 WindowHandle {

public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.testingbar.com/multiple-window/");

driver.findElement(By.xpath("//span[contains(text(),'Click Here')]")).click();

Set<String> sets = driver.getWindowHandles();
Iterator<String> win1 =sets.iterator();
String Parentwin = win1.next();//get the parent window ID
String childwin = win1.next();//get the Child window ID

driver.switchTo().window(childwin); //Switched to Child window
Thread.sleep(10000);
//Perform some action on Child window
driver.findElement(By.xpath("//input[@name='email']")).sendKeys("Sujoy@testingbar.com");

driver.switchTo().window(Parentwin); // switch back to the original window
}
}

Scenario: If we don’t know how many numbers of window will be appearing after clicking on a particular link then you can use try-catch block to handle such situation.

Please refer the below example.

package pack2;
import java.util.List;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Program1 {

            public static void main(String[] args) {
                       
                        FirefoxDriver driver = new FirefoxDriver();
                        driver.manage().window().maximize();
                        driver.get("http://www.bing.com/");
                        //To get the window handle of the current window
                        String parent=driver.getWindowHandle();
                        driver.findElement(By.linkText("Office Online")).click();
                        //To get the window handle of all the current windows.
                        Set<String> winId=driver.getWindowHandles();
                        for(String id : winId)
                        {
                                    driver.switchTo().window(id);
            // WebDriver supports moving between named windows using the “switchTo” method.
                                    try
                                    {
                                                driver.findElement(By.linkText("Privacy at Microsoft")).click();
                                                break;
                                    }
                                    catch(Exception e){
                                                driver.switchTo().window(parent);
                                                System.out.println(e);
                                    }
                        }

            }

}


No comments:

Post a Comment