Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

Multiple windows using Selenium Java

Multiple windows using Selenium Java

  1. Window A has a link "Link1" and we need to click on the link (click event).Window B displays and we perform some actions. 
  2. The entire process can be fundamentally segregated into following steps:
  3. Step 1 : Clicking on Link1 on Window A
  4. A new Window B is opened.
  5. Step 2 : Save reference for Window A
  6. Step 3 : Create reference for Window B
  7. Step 3 : Move Focus from Window A to Window B
  8. Window B is active now
  9. Step 3 : Perform Actions on Window B
  10. Complete the entire set of Actions
  11. Step 4 : Move Focus from Window B to Window A
  12. Window A is active now
  13. Let us understand the same coding example.

package com.example;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class MultipleWindowsHandles {
WebDriver driver;
public void launchingbrowser(){ driver=new FirefoxDriver();
driver.get("http://www.infimum.dk/HTML/JSwindows.html#ref_3_1");
driver.manage().window().maximize();
}
public void windowhandles(){
//window1
// Storing parent window reference into a String Variable  
     String Parent_Window = driver.getWindowHandle();
     // clikcon window 1 and which can open 
     driver.findElement(By.xpath("//div[@class='example']/input")).click();
Set listofwindow=driver.getWindowHandles();
System.out.println("list of window size;;;"+listofwindow.size());
  for (String Child_Window : listofwindow)  
     {     // switch child window
     driver.switchTo().window(Child_Window); 
     }
driver.manage().window().maximize(); // maximize child window
  // closing child window
driver.close();
//inserct driver switch back parent
driver.switchTo().window(Parent_Window);
// another action on parent window ..which .. again child window
driver.findElement(By.xpath("//div[@class='example']/input")).click();

}
public static void main(String[] args) {
MultipleWindowsHandles mw=new MultipleWindowsHandles();
mw.launchingbrowser();
mw.windowhandles();
}
}

Post a Comment

0 Comments