How to Call Javascript in Tests

Apr 1, 2011 at 12:15 AM

Hi,

I have added javascript by using Resource that I need to run with my unit tests. Using the following:

String script = null;
Uri uri = new Uri("Namespace;component/myJavascript.js", UriKind.RelativeOrAbsolute);
System.Windows.Resources.StreamResourceInfo sr = Application.GetResourceStream(uri);
using (System.IO.StreamReader reader = new System.IO.StreamReader(sr.Stream))
{
   script = reader.ReadToEnd();
}
System.Windows.Browser.HtmlPage.Window.Eval(script); 



I then do some calls to get configuration settings from javascript file and also test some javascript functions that are called in Silverlight. using:

HtmlPage.Window.Invoke(function, args);

How can I include these to run correctly the tests?

 

Coordinator
Apr 1, 2011 at 1:44 AM

Do you have some tests that pass in browser but do not in StatLight? If so, you probably have the javascript injection code in the App.xaml.cs file so it's run on startup. StatLight will bypass that as it has it's own. 

What testing framework are you using?

Apr 1, 2011 at 1:59 AM

I am using MS Silverlight Toolkit April 2010. Is there a way I can override page and add javascript to it?

Coordinator
Apr 1, 2011 at 3:05 AM

Is it true what I asked earlier?

---

Do you have some tests that pass in browser but do not in StatLight? If so, you probably have the javascript injection code in the App.xaml.cs file so it's run on startup. 

Apr 1, 2011 at 4:29 AM
Edited Apr 1, 2011 at 4:30 AM

For example in App.xaml.cs I have:

RootVisual = UnitTestSystem.CreateTestPage();
String script = null;
Uri uri = new Uri("Namespace;component/myJavascript.js", UriKind.RelativeOrAbsolute);
System.Windows.Resources.StreamResourceInfo sr = Application.GetResourceStream(uri);
using (System.IO.StreamReader reader = new System.IO.StreamReader(sr.Stream))
{
  script = reader.ReadToEnd();
}
System.Windows.Browser.HtmlPage.Window.Eval(script);

 

Then I have some methods I unit test that depend on javascript.

Coordinator
Apr 1, 2011 at 1:25 PM

Ok, I'd suggest moving that code into a method and give it the necessary decorations for use with the AssemblyInitialize attribute http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.assemblyinitializeattribute(v=vs.80).aspx

FYI: the AssemblyInitialize doesn't work in StatLight 1.3. You'll have to download the latest source and run the release build (with no tests) bat file (is the easiest way to get running). 

Hope this helps.

Apr 1, 2011 at 11:29 PM
Edited Apr 1, 2011 at 11:30 PM

Thank you it all works now I created a BaseTest page that all my tests use and had an AssemblyInitialize method to register all the Javascript I needed.