Reputation: 1556
I have multiple Selenium tests that I run in parallel. How do I "activate" the different browser windows (i.e. simulating a user click in the taskbar, making the window active)?
By default, Selenium's first browser instance is active and covers my other browser windows.
I've looked in to the other SO threads (java), and if I understand them correctly, I should keep track of all the webdriver instances "current window handles", and perform a switch in the following way:
activeWebDriver.SwitchTo().Window(someInactiveWindowHandle);
Is this correct? If so, how would I know which of my webdriver instances that are currently on top, so I know what to switch from?
Upvotes: 0
Views: 2232
Reputation: 50809
You can keep track on the windows using driver.CurrentWindowHandle
. This is a string that looks like
"CDwindow-3E960650-3D6F-4083-939A-5887B46A5E23"
And used to identify the current driver active window.
string currentHandler = driver.CurrentWindowHandle;
// switch to new window
foreach (string handle in driver.WindowHandles)
{
if (!handle.Equals(currentHandler))
{
driver.SwitchTo().Window(handle);
}
}
//do something on new window
// switch back to old window
driver.Close();
driver.SwitchTo().Window(currentHandler);
Usage example
Dictionary<string, IWebDriver> driversByHandles;
IWebDriver driver1;
IWebDriver driver2;
IWebDriver driver3;
[SetUp]
public void SetUp()
{
driversByHandles = new Dictionary<string, IWebDriver>();
driver1.Navigate().GoToUrl(Url);
driversByHandles.Add(driver1.CurrentWindowHandle, driver1);
driver2.Navigate().GoToUrl(Url);
driversByHandles.Add(driver2.CurrentWindowHandle, driver2);
...
}
[Test]
public void Test()
{
foreach(KeyValuePair<string, IWebDriver> entry in driversByHandles)
{
if (entry.Key.equals(targetHandle))
{
entry.Value.SwitchTo().Window(entry.Key);
}
}
}
Upvotes: 2