Skip to content

Commit

Permalink
Implementing SwitchTo().Window() by name for W3C compliant implementa…
Browse files Browse the repository at this point in the history
…tions in .NET
  • Loading branch information
jimevans committed Oct 8, 2015
1 parent 3244bf4 commit d9fe0c0
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions dotnet/src/webdriver/Remote/RemoteTargetLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,41 @@ public IWebDriver ParentFrame()
/// <summary>
/// Change to the Window by passing in the name
/// </summary>
/// <param name="windowName">name of the window that you wish to move to</param>
/// <param name="windowHandleOrName">Window handle or name of the window that you wish to move to</param>
/// <returns>A WebDriver instance that is currently in use</returns>
public IWebDriver Window(string windowName)
public IWebDriver Window(string windowHandleOrName)
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("name", windowName);
if (this.driver.IsSpecificationCompliant)
{
parameters.Add("handle", windowHandleOrName);
try
{
this.driver.InternalExecute(DriverCommand.SwitchToWindow, parameters);
return this.driver;
}
catch (NoSuchWindowException e)
{
// simulate search by name
string original = this.driver.CurrentWindowHandle;
foreach (string handle in driver.WindowHandles)
{
this.Window(handle);
if (windowHandleOrName == this.driver.ExecuteScript("return window.name").ToString())
{
return this.driver; // found by name
}
}

this.Window(original);
throw e;
}
}
else
{
parameters.Add("name", windowHandleOrName);
}

this.driver.InternalExecute(DriverCommand.SwitchToWindow, parameters);
return this.driver;
}
Expand Down

0 comments on commit d9fe0c0

Please sign in to comment.