19
19
20
20
import static org .openqa .selenium .firefox .FirefoxDriver .MARIONETTE ;
21
21
22
+ import com .beust .jcommander .JCommander ;
23
+ import com .beust .jcommander .Parameter ;
22
24
import com .thoughtworks .selenium .Selenium ;
23
25
import com .thoughtworks .selenium .webdriven .WebDriverBackedSelenium ;
24
26
52
54
import java .nio .file .Files ;
53
55
import java .nio .file .Path ;
54
56
import java .nio .file .Paths ;
57
+ import java .util .ArrayList ;
55
58
import java .util .List ;
56
59
import java .util .logging .Level ;
57
60
import java .util .logging .Logger ;
@@ -76,31 +79,16 @@ public class HTMLLauncher {
76
79
* @param suiteURL - the relative URL to the HTML suite
77
80
* @param outputFile - The file to which we'll output the HTML results
78
81
* @param timeoutInSeconds - the amount of time (in seconds) to wait for the browser to finish
79
- * @param multiWindow TODO
80
82
* @return PASS or FAIL
81
83
* @throws IOException if we can't write the output file
82
84
*/
83
- public String runHTMLSuite (String browser , String browserURL , String suiteURL , File outputFile ,
84
- long timeoutInSeconds , boolean multiWindow ) throws IOException {
85
- return runHTMLSuite (browser , browserURL , suiteURL , outputFile ,
86
- timeoutInSeconds , multiWindow , "info" );
87
- }
88
-
89
- /**
90
- * Launches a single HTML Selenium test suite.
91
- *
92
- * @param browser - the browserString ("*firefox", "*iexplore" or an executable path)
93
- * @param browserURL - the start URL for the browser
94
- * @param suiteURL - the relative URL to the HTML suite
95
- * @param outputFile - The file to which we'll output the HTML results
96
- * @param multiWindow TODO
97
- * @param defaultLogLevel TODO
98
- * @param timeoutInSeconds - the amount of time (in seconds) to wait for the browser to finish
99
- * @return PASS or FAIL
100
- * @throws IOException if we can't write the output file
101
- */
102
- private String runHTMLSuite (String browser , String browserURL , String suiteURL , File outputFile ,
103
- long timeoutInSeconds , boolean multiWindow , String defaultLogLevel ) throws IOException {
85
+ public String runHTMLSuite (
86
+ String browser ,
87
+ String browserURL ,
88
+ String suiteURL ,
89
+ File outputFile ,
90
+ long timeoutInSeconds ,
91
+ String userExtensions ) throws IOException {
104
92
File parent = outputFile .getParentFile ();
105
93
if (parent != null && !parent .exists ()) {
106
94
parent .mkdirs ();
@@ -121,6 +109,10 @@ private String runHTMLSuite(String browser, String browserURL, String suiteURL,
121
109
122
110
driver .get (suiteUrl .toString ());
123
111
Selenium selenium = new WebDriverBackedSelenium (driver , browserURL );
112
+ selenium .setTimeout (String .valueOf (timeoutInMs ));
113
+ if (userExtensions != null ) {
114
+ selenium .setExtensionJs (userExtensions );
115
+ }
124
116
List <WebElement > allTables = driver .findElements (By .id ("suiteTable" ));
125
117
if (allTables .isEmpty ()) {
126
118
throw new RuntimeException ("Unable to find suite table: " + driver .getPageSource ());
@@ -211,31 +203,46 @@ private URL verifySuiteUrl(URL url) throws IOException {
211
203
}
212
204
213
205
public static int mainInt (String ... args ) throws Exception {
214
- if (args .length != 4 ) {
215
- throw new IllegalAccessException (
216
- "Usage: HTMLLauncher outputDir testSuite startUrl browser" );
206
+ Args processed = new Args ();
207
+ JCommander jCommander = new JCommander (processed );
208
+ jCommander .setCaseSensitiveOptions (false );
209
+ jCommander .parse (args );
210
+
211
+ if (processed .help ) {
212
+ StringBuilder help = new StringBuilder ();
213
+ jCommander .usage (help );
214
+ System .err .print (help );
215
+ return 0 ;
217
216
}
218
217
219
- File dir = new File (args [0 ]);
220
- if (!dir .exists () && !dir .mkdirs ()) {
221
- throw new RuntimeException ("Cannot create output directory for: " + dir );
218
+ if (!validateArgs (processed )) {
219
+ return -1 ;
222
220
}
223
221
224
- String suite = args [1 ];
225
- String startURL = args [2 ];
226
- String [] browsers ;
227
- browsers = new String [] {args [3 ]};
222
+ Path resultsPath = Paths .get (processed .htmlSuite .get (3 ));
223
+ Files .createDirectories (resultsPath );
224
+
225
+ String suite = processed .htmlSuite .get (2 );
226
+ String startURL = processed .htmlSuite .get (1 );
227
+ String [] browsers = new String [] {processed .htmlSuite .get (0 )};
228
228
229
229
HTMLLauncher launcher = new HTMLLauncher ();
230
230
231
231
boolean passed = true ;
232
232
for (String browser : browsers ) {
233
233
// Turns out that Windows doesn't like "*" in a path name
234
- File results = new File ( dir , browser .substring (1 ) + ".results" );
234
+ File results = resultsPath . resolve ( browser .substring (1 ) + ".results" ). toFile ( );
235
235
String result = "FAILED" ;
236
236
237
237
try {
238
- result = launcher .runHTMLSuite (browser , startURL , suite , results , 600 , true );
238
+ long timeout ;
239
+ try {
240
+ timeout = Long .parseLong (processed .timeout );
241
+ } catch (NumberFormatException e ) {
242
+ System .err .println ("Timeout does not appear to be a number: " + processed .timeout );
243
+ return -2 ;
244
+ }
245
+ result = launcher .runHTMLSuite (browser , startURL , suite , results , timeout , processed .userExtensions );
239
246
passed &= "PASSED" .equals (result );
240
247
} catch (Throwable e ) {
241
248
log .log (Level .WARNING , "Test of browser failed: " + browser , e );
@@ -246,6 +253,22 @@ public static int mainInt(String... args) throws Exception {
246
253
return passed ? 1 : 0 ;
247
254
}
248
255
256
+ private static boolean validateArgs (Args processed ) {
257
+ if (processed .multiWindow ) {
258
+ System .err .println ("Multi-window mode is longer used as an option and will be ignored." );
259
+ }
260
+
261
+ if (processed .port != 0 ) {
262
+ System .err .println ("Port is longer used as an option and will be ignored." );
263
+ }
264
+
265
+ if (processed .trustAllSSLCertificates ) {
266
+ System .err .println ("Trusting all ssl certificates is no longer a user-settable option." );
267
+ }
268
+
269
+ return true ;
270
+ }
271
+
249
272
public static void main (String [] args ) throws Exception {
250
273
System .exit (mainInt (args ));
251
274
}
@@ -285,4 +308,45 @@ private WebDriver createDriver(String browser) {
285
308
throw new RuntimeException ("Unrecognized browser: " + browser );
286
309
}
287
310
}
311
+
312
+ public static class Args {
313
+ @ Parameter (
314
+ names = "-htmlSuite" ,
315
+ required = true ,
316
+ arity = 4 ,
317
+ description = "Run an HTML Suite: '*browser' 'http://baseUrl.com' 'path\\ to\\ HTMLSuite.html' 'c:\\ absolute\\ path\\ to\\ my\\ results.html'" )
318
+ private List <String > htmlSuite ;
319
+
320
+ @ Parameter (
321
+ names = "-timeout" ,
322
+ description = "Timeout to use in seconds" )
323
+ private String timeout = "30" ;
324
+
325
+ @ Parameter (
326
+ names = "-userExtensions" ,
327
+ description = "User extensions to attempt to use."
328
+ )
329
+ private String userExtensions ;
330
+
331
+ @ Parameter (
332
+ names = "-multiwindow" ,
333
+ hidden = true )
334
+ private boolean multiWindow = true ;
335
+
336
+ @ Parameter (
337
+ names = "-port" ,
338
+ hidden = true )
339
+ private Integer port ;
340
+
341
+ @ Parameter (
342
+ names = "-trustAllSSLCertificates" ,
343
+ hidden = true )
344
+ private boolean trustAllSSLCertificates ;
345
+
346
+ @ Parameter (
347
+ names = {"-help" , "--help" , "-h" },
348
+ description = "This help message" ,
349
+ help = true )
350
+ private boolean help ;
351
+ }
288
352
}
0 commit comments