Create a Program to Iternate through the folder and create Fitnese test Automatically

Khemlall Mangal
4 min readJun 9, 2023

I was faced with an issue and wanted to basically take existing data that was created with the input and expected output from a current app, which would be replaced with a low code, no code translation application.

I wanted to connect to a BOOMI endpoint and then basically test that the translation produce an identical output. We are using fit library within fitnesse for our automation tool.

So i created a tool to basically do just that.

  1. Template out your action (this require plan and know how all of the sets come together)
  2. In my case i found a pattern which is i will look into each folder (main suite) then Sub-folder — Suite within my Main folder — files are tests
  3. My base file is within the folder given along with the output expected…
  4. I will name my folder the name of the base, capture the name of the folder and add it in my fitnesse page as a variable to be re-used and included in my page.
  5. The folder will be:
  1. Coverted to this
  1. Test will be :
 string path = @"C:\someloacation";
static string basepathdir = @"C:\Destinationpoc\\BASEloca\";
static void Main(string[] args)
{
string sourceDirectory = @"C:\xbv";
string destinationDir = @"C:somelocationdest\";
Console.WriteLine("Hello World!");
ProcessDirectory(sourceDirectory,destinationDir);


foreach (string path in args)
{
if (File.Exists(path))
{
// This path is a file
ProcessFile(path,destinationDir);
}
else if (Directory.Exists(path))
{
// This path is a directory
ProcessDirectory(path,destinationDir);
}
else
{
Console.WriteLine("{0} is not a valid file or directory.", path);
}
}
}
 private static void createDirectory(string directoryname)
{
DirectoryInfo di = Directory.CreateDirectory(directoryname);
Console.WriteLine("The directory was created successfully at {0}.",
Directory.GetCreationTime(directoryname));
}

Process directory

public static void ProcessDirectory(string targetDirectory,string destinationdir)
{

// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(targetDirectory);
foreach (string fileName in fileEntries)
ProcessFile(fileName,destinationdir);

// Recurse into subdirectories of this directory.
string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);

foreach (string subdirectory in subdirectoryEntries)

ProcessDirectory(subdirectory,destinationdir);
}

Open and read and write to file removed some real name and path but just to give you an idea on how you can accomplish something like this.

 public static void Openreadwritefitnessedircontent(string path, string fitnessetestfoldername)
{
string directoryname = MyExtensions.TrimLastCharacter(fitnessetestfoldername);
if (!File.Exists(path))
{
File.Create(path);
TextWriter tw = new StreamWriter(path);
tw.WriteLine("!contents -R2 -g -p -f -h");
//tw.WriteLine("!define DIRECTORY_NAME {" + directoryname + "} ");
tw.WriteLine("!define IN_FILE_NAME {" + fitnessetestfoldername + "}".TrimStart());
tw.WriteLine("!include .SharedTests.TestTemplates.ValidateGenerateddVsTfTemplate");

tw.Close();
}
else if (File.Exists(path))
{
using (var sw = new StreamWriter(path, true))
{

sw.WriteLine("!contents -R2 -g -p -f -h");
//sw.WriteLine("!define DIRECTORY_NAME {" + directoryname + "} ");
sw.WriteLine("!define xIN_FILE_NAME {" + fitnessetestfoldername + "}".TrimStart());
sw.WriteLine("!include .SharedTests.TestTemplates.ValidateBoomiGeneratedTemplate");

}
}


}

Readfile

 public static string ReadFile(string filePath)
{
FileInfo fi = new FileInfo(filePath);

//Open a file for Read\Write
FileStream fs = fi.Open(FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read);

//Create an object of StreamReader by passing FileStream object on which it needs to operates on
StreamReader sr = new StreamReader(fs);

//Use the ReadToEnd method to read all the content from file
string fileContent = sr.ReadToEnd();

//Close the StreamReader object after operation
sr.Close();
fs.Close();
return fileContent;
}
 public static void ProcessFile(string path,string destinationdir)
{
Console.WriteLine("Processed file '{0}'.", path);
string filename = Path.GetFileName(path);

if (filename.Contains("_fdy"))
{
//CREATE A DIRECTORY CALL BASE CXTIN AND COPY THAT ENTIRE FILE IN THERE

DirectoryInfo di = Directory.CreateDirectory(destinationdir);
Console.WriteLine("The directory was created successfully at {0}.",
Directory.GetCreationTime(destinationdir));
//copy file from destination to that new foldr

File.Copy(path, basepathdir + Path.GetFileName(path),true);
if (Directory.Exists(path))
{
var entries = path.Split(@"\/", StringSplitOptions.RemoveEmptyEntries);
}
else if (File.Exists(path))
{
var entries = Path.GetDirectoryName(path).Split(
@"\/", StringSplitOptions.RemoveEmptyEntries);

}
else
{
// error handling
}
string[] directories = path.Split(Path.DirectorySeparatorChar);
foreach (var folder in directories)
{
if (File.Exists(folder))
{
var entries = Path.GetDirectoryName(path).Split(
@"\/", StringSplitOptions.RemoveEmptyEntries);

}

}


foreach (var dir in directories)
{
if(!destinationdir.Contains(dir))
{
if (dir.Contains(".xml"))
{ //fitnessetestfoldername
string fitnessetestfoldername = System.IO.Path.GetFileNameWithoutExtension(path);
destinationdir = destinationdir + "\\ " + fitnessetestfoldername;
createDirectory(destinationdir);

string filecontentpath = destinationdir + "\\" + "content.txt";
string propertiespath = destinationdir + "\\" + "properties.xml";
FileStream filecontent = File.Create(filecontentpath);
filecontent.Close();
FileStream properities = File.Create(propertiespath);
properities.Close();

int pos = fitnessetestfoldername.IndexOf("CXT_");

if (pos >= 0)
{
string Fitnessedefinitionfolderpathname;
// String after founder
Fitnessedefinitionfolderpathname = fitnessetestfoldername.Remove(pos);
Console.WriteLine(Fitnessedefinitionfolderpathname);
Openreadwritefitnessedircontent(filecontentpath, Fitnessedefinitionfolderpathname);
}



string property = ReadFile(@"C:\zom\property\properties.xml");
File.WriteAllText(propertiespath, property);

// File.WriteAllText(propertiespath, "<?xml version="1.0" encoding="UTF - 8" standalone="no"?>< properties >< Edit />< Files />< Help />< Properties />< RecentChanges />< Refactor />< Search />< Suite />< Suites />< Versions />< WhereUsed /></ properties > ";

}
//create a direoctory with that name
else {
destinationdir = destinationdir + "\\ " + dir;
//NAVIGATE INSIDE THAT DIRETORY
createDirectory(destinationdir);
//Create another folder with filename without the xml
string filecontentpath = destinationdir + "\\" + "content.txt";
string propertiespath = destinationdir + "\\" + "properties.xml";
FileStream filecontent = File.Create(filecontentpath);
filecontent.Close();
FileStream properities = File.Create(propertiespath);
properities.Close();

// check if myFile.txt file is created at the specified path
if (File.Exists(filecontentpath))
{
Console.WriteLine("File is created.");
}
else
{
Console.WriteLine("File is not created.");
}

int pos = dir.IndexOf("CXT_");

if (pos >= 0)
{
string Fitnessedefinitionfolderpathname;
// String after founder
Fitnessedefinitionfolderpathname = dir.Remove(pos);
Console.WriteLine(Fitnessedefinitionfolderpathname);
}

OpenReadandWriteToFitnesseContentPage(filecontentpath, dir);
var data = "!define IN_FILE_NAME {" + dir+"_}";
//Add property file
string property = ReadFile(@"C:\property\properties.xml");
File.WriteAllText(propertiespath, property);

string destinationDirectory = destinationdir;

//SAVE AND CLOSE
//Repeat

}
}

}


//Check If directory exist in
//DirectoryInfo di1 = Directory.CreateDirectory(destinationdir);
Console.WriteLine("The directory was created successfully at {0}.",
Directory.GetCreationTime(destinationdir));
//let stream a file and copy that file over to this folder location




}}}

Hope this at least help someone in the future.

--

--

Khemlall Mangal

I am a passionate coder, QA Engineer, and someone who enjoys the outdoors.