Reputation: 1167
Scenario: I have configured Grid 2 and multiple tests are now running in parallel. When test starts there is opened browser window (only one tab opened) and some controls filled inside it. After that I open another tab (in the same browser window), switch to it and fill some controls inside it.
Before filling data inside second tab there needs to be done following steps:
1. Open new tab by calling SendKeys(Keys.Ctrl + 't')
2. Before switching to second tab wait for that second tab's handle to be added to driver instance.
3. If handle added to driver instance then switch to it, else 4.
4. Repeat operation 2. and 3. until timeout reached.
Problem:
When debugging I noticed that when opening a new tab, it's handle was not added to driver.WindowHandles
. That means, if not checking if handle added and trying to switch to it, the exception will be thrown. In my case it would switch to incorrect tab as I'm calling driver.SwitchTo().Window(handles[handles.Count() -1]);
. So I created method that waits for handle to be added.
The problem is that, when running in multiple workers, it always times out. I have changed the timeout
but nothing changes. The newly opened tab's handle is not added to WindowHandles
. If I'm not running in parallel, then it works as expected.
// previousTabCount- browser's tab count before opening new one
public void WaitForTabToOpenAndSwtich(int previousTabCount)
{
int currentTabCount = driver.WindowHandles.Count();
int count = 0;
while(currentTabCount == previousTabCount)
{
// after 20 seconds throw exception
if(count > 20)
throw new Exception("The newly opened tab's handle was not added.");
// update current tab count
currentTabCount = driver.WindowHandles.Count();
count++;
Thread.Sleep(1000);
}
var handles = driver.WindowHandles;
driver.SwitchTo().Window(handles[handles.Count() -1]);
}
Upvotes: 0
Views: 145
Reputation: 1167
I found the solution. The problem was that when using SendKeys(Keys.Ctrl + 't')
to open new tab on remote machine it did not work I'm not sure why. Fortunately I found an alternative approach.
Instead of using that send keys command I used:
// script that opens a new tab
driver.ExecuteScript("var w = window.open(); w.document.open();");
After running this script the new tab was opened but I could not change its url by using driver.Navigate().GoToUrl("...");
and exception was thrown:
The HTTP request to the remote WebDriver server for URL 'http: //example.com' timed out after 60 seconds
So I modified that script a bit by adding a page load at the end like this:
// script that opens new tab and reloads it
driver.ExecuteScript("var w = window.open(); w.document.open(); w.location.reload();");
This worked for me. Maybe someone will find this useful.
Upvotes: 0