package selenium;
import java.util.Set;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class MultipleBrowser {
WebDriver driver;
@Test
public void multipleBrowsers() throws InterruptedException {
driver=new FirefoxDriver();
driver.get("https://www.google.co.in/");
System.out.println(driver.getTitle());
//Get a list window handles which are currently opened (the list will contain more than one handle if there are any child windows (popups) opened from parent window, otherwise it will contain only 1 set)
Set<String> listofallfirstbrowserrelatedwindows = driver.getWindowHandles();
System.out.println(listofallfirstbrowserrelatedwindows);
//Get the Parent window handle for the firstbrowser window and assgine to firstbrowser
String firstbrowser = driver.getWindowHandle();
System.out.println(driver.getTitle());
//open a new window using "executeScript"JavaScript but under same driver umbrella (Means same driver will be able to communicate with this browser also as it is opend under same driver instance)
((JavascriptExecutor)driver).executeScript("window.open();");
//Get latest list of the currently open windows (this is supposed to return all the browser handles which are openend under same driver instance (in our case it will be 2))
Set<String> listofallsecondbrowserrelatedwindows = driver.getWindowHandles();
System.out.println(listofallsecondbrowserrelatedwindows);
// Remove all the browser handle which were openend prior to above second window (in this case, only first browser handle will be removed and second browser handle will remain)
listofallsecondbrowserrelatedwindows.removeAll(listofallfirstbrowserrelatedwindows);
System.out.println(listofallsecondbrowserrelatedwindows);
// Assign the above second window handle to a string
String secondbrowser = ((String)listofallsecondbrowserrelatedwindows.toArray()[0]);
System.out.println(secondbrowser);
// NOTE : NOW YOU HAVE TWO SEPERATE HANDLE IN TWO STRINGS (firstbrowser, secondbrowser), WHICH YOU CAN USE with drive to switch between both browsers.
// switch to secondbrowser (i.e. second window)
driver.switchTo().window(secondbrowser);
driver.get("https://in.yahoo.com/");
Thread.sleep(2000);
// switch to firstbrowser (i.e. first window)
driver.switchTo().window(firstbrowser);
driver.manage().window().maximize();
Thread.sleep(2000);
// switch to secondbrowser (i.e. second window)
driver.switchTo().window(secondbrowser);
}
}
import java.util.Set;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class MultipleBrowser {
WebDriver driver;
@Test
public void multipleBrowsers() throws InterruptedException {
driver=new FirefoxDriver();
driver.get("https://www.google.co.in/");
System.out.println(driver.getTitle());
//Get a list window handles which are currently opened (the list will contain more than one handle if there are any child windows (popups) opened from parent window, otherwise it will contain only 1 set)
Set<String> listofallfirstbrowserrelatedwindows = driver.getWindowHandles();
System.out.println(listofallfirstbrowserrelatedwindows);
//Get the Parent window handle for the firstbrowser window and assgine to firstbrowser
String firstbrowser = driver.getWindowHandle();
System.out.println(driver.getTitle());
//open a new window using "executeScript"JavaScript but under same driver umbrella (Means same driver will be able to communicate with this browser also as it is opend under same driver instance)
((JavascriptExecutor)driver).executeScript("window.open();");
//Get latest list of the currently open windows (this is supposed to return all the browser handles which are openend under same driver instance (in our case it will be 2))
Set<String> listofallsecondbrowserrelatedwindows = driver.getWindowHandles();
System.out.println(listofallsecondbrowserrelatedwindows);
// Remove all the browser handle which were openend prior to above second window (in this case, only first browser handle will be removed and second browser handle will remain)
listofallsecondbrowserrelatedwindows.removeAll(listofallfirstbrowserrelatedwindows);
System.out.println(listofallsecondbrowserrelatedwindows);
// Assign the above second window handle to a string
String secondbrowser = ((String)listofallsecondbrowserrelatedwindows.toArray()[0]);
System.out.println(secondbrowser);
// NOTE : NOW YOU HAVE TWO SEPERATE HANDLE IN TWO STRINGS (firstbrowser, secondbrowser), WHICH YOU CAN USE with drive to switch between both browsers.
// switch to secondbrowser (i.e. second window)
driver.switchTo().window(secondbrowser);
driver.get("https://in.yahoo.com/");
Thread.sleep(2000);
// switch to firstbrowser (i.e. first window)
driver.switchTo().window(firstbrowser);
driver.manage().window().maximize();
Thread.sleep(2000);
// switch to secondbrowser (i.e. second window)
driver.switchTo().window(secondbrowser);
}
}
No comments:
Post a Comment