-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEggPlantScript.cs
198 lines (184 loc) · 6.85 KB
/
EggPlantScript.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Xml;
using Gallio.Common.Markup;
using Gallio.Framework;
using Gallio.Model;
using MbUnit.Framework;
using ProtoTest.Nightshade;
using Action = Gallio.Common.Action;
namespace ProtoTest.TestRunner.Nightshade
{
/// <summary>
/// This class contains all info related to executing an eggplant script and validating it was successful.
/// It is passed an instantiated EggplantDriver.
/// </summary>
public class EggplantScript
{
private readonly EggplantDriver Driver;
//private Process cmdProcess;
public string description;
public string host;
public string scriptName;
public string scriptPath;
public string suitePath;
public int timeoutMin;
public EggplantScript(EggplantDriver Driver, string suitePath, string scriptName, string host, int timeoutMin)
{
this.Driver = Driver;
this.suitePath = suitePath;
this.scriptName = scriptName;
this.host = host;
this.timeoutMin = timeoutMin;
scriptPath += suitePath + "\\Scripts\\" + scriptName + ".script";
description = GetCommentsFromScript();
VerifyScriptExists();
}
/// <summary>
/// Verify the script file exists
/// </summary>
private void VerifyScriptExists()
{
Assert.IsTrue(File.Exists(scriptPath), "Test aborted, could not find file : " + scriptPath);
}
/// <summary>
/// Execute the script and verify it passed
/// </summary>
/// <param name="testName"></param>
/// <returns></returns>
public TestOutcome ExecuteScriptAndGetOutcome(string testName)
{
Action executeTest = delegate
{
Driver.Connect(host);
Driver.ExecuteScript(scriptName, description);
VerifySuccess();
AttachTestFiles();
};
return TestStep.RunStep(testName, executeTest, new TimeSpan(0, 0, timeoutMin, 0), true, null).Outcome;
}
/// <summary>
/// Get the test result files and embed them into the report
/// </summary>
private void AttachTestFiles()
{
TestLog.Attach(new TextAttachment("LogFile.txt", "locator", GetLogFile()));
TestLog.Attach(new TextAttachment("LogFile.xml", "locator", GetLogXmlFile()));
}
/// <summary>
/// Gets the LogFile.txt for the current test
/// </summary>
/// <returns></returns>
private string GetLogFile()
{
return File.ReadAllText(getResultDirectory() + "\\LogFile.txt");
}
/// <summary>
/// Gets the LogFile.xml for the current test
/// </summary>
/// <returns></returns>
private string GetLogXmlFile()
{
return File.ReadAllText(getResultDirectory() + "\\LogFile.xml");
}
/// <summary>
/// Gets the path of the results directory
/// </summary>
/// <returns></returns>
private string getResultDirectory()
{
string path = "";
path += suitePath;
path += "\\Results\\" + scriptName + "\\";
string[] directories = Directory.GetDirectories(path);
string biggest = "0";
foreach (string dir in directories)
{
string folder = dir.Replace(path, "");
folder = folder.Replace("\\", "");
if (folder.CompareTo(biggest) > 0)
biggest = folder;
}
return path + "\\" + biggest;
}
/// <summary>
/// Returns all lines marked as comments in the script file
/// </summary>
/// <returns></returns>
private string GetCommentsFromScript()
{
if (!File.Exists(scriptPath))
throw new FileNotFoundException("Could not find script file at path : " + scriptPath);
string path = scriptPath;
var file = new StreamReader(path);
string line = "";
string result = "";
while ((line = file.ReadLine()) != null)
{
if (line.Contains("--"))
{
result += line += "\r\n";
}
}
return result;
}
/// <summary>
/// Verify the script that ran was successful. Load the results files and parse them for errors.
/// </summary>
private void VerifySuccess()
{
DiagnosticLog.WriteLine("Verifying Test : " + scriptName);
var resultsFile = new XmlDocument();
string file = getResultDirectory() + "\\LogFile.xml";
DiagnosticLog.WriteLine("Checking results file : " + file);
resultsFile.Load(file);
string result = resultsFile.SelectSingleNode("//property[@name='Status']/@value").Value;
if (result != "Success")
{
DiagnosticLog.WriteLine("Test Failure Detected : " + scriptName);
TestContext.CurrentContext.IncrementAssertCount();
TestLog.Failures.BeginSection("EggPlant Error");
TestLog.Failures.WriteLine(GetFailureMessage());
if (File.Exists(getResultDirectory() + "\\Screen_Error.png"))
{
TestLog.Failures.EmbedImage(null,
Common.ScaleImage(Image.FromFile(getResultDirectory() + "\\Screen_Error.png")));
}
TestLog.Failures.End();
Assert.TerminateSilently(TestOutcome.Failed);
}
}
/// <summary>
/// Get the failure message from the LogFile.txt file
/// </summary>
/// <returns></returns>
public string GetFailureMessage()
{
string file = getResultDirectory() + "\\LogFile.txt";
DiagnosticLog.WriteLine("Getting failure reason from file : " + file);
string line;
string previous = "";
using (var reader = new StreamReader(file))
{
line = reader.ReadLine();
while (line != null)
{
if (line.Contains("Error"))
{
var error = new StringBuilder();
error.AppendLine(previous);
error.AppendLine(line);
error.AppendLine(reader.ReadToEnd());
return error.ToString();
}
previous = line;
line = reader.ReadLine();
}
return "";
}
}
}
}