Created
September 13, 2016 20:34
-
-
Save klmlfl/9d7693383fabaa9112d7197ba4d5b3cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Xml; | |
using Microsoft.Office.Interop.Outlook; | |
using OutlookApp = Microsoft.Office.Interop.Outlook.Application; | |
namespace env_setup_teardown | |
{ | |
internal class Program | |
{ | |
private static string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); | |
private static string tsname, package, tests, successes, errors, runDate, status; | |
private static void Main(string[] args) | |
{ | |
XcopyBatchKillBrowsers(); | |
RunEggplant(); | |
ParseXML(); | |
GenerateHTMLreport(); | |
CreateMailItem(); | |
} | |
private static void XcopyBatchKillBrowsers() | |
// Executes a local batch file that xcopy's a batch file to remote system and executes it .. killing all browser sessions on remote machine. | |
{ | |
Process proc = new Process(); | |
proc.StartInfo.UseShellExecute = false; | |
proc.StartInfo.FileName = desktopPath + "\\SSH\\win_env_setup.bat"; | |
proc.StartInfo.WindowStyle = ProcessWindowStyle.Minimized; | |
proc.Start(); | |
proc.WaitForExit(); | |
} | |
private static void RunEggplant() | |
// Executes an Eggplant script on remote (The Eggplant script itself contains code that generates a .txt file which details the associated tests run's Results directory) | |
{ | |
Process proc = new Process(); | |
proc.StartInfo.UseShellExecute = false; | |
proc.StartInfo.FileName = desktopPath + "\\SSH\\eggplant.bat"; | |
proc.StartInfo.WindowStyle = ProcessWindowStyle.Minimized; | |
proc.Start(); | |
proc.WaitForExit(); | |
} | |
private static void ParseXML() | |
// Looks at .txt file to determine the Results directory, uses that to parse the results XML | |
{ | |
string logFileDirectory = | |
File.ReadLines(desktopPath + "\\SSH\\EggplantResultsDirectory.txt").First(); | |
XmlDocument doc = new XmlDocument(); | |
doc.Load(logFileDirectory + "LogFile.xml"); | |
XmlNodeList testSuiteNodeList = doc.SelectNodes("/testsuites/testsuite"); | |
XmlNodeList propertyNodeList = doc.SelectNodes("/testsuites/testsuite/properties/property"); | |
foreach (XmlNode testsuitenode in testSuiteNodeList) | |
{ | |
tsname = testsuitenode.Attributes.GetNamedItem("name").Value; | |
package = testsuitenode.Attributes.GetNamedItem("package").Value; | |
tests = testsuitenode.Attributes.GetNamedItem("tests").Value; | |
successes = testsuitenode.Attributes.GetNamedItem("successes").Value; | |
errors = testsuitenode.Attributes.GetNamedItem("errors").Value; | |
Console.WriteLine("Test Name: " + tsname); | |
Console.WriteLine("Package Name: " + package); | |
Console.WriteLine("# of tests: " + tests); | |
Console.WriteLine("Passed: " + successes); | |
Console.WriteLine("Failed: " + errors); | |
} | |
foreach (XmlNode propertynode in propertyNodeList) | |
{ | |
if (propertynode.Attributes.GetNamedItem("name").Value.Equals("RunDate")) | |
{ | |
runDate = propertynode.Attributes.GetNamedItem("value").Value; | |
Console.WriteLine("Run date: " + runDate); | |
} | |
else | |
{ | |
status = propertynode.Attributes.GetNamedItem("value").Value; | |
Console.WriteLine("Status: " + status); | |
} | |
} | |
} | |
private static void GenerateHTMLreport() // Uses the parsed XML data to generate an HTML-formatted report | |
{ | |
using ( | |
FileStream fs = new FileStream(desktopPath + "\\SSH\\EggPlantResultsReport.htm", | |
FileMode.Create)) | |
{ | |
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8)) | |
{ | |
w.WriteLine("<html>"); | |
w.WriteLine("<head><style> body {font-family: Trebuchet MS} table {border-collapse: collapse;} table, td, th {border: 1px solid black; padding: 15px;} caption {font-weight: bold;}</style></head>"); | |
w.WriteLine("<body>"); | |
w.WriteLine("<table>"); | |
w.WriteLine("<tbody>"); | |
w.WriteLine("<tr>"); | |
w.WriteLine("<th>"); | |
w.WriteLine("Package"); | |
w.WriteLine("</th>"); | |
w.WriteLine("<th>"); | |
w.WriteLine("Test name"); | |
w.WriteLine("</th>"); | |
w.WriteLine("<th>"); | |
w.WriteLine("Total"); | |
w.WriteLine("</th>"); | |
w.WriteLine("<th>"); | |
w.WriteLine("Pass"); | |
w.WriteLine("</th>"); | |
w.WriteLine("<th>"); | |
w.WriteLine("Fail"); | |
w.WriteLine("</th>"); | |
w.WriteLine("<th>"); | |
w.WriteLine("Date"); | |
w.WriteLine("</th>"); | |
w.WriteLine("<th>"); | |
w.WriteLine("Status"); | |
w.WriteLine("</th>"); | |
w.WriteLine("</tr>"); | |
w.WriteLine("<tr>"); | |
w.WriteLine(@"<td>"); | |
w.WriteLine(package); | |
w.WriteLine(@"</td>"); | |
w.WriteLine(@"<td>"); | |
w.WriteLine(tsname); | |
w.WriteLine("</td>"); | |
w.WriteLine(@"<td>"); | |
w.WriteLine(tests); | |
w.WriteLine("</td>"); | |
w.WriteLine(@"<td>"); | |
w.WriteLine(successes); | |
w.WriteLine("</td>"); | |
w.WriteLine(@"<td>"); | |
w.WriteLine(errors); | |
w.WriteLine("</td>"); | |
w.WriteLine(@"<td>"); | |
w.WriteLine(runDate); | |
w.WriteLine("</td>"); | |
w.WriteLine(@"<td>"); | |
w.WriteLine(@"<span style=""font-weight:bold; color:blue"">" + status + "</span>"); | |
w.WriteLine("</td>"); | |
w.WriteLine("</tr>"); | |
w.WriteLine("</tbody>"); | |
w.WriteLine("</table>"); | |
w.WriteLine("</body>"); | |
w.WriteLine("</html>"); | |
} | |
} | |
Process.Start(desktopPath + "\\SSH\\EggPlantResultsReport.htm"); | |
} | |
private static void CreateMailItem() | |
{ | |
OutlookApp outlookApp = new OutlookApp(); | |
MailItem mailItem = outlookApp.CreateItem(OlItemType.olMailItem); | |
mailItem.Subject = tsname + ": " + status; | |
mailItem.To = "srahman@xxx.xxx"; | |
mailItem.HTMLBody = File.ReadAllText(desktopPath + "\\SSH\\EggPlantResultsReport.htm"); | |
mailItem.Display(false); | |
mailItem.Send(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment