In my several projects, I use MVC pattern for separating code (of concerns) into 3 tiers. Both of Model and Control tiers run on C# so I use testing framework like MSTest or NUnit to validate functional requirement for these tiers. For View tiers, I use QUnit to test JavaScript files.
However, I cannot execute QUnit as automated test because MSTest doesn't directly support for testing web page. I need to run it in MSTest like the following logic.
[TestMethod]
public void JavaScriptTest()
{
var result = QUnit.Test('~/QUnit/test1.htm');
Assert.IsTrue(result.Failed <= 0)
}
Solution must use callback function in QUnit (not while-loop checking) to ensure that test method will execute immediately after test done.
For cross browser testing, Selenium WebDriver is the best option for solving this problem because we can easily switch between browser by changing only one line of code.
2.Download preferred driver to your project, add to your project and set "Copy to Output Directory" equals "Copy if newer". For this example, I use Chrome driver to run Selenium with Google Chrome on my machine.
3.In test method, create driver and set max execution time before running QUnit.
4.Please make sure that you already set autostart of QUnit to false.
5.Navigate to QUnit page. In this case, I use local webpage in solution folder.
6.Use ExecuteAsyncScript method in browser object to register callback for QUnit.done function and manually start QUnit testing.
7.When QUnit test done, it will return a response. We need to convert it as suitable type and get the test result.
8.Don't forget to close browser every time use finish testing.
PS. I also provide Visual Studio 2012 solution (source code) for this answer.
Click here to download
Update 1
PhantomJS is a headless WebKit with JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG. It is full web stack an optimal solution for Headless Website Testing, Screen Capture, Page Automation and Network Monitoring.
I suggest you to use this framework when you want to test some JavaScript library and don't want to use installed browser on test machine.
1.Please make sure that you already set autostart of QUnit to false.
2.Download PhantomJS executable file for Windows, add to your project and set "Copy to Output Directory" equals "Copy if newer".
3.Create process to run PhantomJS.exe with 2 arguments that are JavaScript file and tested page url.
4.Start process and check the exit code of this process.
In JavaScript file, I use eveluateAsync for accessing context of page to run QUnit test, wait until it finish and log amount of test failed.
To handle log, I use the following code to exit process with exit code.
PS. I also provide full source code (VS2012) to my SkyDrive. You can download it at the following link.
PhantomJS Test project
This project demo you how to run PhantomJS in MSTest.
PhantomJS Form project
This project is PhantomJS wrapper that is wrote in C# Windows Form. I use this to test "main.js" and "core.js" files before use it in test project.