Skip to main content

Switch between browsers\windows and pop ups

These commands can be used to switch between windows, iterate through different windows opened by the webdriver, handle pop ups and frames in windows. 

GetWindowHandle Command

Purpose: To get the window handle of the current window.
driver.getWindowHandle(); Returns an alphanumeric string

GetWindowHandles Command

Purpose: To get the window handle of all the current windows.
Set<String> handle=Driver.getWindowHandles(); Returns a set of window handles

SwitchTo Window Command

Purpose: WebDriver supports moving between named windows using the “switchTo” method.
For(String handle:driver.getWindowHandles())
Driver.switchto().window(handle);
Or
Driver.switchto().window(windowname);
<A href="newwindow.html" target="windowname">click here for a new window </A>

SwitchTo Frame Command

Purpose: WebDriver supports moving between frames using the “switchTo” method.
Driver.switchto().frame(“framename”);
Driver.switchto().frame(0);
Driver.switchto().frame(previously found element using driver);
All these are evaluated from the current frame. You can go to the sub frames from the current frame. You can go back to the default top frame using the driver.switchTo().defaultContent();

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();
Alert.getText();//gets the text of the alert box
Alert.accept();//clicks on the accept button
Alert.dismiss();//clicks on the cancel button
Alert.sendkeys("yes"); // sends keys strokes
Alert.authenticateusing(credentials);//Used for authentication like proxy server

Credentials credentials=new UserAndPassword("admin","password");


Example program:
 String parentWindowHandle = browser.getWindowHandle(); // save the current window handle.
      WebDriver popup = null;
      Iterator<String> windowIterator = browser.getWindowHandles();
      while(windowIterator.hasNext()) { 
        String windowHandle = windowIterator.next(); 
        popup = browser.switchTo().window(windowHandle);
        if (popup.getTitle().equals("Google") {
          break;
        }
      }
Finds a window pop up with title google.
popup.close() would close the pop up. 

Comments

Popular posts from this blog

How to Install Selenium and execute your first program

You can install and test Selenium using the following steps. All files can be downloaded from  http://www.seleniumhq.org/download/   Download Selenium Server jar file   Download client library java jar file   Download InternetExplorer Driver file   Install eclipse workspace. (download eclipse. Unzip folder. Find eclipse.exe application and open it) Create a new java project with 1,2 as references Copy 3 to workspace For IE, go to internet options->security and change the security level so that it’s the same for all zones. Write sample java program Below program opens google in IE and searches for selenium test and closes it. // All the imports happen automatically when you use eclipse ide and add the downloaded jars in the project references package test; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.ie...

Form - User Input Automation

Text boxes WebElement t=driver.findElement(By.id(“username”)); t.sendkeys(“test name”); Buttons WebElement t=driver.findElement(By.id(“createbutton”)); t.click(); DropDown Boxes  WebElement select = driver.findElement(By.tagName("select")); List<WebElement> allOptions = select.findElements(By.tagName("option")); for (WebElement option : allOptions) { System.out.println(String.format("Value is: %s", option.getAttribute("value"))); option.click(); } This will find the first “SELECT” element on the page, and cycle through each of its OPTIONs in turn, printing out their values, and selecting each in turn. As you will notice, this isn’t the most efficient way of dealing with SELECT elements. WebDriver’s support classes include one called “Select”, which provides useful methods for interacting with these Select oSelection = new Select(driver.findElement(By.tagName("select"))); oSelection.deselectAll(); //All op...