-
Notifications
You must be signed in to change notification settings - Fork 0
/
sauceREST.cs
252 lines (210 loc) · 10.1 KB
/
sauceREST.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using Newtonsoft.Json;
namespace saucelabs.saucerest {
public class SauceREST : saucerest_net.ISauceREST {
protected string username;
protected string accessKey;
public static readonly string RESTURL = "https://saucelabs.com/rest/v1/{0}";
private static readonly string USER_RESULT_FORMAT = RESTURL + "/{1}";
private static readonly string JOB_RESULT_FORMAT = RESTURL + "/jobs/{1}";
private static readonly string JOBLIST_RESULT_FORMAT = RESTURL + "/jobs?from={1}&to={2}&limit={3}";
private static readonly string DOWNLOAD_VIDEO_FORMAT = "https://saucelabs.com/rest/{0}/jobs/{1}/results/video.flv";
private static readonly string DOWNLOAD_LOG_FORMAT = JOB_RESULT_FORMAT + "/results/video.flv";
private static readonly string DATE_FORMAT = "yyyyMMdd_HHmmSS";
public int TimoutMS { get; set; }
public SauceREST(string username, string accessKey) {
this.username = username;
this.accessKey = accessKey;
TimoutMS = 30000;
}
public SauceREST(string username, string accessKey, int timeout) {
this.username = username;
this.accessKey = accessKey;
TimoutMS = timeout;
}
/**
* Marks a Sauce Job as 'passed'.
*
* @param jobId the Sauce Job Id, typically equal to the Selenium/WebDriver sessionId
* @throws IOException thrown if an error occurs invoking the REST request
*/
public void jobPassed(string jobId) {
Dictionary<string, Object> updates = new Dictionary<string, Object>();
updates.Add("passed", true);
updateJobInfo(jobId, updates);
}
/**
* Marks a Sauce Job as 'failed'.
*
* @param jobId the Sauce Job Id, typically equal to the Selenium/WebDriver sessionId
* @throws IOException thrown if an error occurs invoking the REST request
*/
public void jobFailed(string jobId) {
Dictionary<string, Object> updates = new Dictionary<string, Object>();
updates.Add("passed", false);
updateJobInfo(jobId, updates);
}
/**
* Downloads the video for a Sauce Job to the filesystem. The file will be stored in
* a directory specified by the <code>location</code> field.
*
* @param jobId the Sauce Job Id, typically equal to the Selenium/WebDriver sessionId
* @param location
* @throws IOException thrown if an error occurs invoking the REST request
*/
public void downloadVideo(string jobId, string location) {
Uri restEndpoint = null;
try {
restEndpoint = new Uri(String.Format(DOWNLOAD_VIDEO_FORMAT, username, jobId));
} catch (UriFormatException e) {
Log("Error constructing Sauce URL: " + e.ToString());
}
downloadFile(jobId, location, restEndpoint);
}
/**
* Downloads the log file for a Sauce Job to the filesystem. The file will be stored in
* a directory specified by the <code>location</code> field.
*
* @param jobId the Sauce Job Id, typically equal to the Selenium/WebDriver sessionId
* @param location
* @throws IOException thrown if an error occurs invoking the REST request
*/
public void downloadLog(string jobId, string location) {
Uri restEndpoint = null;
try {
restEndpoint = new Uri(String.Format(DOWNLOAD_LOG_FORMAT, username, jobId));
} catch (UriFormatException e) {
Log("Error constructing Sauce URL: " + e.ToString());
}
downloadFile(jobId, location, restEndpoint);
}
public string retrieveResults(string path) {
Uri restEndpoint = null;
try {
restEndpoint = new Uri(String.Format(USER_RESULT_FORMAT, username, path));
} catch (UriFormatException e) {
Log("Error constructing Sauce URL: " + e.ToString());
}
return retrieveResults(restEndpoint);
}
public string getJobInfo(string jobId) {
Uri restEndpoint = null;
try {
restEndpoint = new Uri(String.Format(JOB_RESULT_FORMAT, username, jobId));
} catch (UriFormatException e) {
Log("Error constructing Sauce URL: " + e.ToString());
}
return retrieveResults(restEndpoint);
}
private string retrieveResults(Uri restEndpoint) {
string results = string.Empty;
try {
WebRequest request = WebRequest.Create(restEndpoint);
request.Method = "GET";
request.Timeout = TimoutMS;
String auth = encodeAuthentication();
request.Headers.Add("Authorization", "Basic " + auth);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK) {
StreamReader stream = new StreamReader(response.GetResponseStream());
results = stream.ReadToEnd();
}
response.Close();
} catch (IOException e) {
Log("Error retrieving Sauce Results: " + e.ToString());
}
return results;
}
private void downloadFile(string jobId, string location, Uri restEndpoint) {
string results = string.Empty;
StreamReader stream;
try {
WebRequest request = WebRequest.Create(restEndpoint);
request.Method = "GET";
request.Timeout = TimoutMS;
string auth = encodeAuthentication();
request.Headers.Add("Authorization", "Basic " + auth);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK) {
stream = new StreamReader(response.GetResponseStream()); // might need to just use the Stream and create a file directly from the stream
results = stream.ReadToEnd();
}
string saveName = jobId + DateTime.Now.ToString(DATE_FORMAT);
if (restEndpoint.AbsoluteUri.EndsWith(".flv")) {
saveName = saveName + ".flv";
} else {
saveName = saveName + ".log";
}
System.IO.StreamWriter file = new System.IO.StreamWriter(location + saveName, false);
file.WriteLine(results);
file.Close();
Log("Error downloading Sauce Results: ");
} catch (IOException e) {
Log("Error downloading Sauce Results: " + e.ToString());
}
}
public void updateJobInfo(string jobId, Dictionary<string, Object> updates) {
string results = string.Empty;
StreamReader stream;
try {
Uri restEndpoint = new Uri(String.Format(JOB_RESULT_FORMAT, username, jobId));
WebRequest request = WebRequest.Create(restEndpoint);
request.Method = "PUT";
request.Timeout = TimoutMS;
string auth = encodeAuthentication();
request.Headers.Add("Authorization", "Basic " + auth);
string jsonText = JsonConvert.SerializeObject(updates);
Stream requestStream = request.GetRequestStream();
ASCIIEncoding encoding = new ASCIIEncoding ();
byte[] byte1 = encoding.GetBytes (jsonText);
requestStream.Write(byte1, 0, byte1.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK) {
stream = new StreamReader(response.GetResponseStream()); // might need to just use the Stream and create a file directly from the stream
results = stream.ReadToEnd();
}
response.Close();
} catch (IOException e) {
Log("Error updating Sauce Results: " + e.ToString());
}
}
public Dictionary<string, string>[] getJobIDList(DateTime start_time, DateTime end_time, int limit) {
Uri restEndpoint = null;
try {
restEndpoint = new Uri(String.Format(JOBLIST_RESULT_FORMAT, username, DateTimeToUnixTimestamp(start_time), DateTimeToUnixTimestamp(end_time), limit));
} catch (UriFormatException e) {
Log("Error constructing Sauce URL: " + e.ToString());
}
string result = retrieveResults(restEndpoint);
if (!result.Contains("error"))
return JsonConvert.DeserializeObject<Dictionary<string, string>[]>(result);
else
return JsonConvert.DeserializeObject<Dictionary<string, string>[]>("");
}
private double DateTimeToUnixTimestamp(DateTime dateTime) {
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
TimeSpan diff = dateTime - origin;
return Math.Floor(diff.TotalSeconds);
}
public string encodeAuthentication() {
string auth = username + ":" + accessKey;
try {
byte[] encData_byte = new byte[auth.Length];
encData_byte = System.Text.Encoding.UTF8.GetBytes(auth);
string encodedData = Convert.ToBase64String(encData_byte);
return encodedData;
} catch (Exception e) {
throw new Exception("Error in base64Encode" + e.Message);
}
}
private void Log(String message) {
System.IO.StreamWriter file = new System.IO.StreamWriter("\\log.txt", true);
file.WriteLine(message);
file.Close();
}
}
}