Silverlight UIAutomation Testing -- Using WatiN to navigate to a page and White to test a Silverlight app
I had the need recently to automate testing of a Silverlight app and ran into a problem using white (could anyone have come up with a more difficult name to search on?) where white does not provide functionality to get a reference to a hyperlink in the DOM and click it in the way that, for example, WatiN does. The scenario where this came up is there is a web app with a landing page that has a link to the page hosting the Silverlight app and we need to click through the link into the Silverlight hosting page.
I briefly thought about just cribbing the functionality from WatiN and extending white's capabilities but quickly discarded that idea as too complicated and out of concern that it would become a maintenance nightmare.
The solution I came up with is to use WatiN and white together in a hybrid fashion, where WatiN spins up a browser instance and then white attaches to that browser instance using the ProcessID of the instance from WatiN. White doesn't expose an Attach method up in the InternetExplorer class where Launch lives, but it does have it down in White.Core on the Application class, so it was a simple matter to create an extension method on the InternetExplorer class to expose one that can be used in unit tests.
It goes a little like this (warning: horrible formatting):
I briefly thought about just cribbing the functionality from WatiN and extending white's capabilities but quickly discarded that idea as too complicated and out of concern that it would become a maintenance nightmare.
The solution I came up with is to use WatiN and white together in a hybrid fashion, where WatiN spins up a browser instance and then white attaches to that browser instance using the ProcessID of the instance from WatiN. White doesn't expose an Attach method up in the InternetExplorer class where Launch lives, but it does have it down in White.Core on the Application class, so it was a simple matter to create an extension method on the InternetExplorer class to expose one that can be used in unit tests.
It goes a little like this (warning: horrible formatting):
public static class WhiteExtensions
{
// the extension method to simplify usage
public static InternetExplorerWindow Attach(
this InternetExplorer val, int processId, string title)
{
InternetExplorerFactory.Plugin();
return (InternetExplorerWindow)Application
.Attach(processId)
.GetWindow(title);
}
}
// new up a browser with WatiN
Watin.Core.IE watin = new Watin.Core.IE(url);
watin.Link(Find.ByText("foo")).Click();
// spin up white and attach to the browser that WatiN started
White.WebBrowser.InternetExplorer white = new White.WebBrowser.InternetExplorer();
white.Attach(watin.ProcessID, "browser window title");
// get the Silverlight app reference from the white browser reference
White.WebBrowser.Silverlight.SilverlightDocument sl = white.SilverlightDocument;
// do stuff
sl.Get<Button>(SearchCriteria.ByAutomationId("SomeButton")).Click();


3 Comments:
I went to school with a gal named Pamela Anderson. Needless to say, when I googled her the first few million hits weren't that Pamela Anderson.
Hi Leo,
Thanks for publishing the code! I added a link to your article here:
http://watinandmore.blogspot.com/2010/01/combine-watin-and-white-to-test.html
Jeroen van Menen
Lead dev WatiN
Thanks Jeroen!
And thanks for creating WatiN, it's a great tool and I use it quite a lot.
Post a Comment
Links to this post:
Create a Link
<< Home