Skip to content

Commit 24ff037

Browse files
committed
Updating .NET PageFactory to allow frame switching with located elements
Previously, if you located a `<frame>` or `<iframe>` element using the PageFactory attributes, you could not switch to it. This change allows one to use `driver.SwitchTo().Frame(element)` with the element found via the PageFactory.
1 parent 9dd878d commit 24ff037

File tree

5 files changed

+38
-8
lines changed

5 files changed

+38
-8
lines changed

dotnet/src/support/PageObjects/WebElementProxy.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace OpenQA.Selenium.Support.PageObjects
3131
/// <summary>
3232
/// Intercepts the request to a single <see cref="IWebElement"/>
3333
/// </summary>
34-
internal sealed class WebElementProxy : WebDriverObjectProxy
34+
internal sealed class WebElementProxy : WebDriverObjectProxy, IWrapsElement
3535
{
3636
private IWebElement cachedElement;
3737

@@ -48,6 +48,14 @@ private WebElementProxy(Type classToProxy, IElementLocator locator, IEnumerable<
4848
{
4949
}
5050

51+
/// <summary>
52+
/// Gets the <see cref="IWebElement"/> wrapped by this object.
53+
/// </summary>
54+
public IWebElement WrappedElement
55+
{
56+
get { return this.Element; }
57+
}
58+
5159
/// <summary>
5260
/// Gets the IWebElement object this proxy represents, returning a cached one if requested.
5361
/// </summary>

dotnet/src/webdriver/Remote/RemoteTargetLocator.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System.Collections.ObjectModel;
2222
using System.Text;
2323
using System.Text.RegularExpressions;
24+
using OpenQA.Selenium.Internal;
2425

2526
namespace OpenQA.Selenium.Remote
2627
{
@@ -65,10 +66,6 @@ public IWebDriver Frame(string frameName)
6566
throw new ArgumentNullException("frameName", "Frame name cannot be null");
6667
}
6768

68-
// Dictionary<string, object> parameters = new Dictionary<string, object>();
69-
// parameters.Add("id", frameName);
70-
// this.driver.InternalExecute(DriverCommand.SwitchToFrame, parameters);
71-
// return this.driver;
7269
string name = Regex.Replace(frameName, @"(['""\\#.:;,!?+<>=~*^$|%&@`{}\-/\[\]\(\)])", @"\$1");
7370
ReadOnlyCollection<IWebElement> frameElements = this.driver.FindElements(By.CssSelector("frame[name='" + name + "'],iframe[name='" + name + "']"));
7471
if (frameElements.Count == 0)
@@ -96,6 +93,15 @@ public IWebDriver Frame(IWebElement frameElement)
9693
}
9794

9895
RemoteWebElement convertedElement = frameElement as RemoteWebElement;
96+
if (convertedElement == null)
97+
{
98+
IWrapsElement elementWrapper = frameElement as IWrapsElement;
99+
if (elementWrapper != null)
100+
{
101+
convertedElement = elementWrapper.WrappedElement as RemoteWebElement;
102+
}
103+
}
104+
99105
if (convertedElement == null)
100106
{
101107
throw new ArgumentException("frameElement cannot be converted to RemoteWebElement", "frameElement");

dotnet/test/support/PageObjects/PageFactoryBrowserTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ public void MixingFindBySequenceAndFindByAllShouldThrow()
116116
Assert.Throws<ArgumentException>(() => PageFactory.InitElements(driver, page), "Cannot specify FindsBySequence and FindsByAll on the same member");
117117
}
118118

119+
[Test]
120+
public void FrameTest()
121+
{
122+
driver.Url = iframePage;
123+
var page = new PageFactoryBrowserTest.IFramePage();
124+
PageFactory.InitElements(driver, page);
125+
driver.SwitchTo().Frame(page.Frame);
126+
}
127+
119128
#region Page classes for tests
120129
#pragma warning disable 649 //We set fields through reflection, so expect an always-null warning
121130

@@ -156,6 +165,12 @@ private class InvalidAttributeCombinationPage
156165
public IWebElement NotFound;
157166
}
158167

168+
private class IFramePage
169+
{
170+
[FindsBy(How = How.Id, Using = "iframe1")]
171+
public IWebElement Frame;
172+
}
173+
159174
#pragma warning restore 649
160175
#endregion
161176
}

dotnet/test/support/WebDriver.Support.Tests.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<add key="Protocol" value="http"/>
1010
<add key="HostName" value="localhost"/>
1111
<add key="Port" value="2310"/>
12+
<add key="SecurePort" value="2410"/>
1213
<add key="Folder" value="common"/>
1314
</appSettings>
1415
</configuration>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<NUnitProject>
22
<Settings appbase="." />
3-
<Config name="Default" binpathtype="Auto">
4-
<assembly path="WebDriver.Support.Tests.dll" />
5-
</Config>
3+
<Config name="Default" binpathtype="None" configfile="WebDriver.Support.Tests.config">
4+
<assembly path="WebDriver.Support.Tests.dll" />
5+
</Config>
66
</NUnitProject>

0 commit comments

Comments
 (0)