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.

Advertisement

2 comments

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: