Sunday, May 24, 2015

Lesson #13 - Selenium WebDriver – Window Handles with C#


In this article I want to review the WebDriver “Handling” scenarios that will help you to control any situation where you need to work with any other window that different from the default web page that you opened (popups, New Tab Etc.).

We will start with a few basic facts that you need to know before reading this post, so please read it before you continue to my practical examples  J.

Fact No’1: 

When you start to write your selenium code, one of the mandatory operations that you need to do is to create the first IwebDriver instance, Example: 

IWebDriver FirefoxInstance = new FirefoxDriver ();

Fact No’2: 

When you create this instance, the selenium framework identifies this Window with a unique identifier (In our case the window that attached to “FirefoxInstance”).

Fact No’3: 

The unique identifier called “Window Handler” and created as “String” variable (Therefore he can host any keyboard char), this handler is relevant to the browser Tabs only.

Fact no’4:

When you work with multiple windows, the selenium framework will use the Handler to identify and access each window that available during the code execution.


How can we use this handler in our code?

The first thing that we need to do before we can answer this question, is to understand the code that available for us to control; this handler, in C#, there are two basic “Properties” that we can use:


Window Handler

We will use this property to identify the unique ID of the current window.


Window Handlers

We will use this property to identify the unique ID of all the available open windows.

public void WindowHandle()
         IWebDriver FirefoxInstance = new FirefoxDriver();
         String DefaultWindow = FirefoxInstance.CurrentWindowHandle;       

Result: Handler ID is {49e0850a-0c5b-4b49-8812-f717a0f4e877}”.


This code will demonstrate how to open a new window and return back to the parent window using a simple “Handle” property.


public void WindowHandleS()


IWebElement WebElement;        
Dictionary<string, string> Handlers = new Dictionary<string, string>();
IWebDriver FirefoxInstance = new FirefoxDriver();


WebElement = FirefoxInstance.FindElement(By.Name("Email"));
WebElement.SendKeys("Your E-Mail Address");
WebElement = FirefoxInstance.FindElement(By.Name("Passwd"));
WebElement.SendKeys("Your Password");
WebElement = FirefoxInstance.FindElement(By.Name("signIn"));

Set the First Handler: 


WebElement = FirefoxInstance.FindElement(By.Id("blog-tab-pageelements"));

Switching to the new Frame: 


Click on link “Add a Gadget”:

WebElement = FirefoxInstance.FindElement(By.XPath("//a[@href='//']"));

Add the new window Handler: 

Handlers.Add("AddGadget_Window", FirefoxInstance.CurrentWindowHandle);


Return to main handler :

Validate that the root Handler of my site is available, after this validation, we will return to the main window and perform a simple navigation.

if (Handlers.ContainsKey("Main_Window"))
             string WindowToAccess = Handlers["Main_Window"];            
WebElement = FirefoxInstance.FindElement(By.XPath("//span[@id='blog-tab-overview']"));


No comments:

Post a Comment

My Presentations