Thursday, 3 October 2013

Handling Multiple Windows in WebDriver

Dealing with multiple windows in Automation Testing has always been a little tricky and require an extra effort.
Before commencing, let us first consider a few situations when we are likely to deal with multiple windows.
  • Filling forms may require to select the date from a separately opened window.
  • Clicking on some link/button can kick-off yet another window.
  • Handling Advertisement windows
Hence, we can come up with various scenarios depending upon the application.
Now let us motion ourselves towards the challenge we face under above situations. The most particular of all is switching the focus from one window to another. Let us understand the same in the following way:
1Comprehending from the above figure, the entire process can be fundamentally segregated into following steps:
Step 1 : Clicking on Link1 on Window A
A new Window B is opened.
Step 2 : Move Focus from Window A to Window B
Window B is active now
Step 3 : Perform Actions on Window B
Complete the entire set of Actions
Step 4 : Move Focus from Window B to Window A
Window A is active now
These are the steps which we can easily interpret out of the diagram, but there are a few more steps to add to complete this process and making our script execute. These steps don’t have visibility but plays a very vital role. Let us now re-consider the same scenario.
Step 1 : Clicking on Link1 on Window A
A new Window B is opened.
Step 2 : Save reference for Window A
Step 3 : Create reference for Window B
Step 3 : Move Focus from Window A to Window B
Window B is active now
Step 3 : Perform Actions on Window B
Complete the entire set of Actions
Step 4 : Move Focus from Window B to Window A
Window A is active now
Let us understand the same with a small coding example.
 public class MultiWindowHandle {  
 WebDriver driver;  
 @Before  
 public void setup() throws Exception {  
 driver=new FirefoxDriver();  
 String URL="https://www.abc.co.in/";   
 driver.get(URL);  
 driver.manage().window().maximize();  
 }  
 @Test  
 public void test() throws Exception {   
 // Opening Calender  
 driver.findElement(By.xpath("//img[@alt='Calender']")).click();  
 // Storing parent window reference into a String Variable  
 String Parent_Window = driver.getWindowHandle();    
  // Switching from parent window to child window   
 for (String Child_Window : driver.getWindowHandles())  
 {  
 driver.switchTo().window(Child_Window);  
 // Performing actions on child window  
 driver.findElement(By.id("calendar_month_txt")).click();  
 List  Months=driver.findElements(By.xpath("//div[@id='monthDropDown']//div"));  
 int Months_Size=Months.size();  
 System.out.println("Month size is:"+Months_Size);  
 Months.get(1).click();  
 driver.findElement(By.xpath("//*[@id='calendarDiv']/div/table/tbody/tr/td[contains(text(),'16')]")).click();  
 }  
 //Switching back to Parent Window  
 driver.switchTo().window(Parent_Window);  
 //Performing some actions on Parent Window  
 driver.findElement(By.className("btn_style")).click();  
 }  
  @After  
  public void close() {  
  driver.quit();  
  }   
 }  

No comments:

Post a Comment

Angular JS Protractor Installation process - Tutorial Part 1

                     Protractor, formally known as E2E testing framework, is an open source functional automation framework designed spe...