/
JLineup.java
403 lines (325 loc) · 17 KB
/
JLineup.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
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
package de.otto.jlineup.cli;
import de.otto.jlineup.GlobalOption;
import de.otto.jlineup.GlobalOptions;
import de.otto.jlineup.JLineupRunner;
import de.otto.jlineup.RunStepConfig;
import de.otto.jlineup.browser.Browser;
import de.otto.jlineup.browser.BrowserUtils;
import de.otto.jlineup.browser.CloudBrowserFactory;
import de.otto.jlineup.config.ConfigMerger;
import de.otto.jlineup.config.JobConfig;
import de.otto.jlineup.config.RunStep;
import de.otto.jlineup.exceptions.ValidationError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Paths;
import java.util.*;
import java.util.List;
import java.util.concurrent.Callable;
import static de.otto.jlineup.cli.Main.NO_EXIT;
import static de.otto.jlineup.cli.Utils.convertCommandLineParametersToRunConfiguration;
import static de.otto.jlineup.cli.Utils.readConfig;
import static de.otto.jlineup.file.FileService.REPORT_HTML_FILENAME;
import static java.lang.invoke.MethodHandles.lookup;
@Command(name = "jlineup", sortOptions = false, modelTransformer = JLineup.OptionsFilter.class)
public class JLineup implements Callable<Integer> {
private final static Logger LOG = LoggerFactory.getLogger(lookup().lookupClass());
@Option(names = {"-?", "--help"}, usageHelp = true, description = "Shows this help. See https://github.com/otto-de/jlineup/blob/main/README.md for full docs.", order = 0)
private boolean help = false;
@Option(names = {"-u", "--url"}, description = "If you run JLineup without config file, this is the one url that is tested with the default config.", order = 10)
private String url = null;
@Option(names = {"-s", "--step"}, description = "JLineup step - 'before' just takes screenshots, 'after' takes screenshots and compares them with the 'before'-screenshots in the screenshots directory. 'compare' just compares existing screenshots, it's also included in 'after'.", order = 20)
private RunStep step = RunStep.before;
@Option(names = {"-c", "--config"}, description = "The job config file which contains the url(s) and the settings for the JLineup run. See https://github.com/otto-de/jlineup/blob/main/docs/CONFIGURATION.md for all configuration options.", order = 30)
private String configFile = "lineup.json";
@Option(names = {"-m", "--merge-config"}, description = "(Preview Feature) Additional config that will be merged with the given config file. Identical local values have precedence. URL keys are interpreted as regex matchers.", order = 40)
private String mergeConfigFile = null;
@Option(names = {"-d", "--working-dir"}, description = "Path to the working directory", order = 50)
private String workingDirectory = ".";
@Option(names = {"-sd", "--screenshot-dir"}, description = "Screenshots directory name - relative to working directory", order = 60)
private String screenshotDirectory = "report/screenshots";
@Option(names = {"-rd", "--report-dir"}, description = "HTML report directory name - relative to working directory", order = 70)
private String reportDirectory = "report";
@Option(names = {"--print-config"}, description = "Prints the current (if found) or a default config file to standard out.", order = 80)
private boolean printConfig = false;
@Option(names = {"--print-example"}, description = "Prints an example default config file to standard out. Useful as quick start.", order = 90)
private boolean printExample = false;
@Option(names = {"--debug"}, description = "Sets the log level to DEBUG, produces verbose information about the current task.", order = 100)
private boolean debug = false;
@Option(names = {"--log"}, description = "Sets the log level to DEBUG and logs to a file in the current working directory.", order = 110)
private boolean logToFile = false;
@Option(names = {"-v", "--version"}, description = "Prints version information.", order = 120)
private boolean version = false;
@Option(names = {"--chrome-parameter"}, description = "Additional command line parameters for spawned chrome processes. Example: --chrome-parameter \"--use-shm=false\"", order = 130)
private List<String> chromeParameters;
@Option(names = {"--firefox-parameter"}, description = "Additional command line parameters for spawned firefox processes.", order = 140)
private List<String> firefoxParameters;
@Option(names = {"-R", "--replace-in-url"}, description = "The given keys are replaced with the corresponding values in all urls that are tested.", order = 150)
private Map<String, String> urlReplacements = new HashMap<>();
@Option(names = {"-o", "--open-report"}, description = "Opens html report after the run.", order = 160)
private boolean openReport = false;
@Option(names = {"-k", "--keep-existing"}, description = "(Preview feature) Keep existing 'before' screenshots after having added new urls or paths to the config.", order = 170)
private boolean keepExisting = false;
@Option(names = {"--refresh-url"}, description = "(Preview feature) Refresh 'before' screenshots for the given url only. Implicitly sets '--keep-existing' also.", order = 180)
private String refreshUrl = null;
@Option(names = {"-b", "--override-browser"}, description = "(Preview feature) Override browser setting in run config.", order = 190)
private String browserOverride = null;
@Option(names = {"--cleanup-profile"}, description = "Cleanup browser profile directory after the run has finished and a profile dir was specified with the browser parameters.", order = 200)
private boolean cleanupProfile = false;
/**
* This options filter adds the lambda options to the command spec if the LambdaBrowser is available in the classpath.
*/
static class OptionsFilter implements CommandLine.IModelTransformer {
@Override
public CommandLine.Model.CommandSpec transform(CommandLine.Model.CommandSpec commandSpec) {
try {
Class<?> lambdaBrowserClass = Class.forName("de.otto.jlineup.lambda.LambdaBrowser", false, CloudBrowserFactory.class.getClassLoader());
LOG.debug("LambdaBrowser '{}' reachable, adding lambda options to command spec.", lambdaBrowserClass.getCanonicalName());
commandSpec.addOption(CommandLine.Model.OptionSpec.builder("-F", "--" + GlobalOption.JLINEUP_LAMBDA_FUNCTION_NAME.kebabCaseName())
.order(210).description("This specifies the name or the arn of the AWS lambda function")
.parameterConsumer((stack, argSpec, commandSpec1) -> {
String value = stack.pop();
GlobalOptions.setOption(GlobalOption.JLINEUP_LAMBDA_FUNCTION_NAME, value);
System.err.println("Function name set: " + GlobalOptions.asString());
})
.build());
commandSpec.addOption(CommandLine.Model.OptionSpec.builder("-P", "--" + GlobalOption.JLINEUP_LAMBDA_AWS_PROFILE.kebabCaseName())
.order(220).description("The AWS profile for calling the lambda function and for accessing S3")
.parameterConsumer((stack, argSpec, commandSpec1) -> {
String value = stack.pop();
GlobalOptions.setOption(GlobalOption.JLINEUP_LAMBDA_AWS_PROFILE, value);
System.err.println("Profile set: " + GlobalOptions.asString());
})
.build());
} catch (ClassNotFoundException e) {
LOG.debug("No LambdaBrowser reachable.", e);
}
return commandSpec;
}
}
public JLineup() {
}
public String getWorkingDirectory() {
return workingDirectory;
}
public String getScreenshotDirectory() {
return screenshotDirectory;
}
public String getReportDirectory() {
return reportDirectory;
}
String getConfigFile() {
return configFile;
}
public String getMergeConfigFile() {
return mergeConfigFile;
}
public boolean isAfter() {
return step == RunStep.after;
}
public boolean isBefore() {
return step != RunStep.after && step != RunStep.compare && step != RunStep.after_only;
}
public boolean isJustCompare() {
return step == RunStep.compare;
}
public boolean isHelp() {
return help;
}
public RunStep getStep() {
return step;
}
public Map<String, String> getUrlReplacements() {
return urlReplacements;
}
public String getUrl() {
return url;
}
public boolean isPrintConfig() {
return printConfig;
}
public boolean isDebug() {
return debug;
}
public boolean isLogToFile() {
return logToFile;
}
public boolean isVersion() {
return version;
}
public List<String> getChromeParameters() {
return chromeParameters;
}
public List<String> getFirefoxParameters() {
return firefoxParameters;
}
public boolean isPrintExample() {
return printExample;
}
public boolean isOpenReport() {
return openReport;
}
public boolean isKeepExisting() {
return keepExisting;
}
public String getRefreshUrl() {
return refreshUrl;
}
public String getBrowserOverride() {
return browserOverride;
}
public boolean isCleanupProfile() {
return cleanupProfile;
}
public void setCleanupProfile(boolean cleanupProfile) {
this.cleanupProfile = cleanupProfile;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
JLineup jLineup = (JLineup) o;
return help == jLineup.help && printConfig == jLineup.printConfig && printExample == jLineup.printExample && debug == jLineup.debug && logToFile == jLineup.logToFile && version == jLineup.version && openReport == jLineup.openReport && keepExisting == jLineup.keepExisting && cleanupProfile == jLineup.cleanupProfile && Objects.equals(url, jLineup.url) && step == jLineup.step && Objects.equals(configFile, jLineup.configFile) && Objects.equals(mergeConfigFile, jLineup.mergeConfigFile) && Objects.equals(workingDirectory, jLineup.workingDirectory) && Objects.equals(screenshotDirectory, jLineup.screenshotDirectory) && Objects.equals(reportDirectory, jLineup.reportDirectory) && Objects.equals(chromeParameters, jLineup.chromeParameters) && Objects.equals(firefoxParameters, jLineup.firefoxParameters) && Objects.equals(urlReplacements, jLineup.urlReplacements) && Objects.equals(refreshUrl, jLineup.refreshUrl) && Objects.equals(browserOverride, jLineup.browserOverride);
}
@Override
public int hashCode() {
return Objects.hash(help, url, step, configFile, mergeConfigFile, workingDirectory, screenshotDirectory, reportDirectory, printConfig, printExample, debug, logToFile, version, chromeParameters, firefoxParameters, urlReplacements, openReport, keepExisting, refreshUrl, browserOverride, cleanupProfile);
}
@Override
public String toString() {
return "JLineup{" +
"help=" + help +
", url='" + url + '\'' +
", step=" + step +
", configFile='" + configFile + '\'' +
", mergeConfigFile='" + mergeConfigFile + '\'' +
", workingDirectory='" + workingDirectory + '\'' +
", screenshotDirectory='" + screenshotDirectory + '\'' +
", reportDirectory='" + reportDirectory + '\'' +
", printConfig=" + printConfig +
", printExample=" + printExample +
", debug=" + debug +
", logToFile=" + logToFile +
", version=" + version +
", chromeParameters=" + chromeParameters +
", firefoxParameters=" + firefoxParameters +
", urlReplacements=" + urlReplacements +
", openReport=" + openReport +
", keepExisting=" + keepExisting +
", refreshUrl='" + refreshUrl + '\'' +
", browserOverride='" + browserOverride + '\'' +
", cleanupProfile=" + cleanupProfile +
'}';
}
@Override
public Integer call() throws Exception {
if (help) {
CommandLine.usage(new JLineup(), System.out);
LOG.info("Version: {}\n", de.otto.jlineup.Utils.getVersion());
return NO_EXIT;
}
if (version) {
LOG.info("JLineup version {}", de.otto.jlineup.Utils.getVersion());
return NO_EXIT;
}
if (debug) {
de.otto.jlineup.Utils.setLogLevelToDebug();
}
if (printExample) {
System.out.println(JobConfig.prettyPrintWithAllFields(JobConfig.exampleConfig()));
return 0;
}
//If refresh url is set, keepExisting is mandatory!
if (getRefreshUrl() != null) {
keepExisting = true;
}
if (step != RunStep.before && isKeepExisting()) {
LOG.warn("The --keep-existing option is only usable in combination with the 'before' step. It is ignored for step '{}'.", step);
}
JobConfig jobConfig = null;
try {
jobConfig = buildConfig(this);
} catch (IOException e) {
LOG.error("Error building config.", e);
return 1;
}
if (getMergeConfigFile() != null) {
JobConfig mergeConfig = JobConfig.readConfig(workingDirectory, getMergeConfigFile());
jobConfig = ConfigMerger.mergeJobConfigWithMergeConfig(jobConfig, mergeConfig);
}
if (jobConfig.mergeConfig != null) {
JobConfig mainGlobalConfig = JobConfig.copyOfBuilder(jobConfig).withMergeConfig(null).build();
JobConfig mergeGlobalConfig = jobConfig.mergeConfig;
jobConfig = ConfigMerger.mergeJobConfigWithMergeConfig(mainGlobalConfig, mergeGlobalConfig);
}
jobConfig = jobConfig.insertDefaults();
if (browserOverride != null) {
jobConfig = JobConfig.copyOfBuilder(jobConfig).withBrowser(Browser.Type.forValue(browserOverride)).build();
}
if (printConfig) {
System.out.println(JobConfig.prettyPrint(jobConfig));
return 0;
}
if (jobConfig.debug) {
de.otto.jlineup.Utils.setLogLevelToDebug();
}
if (jobConfig.logToFile || logToFile) {
de.otto.jlineup.Utils.logToFile(workingDirectory);
}
LOG.info("Running JLineup [{}] with step '{}'.\n\n", de.otto.jlineup.Utils.getVersion(), step);
RunStepConfig runStepConfig = convertCommandLineParametersToRunConfiguration(this);
JLineupRunner jLineupRunner = null;
try {
jLineupRunner = new JLineupRunner(jobConfig, runStepConfig);
} catch (ValidationError e) {
LOG.error(e.getMessage());
return 1;
}
try {
boolean runSucceeded = jLineupRunner.run();
if (openReport) {
Desktop.getDesktop().browse(new URI("file://" + Paths.get(String.format("%s/%s/%s", runStepConfig.getWorkingDirectory(), runStepConfig.getReportDirectory(), REPORT_HTML_FILENAME)).toAbsolutePath()));
}
if (!runSucceeded) {
return 1;
}
} catch (Exception e) {
LOG.error(e.getMessage(), e);
de.otto.jlineup.Utils.writeInfosForCommonErrors(e.getMessage());
return 1;
}
return NO_EXIT;
}
private JobConfig buildConfig(JLineup parameters) throws IOException {
JobConfig jobConfig;
if (parameters.getUrl() != null) {
String url = BrowserUtils.prependHTTPIfNotThereAndToLowerCase(parameters.getUrl());
jobConfig = JobConfig.defaultConfig(url);
if (!parameters.isPrintConfig()) {
LOG.info("You specified an explicit URL parameter ({}), any given jobConfig file is ignored! This should only be done for testing purposes.", url);
LOG.info("Using generated jobConfig:\n\n{}", JobConfig.prettyPrint(jobConfig));
LOG.info("\nYou can take this generated jobConfig as base and save it as a text file named 'lineup.json'.");
LOG.info("Just add --print-config parameter to let JLineup print a more detailed example jobConfig");
}
} else {
try {
jobConfig = readConfig(parameters);
} catch (Exception e) {
if (!parameters.isPrintConfig()) {
LOG.error(e.getMessage());
LOG.error("Use --help to see the JLineup quick help.");
throw e;
} else {
return JobConfig.exampleConfig();
}
}
}
return jobConfig;
}
}