Suppose you create a test page with qUint to test your javascript code and want to integrate with your Continuous Integration Build Process. Selenium 2 is a very good selection to fit your purpose.
With the APIs provided by Selenium 2 ( aka WebDriver ), it is very easy to write unit test in a language of your choice and run automatically in CI server, for example, Hudson.
Selenium is easy to download as Nuget package. To install, run the following commands in the Package Manager Console of Visual Studio 2012
PM> Install-package Selenium.WebDriver ( see more )
PM> Install-package Selenium.Support ( see more )
MSTest Code example:
using System;
using System.Text;using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
namespace MyTests
{
[TestClass]
public class JSTests
{
[TestMethod]
[DeploymentItem("chromedriver.exe")]
public void JavaScriptTests()
{
try
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl(Properties.Settings.Default.TestPage);
IWebElement testResult = FindElement(driver, "qunit-testresult");
System.Threading.Thread.Sleep(500);
string result = testResult.Text;
System.Console.WriteLine("Result:");
Console.WriteLine(result);
IList<IWebElement> failed = testResult.FindElements(By.ClassName("failed"));
int numFailed = 0;
if ( failed != null && failed.Count > 0 )
{
foreach (IWebElement we in failed)
{
if ( int.TryParse(we.Text, out numFailed) && numFailed > 0 )
{
string msg = string.Format("Tests failed, see detailed on testpage: {0}", Properties.Settings.Default.TestPage);
Console.WriteLine(msg);
driver.Quit();
Assert.Fail(msg);
break;
}
}
}
else
driver.Quit();
}
catch (Exception e)
{
Assert.Fail(e.Message);
}
}
private IWebElement FindElement(IWebDriver driver, string id)
{
// Wait for the result, timeout after 10 seconds
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(Properties.Settings.Default.TimeoutSeconds));
IWebElement queryResult = wait.Until<IWebElement>((d) =>
{
return d.FindElement(By.Id(id));
});
return queryResult;
}
}
}