*Moved to: http://fluentbytes.com/how-to-access-the-src-property-of-an-iframe-with-codedui/
This week I got a request via twitter (@marcelv) if I could help out on an issue with CodedUI. The person had watched my course at Pluralsight on CodedUI and wanted to know if it was possible to change the src property of an IFrame with a CodedUI test.
So the code he had was something along the following lines:
var bw = BrowserWindow.Launch(new Uri("http://yourserver/Index.html")); HtmlIFrame frame = new HtmlIFrame(bw); frame.SearchProperties.Add(HtmlIFrame.PropertyNames.Id, "yourIframeID"); frame.SetProperty("Src", "http://bing.com"); -- or frame.setProperty(HtmlIframe.PropertyNames.PageUrl,"http://bing.com"); --- Assert on the changed property ---
The problem he ran into is that this code will throw an exception the moment you try to set the src property. It will throw the following exception:
System.NotSupportedException: SetProperty of “Src” is not supported on control type: Frame
One thing I tried is to use frame.PageUrl, but that does not work, since the property is marked as read-only, so that code will not even compile.
I think it is a bug in the codedUI object model, but the question is how can I fix this in the meanwhile?
The solution I have to this is the following:
Either create a JavaScript and use BrowserWindow.ExecuteScript to change the property, or change the property using the native element. The later is done as follows:
var bw = BrowserWindow.Launch(new Uri("http://localhost:58664/Index.html")); HtmlIFrame frame = new HtmlIFrame(bw); frame.SearchProperties.Add(HtmlIFrame.PropertyNames.Id, "myIframe"); IHTMLFrameBase nativeframe = (IHTMLFrameBase)frame.NativeElement; nativeframe.src = "http://bing.com"; --- Assert on the changed property ----
Now this solution works, but be aware that this is an IE Only solution. Here I am using COM interop behind the scenes to interact with the IE DOM to make the change to the property. If you would like to keep the capability of cross browser testing, you might be better off using the JavaScript option instead.
Hope this helps
Follow my new blog on http://fluentbytes.com