Skip to content

Commit f5b3934

Browse files
committed
Adding support for SwitchTo().ParentFrame() in .NET bindings
This brings the .NET bindings into parity with other languages. Fixes issue #7419.
1 parent 88dcffe commit f5b3934

File tree

6 files changed

+106
-0
lines changed

6 files changed

+106
-0
lines changed

dotnet/src/support/Events/EventFiringWebDriver.cs

+20
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,26 @@ public IWebDriver Frame(IWebElement frameElement)
992992
return driver;
993993
}
994994

995+
/// <summary>
996+
/// Select the parent frame of the currently selected frame.
997+
/// </summary>
998+
/// <returns>An <see cref="IWebDriver"/> instance focused on the specified frame.</returns>
999+
public IWebDriver ParentFrame()
1000+
{
1001+
IWebDriver driver = null;
1002+
try
1003+
{
1004+
driver = this.wrappedLocator.ParentFrame();
1005+
}
1006+
catch (Exception ex)
1007+
{
1008+
this.parentDriver.OnException(new WebDriverExceptionEventArgs(this.parentDriver, ex));
1009+
throw;
1010+
}
1011+
1012+
return driver;
1013+
}
1014+
9951015
/// <summary>
9961016
/// Change to the Window by passing in the name
9971017
/// </summary>

dotnet/src/webdriver/ITargetLocator.cs

+6
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ public interface ITargetLocator
5252
/// <exception cref="StaleElementReferenceException">If the element is no longer valid.</exception>
5353
IWebDriver Frame(IWebElement frameElement);
5454

55+
/// <summary>
56+
/// Select the parent frame of the currently selected frame.
57+
/// </summary>
58+
/// <returns>An <see cref="IWebDriver"/> instance focused on the specified frame.</returns>
59+
IWebDriver ParentFrame();
60+
5561
/// <summary>
5662
/// Switches the focus of future commands for this driver to the window with the given name.
5763
/// </summary>

dotnet/src/webdriver/Remote/CommandInfoRepository.cs

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ private void InitializeCommandDictionary()
146146
this.commandDictionary.Add(DriverCommand.ExecuteAsyncScript, new CommandInfo(CommandInfo.PostCommand, "/session/{sessionId}/execute_async"));
147147
this.commandDictionary.Add(DriverCommand.Screenshot, new CommandInfo(CommandInfo.GetCommand, "/session/{sessionId}/screenshot"));
148148
this.commandDictionary.Add(DriverCommand.SwitchToFrame, new CommandInfo(CommandInfo.PostCommand, "/session/{sessionId}/frame"));
149+
this.commandDictionary.Add(DriverCommand.SwitchToParentFrame, new CommandInfo(CommandInfo.PostCommand, "/session/{sessionId}/frame/parent"));
149150
this.commandDictionary.Add(DriverCommand.SwitchToWindow, new CommandInfo(CommandInfo.PostCommand, "/session/{sessionId}/window"));
150151
this.commandDictionary.Add(DriverCommand.GetAllCookies, new CommandInfo(CommandInfo.GetCommand, "/session/{sessionId}/cookie"));
151152
this.commandDictionary.Add(DriverCommand.AddCookie, new CommandInfo(CommandInfo.PostCommand, "/session/{sessionId}/cookie"));

dotnet/src/webdriver/Remote/DriverCommand.cs

+5
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ public static class DriverCommand
163163
/// </summary>
164164
public static readonly string SwitchToFrame = "switchToFrame";
165165

166+
/// <summary>
167+
/// Represents SwitchToParentFrame command
168+
/// </summary>
169+
public static readonly string SwitchToParentFrame = "switchToParentFrame";
170+
166171
/// <summary>
167172
/// Represents GetActiveElement command
168173
/// </summary>

dotnet/src/webdriver/Remote/RemoteTargetLocator.cs

+11
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ public IWebDriver Frame(IWebElement frameElement)
9797
return this.driver;
9898
}
9999

100+
/// <summary>
101+
/// Select the parent frame of the currently selected frame.
102+
/// </summary>
103+
/// <returns>An <see cref="IWebDriver"/> instance focused on the specified frame.</returns>
104+
public IWebDriver ParentFrame()
105+
{
106+
Dictionary<string, object> parameters = new Dictionary<string, object>();
107+
this.driver.InternalExecute(DriverCommand.SwitchToParentFrame, parameters);
108+
return this.driver;
109+
}
110+
100111
/// <summary>
101112
/// Change to the Window by passing in the name
102113
/// </summary>

dotnet/test/common/FrameSwitchingTest.cs

+63
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,69 @@ public void ShouldThrowAnExceptionWhenAFrameCannotBeFoundByIndex()
195195
driver.SwitchTo().Frame(27);
196196
}
197197

198+
[Test]
199+
[IgnoreBrowser(Browser.Chrome, "Browser does not support parent frame navigation")]
200+
[IgnoreBrowser(Browser.IE, "Browser does not support parent frame navigation")]
201+
[IgnoreBrowser(Browser.PhantomJS, "Browser does not support parent frame navigation")]
202+
[IgnoreBrowser(Browser.Safari, "Browser does not support parent frame navigation")]
203+
[IgnoreBrowser(Browser.Android, "Browser does not support parent frame navigation")]
204+
[IgnoreBrowser(Browser.PhantomJS, "Browser does not support parent frame navigation")]
205+
[IgnoreBrowser(Browser.Opera, "Browser does not support parent frame navigation")]
206+
public void ShouldBeAbleToSwitchToParentFrame()
207+
{
208+
driver.Url = framesetPage;
209+
driver.SwitchTo().Frame("fourth").SwitchTo().ParentFrame().SwitchTo().Frame("first");
210+
Assert.AreEqual("1", driver.FindElement(By.Id("pageNumber")).Text);
211+
}
212+
213+
[Test]
214+
[IgnoreBrowser(Browser.Chrome, "Browser does not support parent frame navigation")]
215+
[IgnoreBrowser(Browser.IE, "Browser does not support parent frame navigation")]
216+
[IgnoreBrowser(Browser.PhantomJS, "Browser does not support parent frame navigation")]
217+
[IgnoreBrowser(Browser.Safari, "Browser does not support parent frame navigation")]
218+
[IgnoreBrowser(Browser.Android, "Browser does not support parent frame navigation")]
219+
[IgnoreBrowser(Browser.PhantomJS, "Browser does not support parent frame navigation")]
220+
[IgnoreBrowser(Browser.Opera, "Browser does not support parent frame navigation")]
221+
public void ShouldBeAbleToSwitchToParentFrameFromASecondLevelFrame()
222+
{
223+
driver.Url = framesetPage;
224+
225+
driver.SwitchTo().Frame("fourth").SwitchTo().Frame("child1").SwitchTo().ParentFrame().SwitchTo().Frame("child2");
226+
Assert.AreEqual("11", driver.FindElement(By.Id("pageNumber")).Text);
227+
}
228+
229+
[Test]
230+
[IgnoreBrowser(Browser.Chrome, "Browser does not support parent frame navigation")]
231+
[IgnoreBrowser(Browser.IE, "Browser does not support parent frame navigation")]
232+
[IgnoreBrowser(Browser.PhantomJS, "Browser does not support parent frame navigation")]
233+
[IgnoreBrowser(Browser.Safari, "Browser does not support parent frame navigation")]
234+
[IgnoreBrowser(Browser.Android, "Browser does not support parent frame navigation")]
235+
[IgnoreBrowser(Browser.PhantomJS, "Browser does not support parent frame navigation")]
236+
[IgnoreBrowser(Browser.Opera, "Browser does not support parent frame navigation")]
237+
public void SwitchingToParentFrameFromDefaultContextIsNoOp()
238+
{
239+
driver.Url = xhtmlTestPage;
240+
driver.SwitchTo().ParentFrame();
241+
Assert.AreEqual("XHTML Test Page", driver.Title);
242+
}
243+
244+
[Test]
245+
[IgnoreBrowser(Browser.Chrome, "Browser does not support parent frame navigation")]
246+
[IgnoreBrowser(Browser.IE, "Browser does not support parent frame navigation")]
247+
[IgnoreBrowser(Browser.PhantomJS, "Browser does not support parent frame navigation")]
248+
[IgnoreBrowser(Browser.Safari, "Browser does not support parent frame navigation")]
249+
[IgnoreBrowser(Browser.Android, "Browser does not support parent frame navigation")]
250+
[IgnoreBrowser(Browser.PhantomJS, "Browser does not support parent frame navigation")]
251+
[IgnoreBrowser(Browser.Opera, "Browser does not support parent frame navigation")]
252+
public void ShouldBeAbleToSwitchToParentFromAnIframe()
253+
{
254+
driver.Url = iframePage;
255+
driver.SwitchTo().Frame(0);
256+
257+
driver.SwitchTo().ParentFrame();
258+
driver.FindElement(By.Id("iframe_page_heading"));
259+
}
260+
198261
// ----------------------------------------------------------------------------------------------
199262
//
200263
// General frame handling behavior tests

0 commit comments

Comments
 (0)