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 find a WebElement

Find Element & Find Elements Method The difference between “ Find Elemen t” and “ Find Elements ” method is the first returns a WebElement object otherwise it throws an exception and the latter returns a list of WebElements, it can return an empty list if no DOM elements match the query. The “Find” methods take a locator or query object called “ By ”. “ By ” methods are listed below. By ID With this strategy, the first element with the id attribute value matching the location will be returned. If no element has a matching id attribute, a  NoSuchElementException   will be raised. This is the most efficient and preferred way to locate an element, as most of the times IDs are unique. But in some cases UI developers make it having non-unique ids on a page or auto-generating the id, in both cases it should be avoided. Example : If an element is given like this: <input id=”username”></input> WebElement we=driver.findElement(By.id(“username”));  ...

Selenium - What Why Who How?

What is Selenium? Selenium is an open source automated   testing   suite for web applications across different browsers and platforms.   It is quite similar to HP Quick Test Pro ( QTP ) only that Selenium focuses on automating web-based applications. Why use Selenium? Selenium is fast and it supports programming in multiple languages like Java, C#,Python and Ruby.  So development of automation scripts can be done in your language of preference. Selenium works well with AJAX testing. Selenium Grid supports execution of test cases in multiple platforms and multiple browsers in remote systems as well.  The support for different programming languages gave selenium the power to harness the capabilities of those programming languages .  For example, using JAVA as the programming language to code selenium scripts enables selenium to use JDBC for database access and testing.All the APIs supported in Java also become the advantage of Selenium like L...