How to switch the Browser windows using different driver instances.

In the below program we have created two browser instances i.e driver and driver1.
with one driver instance we are opening one URL and with second driver instance other URL.
Below code shows how we can switch the browser windows using different driver instances.

 import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
public class FrameWorkBase {
// 'driver' and 'driver1' are two instances created.
public static WebDriver driver,driver1;
public static WebDriverWait wait;
public static String Parentwindow;
public static void main (String []args) throws Exception
{
driver= new FirefoxDriver();
driver.manage().window().maximize();
wait=new WebDriverWait(driver, 10);
driver.get("http://www.yahoo.com");
System.out.println(driver.getTitle());
Parentwindow = driver.getWindowHandle();
for ( String currentwindow : driver.getWindowHandles())
driver.switchTo( ).window(currentwindow);
{
driver1=new FirefoxDriver();
driver1.manage().window().maximize();
driver1.get("https://translate.google.co.in/");
System.out.println(driver1.getTitle());
driver.switchTo().window(Parentwindow);
}
System.out.println(driver.getTitle());
}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s