/
YUITestSeleniumDriver.java
264 lines (216 loc) · 10.5 KB
/
YUITestSeleniumDriver.java
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
/*
* YUI Test
* Author: Nicholas C. Zakas <nzakas@yahoo-inc.com>
* Copyright (c) 2009, Yahoo! Inc. All rights reserved.
* Code licensed under the BSD License:
* http://developer.yahoo.net/yui/license.txt
*/
package com.yahoo.platform.yuitest.selenium;
import com.yahoo.platform.yuitest.config.TestPageGroup;
import com.yahoo.platform.yuitest.config.TestPage;
import com.yahoo.platform.yuitest.config.TestConfig;
import jargs.gnu.CmdLineParser;
import java.io.*;
import java.net.URL;
import java.util.Date;
import java.util.Properties;
/**
* Main YUI Test Coverage class.
* @author Nicholas C. Zakas
*/
public class YUITestSeleniumDriver {
public static void main(String args[]) {
//----------------------------------------------------------------------
// Initialize command line parser
//----------------------------------------------------------------------
CmdLineParser parser = new CmdLineParser();
CmdLineParser.Option verboseOpt = parser.addBooleanOption('v', "verbose");
CmdLineParser.Option helpOpt = parser.addBooleanOption('h', "help");
CmdLineParser.Option errorOnFailOpt = parser.addBooleanOption("erroronfail");
CmdLineParser.Option silentOpt = parser.addBooleanOption("silent");
CmdLineParser.Option confOpt = parser.addStringOption("conf");
CmdLineParser.Option hostOpt = parser.addStringOption("host");
CmdLineParser.Option portOpt = parser.addStringOption("port");
CmdLineParser.Option browsersOpt = parser.addStringOption("browsers");
CmdLineParser.Option testsOpt = parser.addStringOption("tests");
CmdLineParser.Option resultsDirOpt = parser.addStringOption("resultsdir");
CmdLineParser.Option coverageDirOpt = parser.addStringOption("coveragedir");
Reader in = null;
Writer out = null;
boolean verbose = false;
try {
parser.parse(args);
//Help option
Boolean help = (Boolean) parser.getOptionValue(helpOpt);
if (help != null && help.booleanValue()) {
usage();
System.exit(0);
}
//Verbose option
verbose = parser.getOptionValue(verboseOpt) != null;
//load default properties from configuration file
Properties properties = new Properties();
properties.load(YUITestSeleniumDriver.class.getResourceAsStream("default.properties"));
//conf option
String confFile = (String) parser.getOptionValue(confOpt);
if (confFile != null){
if (verbose){
System.err.println("[INFO] Loading configuration properties from " + confFile);
}
properties.load(new FileInputStream(confFile));
}
//load all command-line properties, which override everything else
//silent option
boolean silent = parser.getOptionValue(silentOpt) != null;
if (silent){
properties.setProperty(SeleniumDriver.CONSOLE_MODE, "silent");
}
//host option
String host = (String) parser.getOptionValue(hostOpt);
if (host != null){
properties.setProperty(SeleniumDriver.SELENIUM_HOST, host);
if (verbose){
System.err.println("[INFO] Using command line value for " + SeleniumDriver.SELENIUM_HOST + ": " + host);
}
}
//port option
String port = (String) parser.getOptionValue(portOpt);
if (port != null){
properties.setProperty(SeleniumDriver.SELENIUM_PORT, port);
if (verbose){
System.err.println("[INFO] Using command line value for " + SeleniumDriver.SELENIUM_PORT + ": " + port);
}
}
//browsers option
String browsers = (String) parser.getOptionValue(browsersOpt);
if (browsers != null){
properties.setProperty(SeleniumDriver.SELENIUM_BROWSERS, browsers);
if (verbose){
System.err.println("[INFO] Using command line value for " + SeleniumDriver.SELENIUM_BROWSERS + ": " + browsers);
}
}
//results directory option
String resultsDir = (String) parser.getOptionValue(resultsDirOpt);
if (resultsDir != null){
properties.setProperty(SeleniumDriver.RESULTS_OUTPUTDIR, resultsDir);
if (verbose){
System.err.println("[INFO] Using command line value for " + SeleniumDriver.RESULTS_OUTPUTDIR + ": " + resultsDir);
}
}
//coverage directory option
String coverageDir = (String) parser.getOptionValue(coverageDirOpt);
if (coverageDir != null){
properties.setProperty(SeleniumDriver.COVERAGE_OUTPUTDIR, coverageDir);
if (verbose){
System.err.println("[INFO] Using command line value for " + SeleniumDriver.COVERAGE_OUTPUTDIR + ": " + coverageDir);
}
}
//erroronfail option
if (parser.getOptionValue(errorOnFailOpt) != null){
properties.setProperty(SeleniumDriver.ERROR_ON_FAIL, "1");
if (verbose){
System.err.println("[INFO] Using command line value for " + SeleniumDriver.ERROR_ON_FAIL + ": 1 (enabled)");
}
}
//create a new selenium driver with the properties
SeleniumDriver driver = new SeleniumDriver(properties, verbose);
SessionResult[] results = null;
//if --tests is specified, run just those tests
String testFile = (String) parser.getOptionValue(testsOpt);
//if there's nothing on the command line, check the properties file
if (testFile == null){
testFile = properties.getProperty(SeleniumDriver.YUITEST_TESTS_FILE, null);
}
//figure out what to do
if (testFile != null){
TestConfig config = new TestConfig();
if (testFile.startsWith("http://")){ //it's a URL
config.load((new URL(testFile)).openStream());
} else { //it's a local file
config.load(new FileInputStream(testFile));
}
if (verbose){
System.err.println("[INFO] Using tests from " + testFile + ".");
}
results = driver.runTests(config);
} else {
//see if there are any test files
String[] testFiles = parser.getRemainingArgs();
if (testFiles.length > 0){
if (verbose){
System.err.println("[INFO] Using tests from command line.");
}
TestPageGroup group = new TestPageGroup("",
Integer.parseInt(properties.getProperty(SeleniumDriver.YUITEST_VERSION)),
Integer.parseInt(properties.getProperty(SeleniumDriver.YUITEST_TIMEOUT)));
for (int i=0; i < testFiles.length; i++){
TestPage page = new TestPage(testFiles[i],
Integer.parseInt(properties.getProperty(SeleniumDriver.YUITEST_VERSION)),
Integer.parseInt(properties.getProperty(SeleniumDriver.YUITEST_TIMEOUT)));
group.add(page);
}
results = driver.runTests(group);
} else {
if (verbose){
System.err.println("[INFO] No tests specified to run, exiting...");
}
}
}
//output result files
if (results != null){
SessionResultFileGenerator generator = new SessionResultFileGenerator(properties, verbose);
generator.generateAll(results, new Date());
}
//verify that there were no errors
if (driver.getErrors().length > 0){
throw new Exception(driver.getErrors()[0]);
}
} catch (CmdLineParser.OptionException e) {
usage();
System.exit(1);
} catch (Exception e) {
System.err.println("[ERROR] " + e.getMessage());
if (verbose){
e.printStackTrace();
}
System.exit(1);
// } catch (Exception e) {
//
// e.printStackTrace();
// // Return a special error code used specifically by the web front-end.
// System.exit(2);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static void usage() {
System.out.println(
"\nUsage: java -jar yuitest-selenium-driver-x.y.z.jar [options] [test files]\n\n"
+ "Global Options\n"
+ " -h, --help Displays this information.\n"
+ " --browsers <browsers> Run tests in these browsers (comma-delimited).\n"
+ " --conf <file> Load options from <file>.\n"
+ " --coveragedir <dir> Output coverage files to <dir>.\n"
+ " --erroronfail Indicates that a test failure should cause\n"
+ " an error to be reported to the console.\n"
+ " --host <host> Use the Selenium host <host>.\n"
+ " --port <port> Use <port> port on the Selenium host.\n"
+ " --resultsdir <dir> Output test result files to <dir>.\n"
+ " --silent Don't output test results to the console.\n"
+ " --tests <file> Loads test info from <file>.\n"
+ " -v, --verbose Display informational messages and warnings.\n\n");
}
}