-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSeleniumhqBuilder.java
More file actions
440 lines (392 loc) · 15.3 KB
/
SeleniumhqBuilder.java
File metadata and controls
440 lines (392 loc) · 15.3 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package hudson.plugins.seleniumhq;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Descriptor;
import hudson.model.Result;
import hudson.tasks.Builder;
import hudson.util.FormValidation;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import net.sf.json.JSONObject;
import org.apache.commons.io.FileUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
/**
* Sample {@link Builder}.
*
* <p>
* When the user configures the project and enables this builder,
* {@link DescriptorImpl#newInstance(StaplerRequest)} is invoked and a new {@link SeleniumhqBuilder}
* is created. The created instance is persisted to the project configuration XML by using XStream,
* so this allows you to use instance fields (like {@link #name}) to remember the configuration.
*
* <p>
* When a build is performed, the {@link #perform(Build, Launcher, BuildListener)} method will be
* invoked.
*
* @author Pascal Martin
*/
public class SeleniumhqBuilder extends Builder {
private final String browser;
private final String startURL;
private final String suiteFile;
private final String resultFile;
private final String other;
private final boolean testEnabled;
@DataBoundConstructor
public SeleniumhqBuilder(String browser, String startURL, String suiteFile, String resultFile,
String other, boolean testEnabled) {
this.browser = browser;
this.startURL = startURL;
this.suiteFile = suiteFile;
this.resultFile = resultFile;
this.other = other;
this.testEnabled = testEnabled;
}
//Left this call in to satisfy the tests. Did not change the tests.
public SeleniumhqBuilder(String browser, String startURL, String suiteFile, String resultFile,
String other) {
this.browser = browser;
this.startURL = startURL;
this.suiteFile = suiteFile;
this.resultFile = resultFile;
this.other = other;
this.testEnabled = true;
}
/**
* We'll use this from the <tt>config.jelly</tt>.
*/
public String getBrowser() {
return browser;
}
/**
* We'll use this from the <tt>config.jelly</tt>.
*/
public String getStartURL() {
return startURL;
}
/**
* We'll use this from the <tt>config.jelly</tt>.
*/
public String getSuiteFile() {
return suiteFile;
}
/**
* We'll use this from the <tt>config.jelly</tt>.
*/
public String getOther() {
return other;
}
/**
* We'll use this from the <tt>config.jelly</tt>.
*/
public String getResultFile() {
return resultFile;
}
/**
* We'll use this from the <tt>config.jelly</tt>.
*/
public boolean getTestEnabled() {
return testEnabled;
}
/**
* Check if the suiteFile is a URL
*
* @return true if the suiteFile is a valid url else return false
*/
public boolean isURLSuiteFile() {
try {
URL url = new URL(this.suiteFile);
return url != null;
} catch (Exception e) {
return false;
}
}
/**
* Check if the suiteFile is a file
*
* @return true if the suiteFile is a filePath else return false
* @throws InterruptedException
* @throws IOException
*/
public boolean isFileSuiteFile(AbstractBuild<?,?> build, Launcher launcher) throws IOException, InterruptedException {
FilePath suiteFilePath = new FilePath(build.getWorkspace(), this.suiteFile);
if (suiteFilePath.exists())
{
return suiteFilePath.isDirectory() == false;
}
else
{
suiteFilePath = new FilePath(launcher.getChannel(), this.suiteFile);
if (suiteFilePath.exists())
{
return suiteFilePath.isDirectory() == false;
}
}
return false;
}
@Override
public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener)
throws IOException, InterruptedException {
// -------------------------------
// Check global config
// -------------------------------
if (!DESCRIPTOR.isGoodSeleniumRunner()) {
listener.error("Please configure the Selenium Remote Control htmlSuite Runner in admin of hudson");
build.setResult(Result.FAILURE);
return false;
}
//if test is not enabled we should mark it as NOT BUILT (rather than success or failure
if(!this.getTestEnabled()) {
listener.getLogger().println("---------------");
listener.getLogger().println("SKIPPING: Test Suite. '" + this.getSuiteFile() + "' was marked as not enabled");
listener.getLogger().println("---------------");
//Let the other surrounding tests determine the build result
//build.setResult(Result.NOT_BUILT);
//return build so build doesn't get aborted & other tests will get run
return true;
}
// -------------------------------
// Check projet config
// -------------------------------
if (this.getBrowser() == null || this.getBrowser().length() == 0) {
listener.error("Build config : browser field is mandatory");
build.setResult(Result.FAILURE);
return false;
}
if (this.getStartURL() == null || this.getStartURL().length() == 0) {
listener.error("Build config : startURL field is mandatory");
build.setResult(Result.FAILURE);
return false;
}
if (this.getSuiteFile() == null || this.getSuiteFile().length() == 0) {
listener.error("Build config : suiteFile field is mandatory");
build.setResult(Result.FAILURE);
return false;
}
if (this.getResultFile() == null || this.getResultFile().length() == 0) {
listener.error("Build config : resultFile field is mandatory");
build.setResult(Result.FAILURE);
return false;
}
// -------------------------------
// check suiteFile type url or file
// -------------------------------
String suiteFile = null;
FilePath tempSuite = null;
if (this.isFileSuiteFile(build, launcher))
{
FilePath suiteFilePath = new FilePath(build.getWorkspace(), this.suiteFile);
if (suiteFilePath.exists() == false) // File exist on remote
{
suiteFilePath = new FilePath(launcher.getChannel(), this.suiteFile);
}
suiteFile = suiteFilePath.getRemote();
}
else if (this.isURLSuiteFile())
{
tempSuite = build.getWorkspace().createTempFile("tempHtmlSuite", "html");
suiteFile = tempSuite.getRemote();
try
{
File localWorkspace = new File(build.getRootDir(), "workspace");
File tempSuiteLocal = localWorkspace.createTempFile("tempHtmlSuite", "html");
listener.getLogger().println("Try downloading suite file on master");
listener.getLogger().println(" from url : " + this.suiteFile );
listener.getLogger().println(" to file : " + tempSuiteLocal.getPath());
FileUtils.copyURLToFile(new URL(this.suiteFile), tempSuiteLocal);
listener.getLogger().println(" ...");
listener.getLogger().println(" Succeed");
listener.getLogger().println("Try transfer suite file on slave");
listener.getLogger().println(" from file : " + tempSuiteLocal.getPath() );
listener.getLogger().println(" to file : " + suiteFile);
FilePath sourceFile = new FilePath(tempSuiteLocal);
sourceFile.copyTo(tempSuite);
listener.getLogger().println(" ...");
listener.getLogger().println(" Succeed");
sourceFile.delete();
}
catch(Exception e)
{
listener.error("Downloading suite file from url failed ! Check your build configuration. ");
build.setResult(Result.FAILURE);
return false;
}
}
//see if we've got a jenkins variable. if so just pass it along
else if (this.suiteFile.startsWith("$"))
{
listener.getLogger().println("Suite File Looks Like a Parameter: "+ this.suiteFile);
suiteFile = this.suiteFile;
}
else
{
// The suiteFile it is a unsuported type
listener.error("The suiteFile is not a file or an url ! Check your build configuration.");
build.setResult(Result.FAILURE);
return false;
}
// -------------------------------
// launch : java -jar selenium-server.jar [other] -htmlSuite "{browser}" "{startURL}"
// "{suiteFile}" "{resultFile}"
// -------------------------------
String seleniumRunner = FileUtil.getExecutableAbsolutePath(DESCRIPTOR.getSeleniumRunner());
FilePath resultFilePath = new FilePath(build.getWorkspace(), this.resultFile);
resultFilePath.getParent().mkdirs();
String resultFile = resultFilePath.getRemote();
ArrayList cmd = new ArrayList();
cmd.add("java");
cmd.add("-jar");
cmd.add(seleniumRunner);
cmd.addAll(this.getOthers());
cmd.add("-htmlSuite");
cmd.add(this.getBrowser());
cmd.add(this.getStartURL());
cmd.add(suiteFile);
cmd.add(resultFile);
try
{
String javaCmdString = "";
Iterator<String> itr = cmd.iterator();
while(itr.hasNext())
{
javaCmdString += " " + itr.next();
}
listener.getLogger().println(javaCmdString);
launcher.launch().cmds(cmd).envs(build.getEnvironment(listener)).stdout(listener.getLogger()).pwd(build.getWorkspace()).join();
return true;
}
catch (IOException e)
{
e.printStackTrace();
listener.getLogger().println("IOException!");
return false;
}
catch (InterruptedException e)
{
e.printStackTrace();
listener.getLogger().println("InterruptedException!");
return false;
}
finally
{
// -------------------------------
// Delete the temp suite file
// -------------------------------
if (tempSuite != null)
tempSuite.delete();
}
}
/**
* @desc get the arrayList of optional parameters
*
* @return ArrayList containing parameters
*/
private final ArrayList getOthers()
{
ArrayList cmdParams = new ArrayList();
// fix https://issues.jenkins-ci.org/browse/JENKINS-7246 caused by patch in https://issues.jenkins-ci.org/browse/JENKINS-6996
// (do not use "this.getOther().isEmpty()", to be compatible with jdk 1.5)
if (this.getOther() != null && this.getOther().length() != 0)
{
String otherParams = this.getOther();
String[] otherParamsArray = otherParams.split(" ");
for (int i = 0; i < otherParamsArray.length; ++i)
{
// Leave spaces between quotes
if (otherParamsArray[i].matches("\""))
{
String paramBuffer = otherParamsArray[i];
do
{
++i;
paramBuffer += otherParamsArray[i];
} while ((!otherParamsArray[i].matches("\"")) && (i < otherParamsArray.length));
cmdParams.add(paramBuffer);
}
else if(otherParamsArray[i].matches("\'"))
{
String paramBuffer = otherParamsArray[i];
do
{
++i;
paramBuffer += otherParamsArray[i];
} while ((!otherParamsArray[i].matches("\'")) && (i < otherParamsArray.length));
cmdParams.add(paramBuffer);
}
else
{
cmdParams.add(otherParamsArray[i]);
}
}
}
return cmdParams;
}
@Extension
public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl();
/**
* Descriptor for {@link SeleniumhqBuilder}. Used as a singleton. The class is marked as public
* so that it can be accessed from views.
*
* <p>
* See <tt>views/hudson/plugins/hello_world/HelloWorldBuilder/*.jelly</tt> for the actual HTML
* fragment for the configuration screen.
*/
public static final class DescriptorImpl extends Descriptor<Builder> {
/**
* To persist global configuration information, simply store it in a field and call save().
*
* <p>
* If you don't want fields to be persisted, use <tt>transient</tt>.
*/
private String seleniumRunner;
DescriptorImpl() {
super(SeleniumhqBuilder.class);
load();
}
/**
* Performs on-the-fly validation of the form field 'name'.
*
* @param value
* This receives the current value of the field.
*/
public FormValidation doCheckSeleniumRunner(@QueryParameter final String value) {
return FormValidation.validateExecutable(value);
}
/**
* This human readable name is used in the configuration screen.
*/
public String getDisplayName() {
return "SeleniumHQ htmlSuite Run";
}
@Override
public boolean configure(StaplerRequest req, JSONObject o) throws FormException {
// to persist global configuration information,
// set that to properties and call save().
seleniumRunner = o.getString("seleniumRunner");
save();
return super.configure(req, o);
}
public String getSeleniumRunner() {
return seleniumRunner;
}
/**
* For junit test
*
* @param seleniumRunner
*/
public void setSeleniumRunner(String seleniumRunner) {
this.seleniumRunner = seleniumRunner;
}
public boolean isGoodSeleniumRunner() {
return this.seleniumRunner != null && this.seleniumRunner.length() > 0;
}
}
}