JSTest.NET from Nuget integration with MS Test Series Part 5

This is part 5 of 6 posts, you can find the previous post here.

Integrating MS Test to execute the JavaScript test:

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace WebPortal.Tests.JSTest
{
 [TestClass]
 public class FirstJavaScriptTests : JavaScriptTestBase
 {
  //Path of file name is critical here.
  private const string ProductionCodeFileName
  = @"..\..\..\..\WebPortal.Tests\SampleProduction.js";
  private const string TestCodeFileName
  = @"..\..\..\..\WebPortal.Tests\SampleTest.js";

  /// <summary>
  /// Setup is where you set Production files
  /// </summary>
  [TestInitialize]
  public void Setup()
  {
     AppendProdJavaScriptFile(ProductionCodeFileName);
  }

  [TestMethod]
  public void ReturnSumOfTwoNumber()
  {
     RunTest(TestCodeFileName, "returnSumOfTwoNumber");
  }
 }
}

Code is explained below:

  1. File names of JavaScript test file must be written as private class variable.
  2. Setup must load all production file.
  3. RunTest Method will be used to execute the Tests.

Read post 6 for the concluding guideline here.

JSTest.NET from Nuget integration with MS Test Series Part 4

This is part 4 of 6 posts, you can find the previous post here.

Creating JavascriptTestBase C# class as a helper class:

using System;
using System.IO;
using JSTest;
using JSTest.ScriptElements;
using JSTest.ScriptLibraries;

namespace WebPortal.Tests.JSTest
{
 /// <summary>
 /// Purpose of the class is to load JavaScript files
 /// before each test and run the JSTest.
 /// It also validated the files that are passed to it.
 /// </summary>
 public abstract class JavaScriptTestBase
 {
 protected readonly TestScript _commonTestScript
 = new TestScript();
 private TestCase _testcase;

 public JavaScriptTestBase()
 {
 _commonTestScript
 .AppendBlock(new JsAssertLibrary());
 }

 protected void AppendProdJavaScriptFile
 (string JSFileName)
 {
 _commonTestScript
 .AppendFile(JSFileName);
 }

 /// <summary>
 /// settting up _testcase is prerequites
 /// by calling InitializeTest
 /// (string JSFileName, string JSFunctionName)
 /// before RunTest
 /// </summary>
 protected void RunTest
 (string JSFileName, string JSFunctionName)
 {
 ValidationOnFiles(JSFileName, JSFunctionName);

 try
 {
 _testcase =
 new TestCase(JSFileName, JSFunctionName);
 }
 catch (Exception ex)
 {
 throw new ApplicationException
 ("Error creating TestCase object."
 + "Error Message :" + ex.Message
 + ". StackTrace :" + ex.StackTrace);
 }

 _commonTestScript
 .AppendFile(_testcase.TestFile);
 _commonTestScript
 .RunTest(_testcase.ToScriptFragment());
 }

 /// <summary>
 /// Vaildate if the file exists &
 /// if the function name in the file exists.
 /// </summary>
 /// <param name="JSFileName"></param>
 /// <param name="JSFunctionName"></param>
 private static void ValidationOnFiles
 (string JSFileName, string JSFunctionName)
 {
 if (string.IsNullOrEmpty(JSFileName))
 throw new ArgumentNullException
 ("JSFileName is Null or Empty.");

 if (string.IsNullOrEmpty(JSFunctionName))
 throw new NullReferenceException
 ("JSFunctionName is Null or Empty.");

 if (!File.Exists(JSFileName))
 throw new FileNotFoundException
 ("Argument-> JSFileName: " + JSFileName
 + ". Unable to find the specified file.");

 if (!File.ReadAllText(JSFileName)
 .Contains(JSFunctionName))
 throw new ArgumentException
 ("Argument-> JSFunctionName: " + JSFunctionName
 + ". Unable to find " + JSFunctionName
 + " function in file " + JSFileName);
 }
 }
}

The above class is self explanatory. Other points will be explained in following post, read  Part 5 here.