0

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

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Any idea how to switch focus between windows generated by different driver? Thanks
Thanks @Karna I had successfully did that, thanks. But now I would like to bring the window to the front based on actions done by respective webdriver. For example, like if chrome is doing action, then the focus of window will be on Chrome browser, and the same for FF. Thanks
1

Just use two driver instancess:

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

You can make them both same flavour if you want.

2 Comments

Thanks! I am now able to run parallel tests. However, any idea on how to switch focus between those windows? Thanks :)
I think you are very confused! Parallel tests is something different than multiple browser tests. I do not understand what you mean by "switch focus". You can use driver1.findElement(...) to access items in the first window, and similarly driver2.findElement(...) to find items in the second window.
0

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); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.