Raymond
Raymond

Reputation: 614

Multiple Browser WebDriver Selenium

I am working on a Selenium test project where I need to launch two browsers at initial setup .

Then I need to do switching between these browsers.

So I will have [Window1] [Window2]

I would like to run test through [Window1] and then switch to [Window2] to check result of actions done in [Window1]

Any idea on how to do it?

I tried driver.switchTo().window() but no luck.

Any help would be greatly appreciated. Thanks

Upvotes: 0

Views: 971

Answers (3)

Sambit
Sambit

Reputation: 1

You need to pass the parameter as window name or you can get all the window handles and then switch to the particular window handle.

You could use:

driver.switchTo().window("windowName");

or:

for (String handle : driver.getWindowHandles()) {
    driver.switchTo().window(handle);
}

Upvotes: 0

Ajinkya
Ajinkya

Reputation: 22710

driver.switchTo().window() will work only if new window is opened by any action in existing window. If you are using different drivers to open different windows then it wont work.
In such case you need to choose appropriate instance of driver to control the new window.

Suppose you have instance of webdriver

// Window 1
WebDriver chrome = new ChromeDriver()

// Window 2
WebDriver firefox = new FirefoxDriver()

Now use chrome whenever you want to interact with Window 1 and use firefox to interact with Window 2.

Upvotes: 3

SiKing
SiKing

Reputation: 10329

Just use two driver instancess:

WebDriver driver1 = new ChromeDriver()
WebDriver driver2 = new FirefoxDriver()

You can make them both same flavour if you want.

Upvotes: 1

Related Questions