Multiple windows using Selenium Java
- Window A has a link "Link1" and we need to click on the link (click event).Window B displays and we perform some actions.
- 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 : 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 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
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();
}
}
0 Comments