About Me

Page views

Powered by Blogger.

Followers

Sunday 3 May 2015

So how do you automate switching between windows in Selenium?

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. 

So Selenium has following methods to get window handles.

1. GetWindowHandle Command

String  handle= driver.getWindowHandle();

{Return a string of alphanumeric window handle}

2. GetWindowHandles Command

Set<String> handle= driver.getWindowHandles();

{Return a set of window handle}

3. SwitchTo Window Command

driver.switchTo().window("windowName");

Or

driver.switchTo().window(handle);}

4. SwitchTo Frame Command

driver.switchTo().frame("frameName");

5. SwitchTo PopUp Command

Alert alert = driver.switchTo().alert();

{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 Yourself :

 

 package dayOne;


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 SwitchWindow {

    public static WebDriver driver;
   
public static void main(String[] args) {

        driver = new FirefoxDriver();

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        driver.get("http://www.toolsqa.com/automation-practice-switch-windows/");

        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();

}

}

 

0 comments:

Post a Comment

Popular Posts

Copyright © Learn Selenium Yourself | Powered by Blogger
Design by Duan Zhiyan | Blogger Theme by NewBloggerThemes.com