Skip to content

Commit d9fe0c0

Browse files
committed
Implementing SwitchTo().Window() by name for W3C compliant implementations in .NET
1 parent 3244bf4 commit d9fe0c0

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

dotnet/src/webdriver/Remote/RemoteTargetLocator.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,41 @@ public IWebDriver ParentFrame()
126126
/// <summary>
127127
/// Change to the Window by passing in the name
128128
/// </summary>
129-
/// <param name="windowName">name of the window that you wish to move to</param>
129+
/// <param name="windowHandleOrName">Window handle or name of the window that you wish to move to</param>
130130
/// <returns>A WebDriver instance that is currently in use</returns>
131-
public IWebDriver Window(string windowName)
131+
public IWebDriver Window(string windowHandleOrName)
132132
{
133133
Dictionary<string, object> parameters = new Dictionary<string, object>();
134-
parameters.Add("name", windowName);
134+
if (this.driver.IsSpecificationCompliant)
135+
{
136+
parameters.Add("handle", windowHandleOrName);
137+
try
138+
{
139+
this.driver.InternalExecute(DriverCommand.SwitchToWindow, parameters);
140+
return this.driver;
141+
}
142+
catch (NoSuchWindowException e)
143+
{
144+
// simulate search by name
145+
string original = this.driver.CurrentWindowHandle;
146+
foreach (string handle in driver.WindowHandles)
147+
{
148+
this.Window(handle);
149+
if (windowHandleOrName == this.driver.ExecuteScript("return window.name").ToString())
150+
{
151+
return this.driver; // found by name
152+
}
153+
}
154+
155+
this.Window(original);
156+
throw e;
157+
}
158+
}
159+
else
160+
{
161+
parameters.Add("name", windowHandleOrName);
162+
}
163+
135164
this.driver.InternalExecute(DriverCommand.SwitchToWindow, parameters);
136165
return this.driver;
137166
}

0 commit comments

Comments
 (0)