Multiple Windows:
Multiple window handling in selenium webdriver is done by using "getWindowHandle()" is for the parent window and "getWindowHandles()" is for multiple child window handling.We have to switch window from parent (or) main window to child window.
- Below i have given the html sample code also for the below program.
- save that html code by .html extension and pass that path in driver get method.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class multiplewindow {
@Test
public void mulwindows() throws InterruptedException
{
WebDriver driver=new FirefoxDriver();
driver.get("H:\\websites.html");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
System.out.println("Parent window title is:"+driver.getTitle());
//get window handle method is used and stored in variable for further refernces.
String parent_window=driver.getWindowHandle();
driver.findElement(By.linkText("google")).click();
//Gets all the links present in main window and its return type is Set of strings.
Set<String> allwindows=driver.getWindowHandles();
for(String child_window:allwindows)
{
if(!child_window.equals(parent_window))
{
//Switching from parent window to child window
driver.switchTo().window(child_window);
System.out.println("Title is:"+driver.getTitle());
driver.findElement(By.className("gsfi")).sendKeys("selenium");
}
}
driver.close();
//Switching back to parent window.
driver.switchTo().window(parent_window);
Thread.sleep(3000);
driver.quit();
}
}
Output:
Parent window title is: mulwindow
Title is:Google
PASSED: mulwindows
HTML Code:
<!DOCTYPE html>
<html>
<body>
<title>mulwindow</title>
<p><a href="http://www.google.com" target="_blank">google</a></p>
<p><a href="http://www.flipkart.com"target="_blank">flipkart</a></p>
</body>
</html>
No comments:
Post a Comment