/
ScenarioRuntime.java
559 lines (521 loc) · 21.8 KB
/
ScenarioRuntime.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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*
* The MIT License
*
* Copyright 2022 Karate Labs Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.intuit.karate.core;
import com.intuit.karate.FileUtils;
import com.intuit.karate.LogAppender;
import com.intuit.karate.Logger;
import com.intuit.karate.RuntimeHook;
import com.intuit.karate.ScenarioActions;
import com.intuit.karate.StringUtils;
import com.intuit.karate.graal.JsEngine;
import com.intuit.karate.graal.JsValue;
import com.intuit.karate.http.ResourceType;
import com.intuit.karate.shell.StringLogAppender;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author pthomas3
*/
public class ScenarioRuntime implements Runnable {
public final Logger logger;
public final FeatureRuntime featureRuntime;
public final ScenarioCall caller;
public final Scenario scenario;
public final Tags tags;
public final ScenarioActions actions;
public final ScenarioResult result;
public final ScenarioEngine engine;
public final boolean reportDisabled;
public final Map<String, Object> magicVariables;
public final boolean selectedForExecution;
public final boolean perfMode;
public final boolean dryRun;
public final LogAppender logAppender;
private boolean skipBackground;
private boolean ignoringFailureSteps;
public ScenarioRuntime(FeatureRuntime featureRuntime, Scenario scenario) {
logger = new Logger();
this.featureRuntime = featureRuntime;
this.caller = featureRuntime.caller;
perfMode = featureRuntime.perfHook != null;
if (caller.isNone()) {
logAppender = new StringLogAppender(false);
engine = new ScenarioEngine(caller.getParentConfig(false), this, new HashMap(), logger);
} else if (caller.isSharedScope()) {
logAppender = caller.parentRuntime.logAppender;
engine = new ScenarioEngine(caller.getParentConfig(false), this, caller.getParentVars(false), logger);
} else { // new, but clone and copy data
logAppender = caller.parentRuntime.logAppender;
// in this case, parent variables are set via magic variables - see initMagicVariables()
engine = new ScenarioEngine(caller.getParentConfig(true), this, new HashMap(), logger);
}
logger.setAppender(logAppender);
actions = new ScenarioActions(engine);
this.scenario = scenario;
if (scenario.isDynamic() && !scenario.isOutlineExample()) { // from dynamic scenario iterator
steps = Collections.emptyList();
skipped = true; // ensures run() is a no-op
magicVariables = Collections.emptyMap();
} else {
magicVariables = initMagicVariables();
}
result = new ScenarioResult(scenario);
if (featureRuntime.setupResult != null) {
// TODO improve this and simplify report rendering code in report/karate-feature.html
StepResult sr = result.addFakeStepResult("@setup", null);
List<FeatureResult> list = new ArrayList(1);
FeatureResult fr = new FeatureResult(featureRuntime.featureCall.feature);
fr.setCallDepth(1);
fr.addResult(featureRuntime.setupResult);
list.add(fr);
sr.addCallResults(list);
featureRuntime.setupResult = null;
}
dryRun = featureRuntime.suite.dryRun;
tags = scenario.getTagsEffective();
reportDisabled = perfMode ? true : tags.valuesFor("report").isAnyOf("false");
selectedForExecution = isSelectedForExecution(featureRuntime, scenario, tags);
}
private Map<String, Object> initMagicVariables() {
// magic variables are only in the JS engine - [ see ScenarioEngine.init() ]
// and not "visible" and tracked in ScenarioEngine.vars
// one consequence is that they won't show up in the debug variables view
// but more importantly don't get passed back to caller and float around, bloating memory
Map<String, Object> map = new HashMap();
if (!caller.isNone()) {
// karate principle: parent variables are always "visible"
// so we inject the parent variables
// but they will be over-written by what is local to this scenario
if (!caller.isSharedScope()) {
// shallow clone variables if not shared scope
Map<String, Variable> copy = caller.getParentVars(true);
copy.forEach((k, v) -> map.put(k, v.getValue()));
}
map.putAll(caller.parentRuntime.magicVariables);
map.put("__arg", caller.arg == null ? null : caller.arg.getValue());
map.put("__loop", caller.getLoopIndex());
}
if (scenario.isOutlineExample()) { // init examples row magic variables
Map<String, Object> exampleData = scenario.getExampleData();
map.putAll(exampleData);
map.put("__row", exampleData);
map.put("__num", scenario.getExampleIndex());
}
return map;
}
public boolean isFailed() {
return error != null || result.isFailed();
}
public Step getCurrentStep() {
return currentStep;
}
public boolean isStopped() {
return stopped;
}
public boolean isSkipBackground() {
return this.skipBackground;
}
public void setSkipBackground(boolean skipBackground) {
this.skipBackground = skipBackground;
}
public String getEmbedFileName(ResourceType resourceType) {
String extension = resourceType == null ? null : resourceType.getExtension();
return scenario.getUniqueId() + "_" + System.currentTimeMillis() + (extension == null ? "" : "." + extension);
}
private Embed saveToFileAndCreateEmbed(byte[] bytes, ResourceType resourceType) {
File file = new File(featureRuntime.suite.reportDir + File.separator + getEmbedFileName(resourceType));
FileUtils.writeToFile(file, bytes);
return new Embed(file, resourceType);
}
public Embed embed(byte[] bytes, ResourceType resourceType) {
if (embeds == null) {
embeds = new ArrayList();
}
Embed embed = saveToFileAndCreateEmbed(bytes, resourceType);
embeds.add(embed);
return embed;
}
public Embed embedVideo(File file) {
StepResult stepResult = result.addFakeStepResult("[video]", null);
Embed embed = saveToFileAndCreateEmbed(FileUtils.toBytes(file), ResourceType.MP4);
stepResult.addEmbed(embed);
return embed;
}
private List<FeatureResult> callResults;
public void addCallResult(FeatureResult fr) {
if (callResults == null) {
callResults = new ArrayList();
}
callResults.add(fr);
}
public LogAppender getLogAppender() {
return logAppender;
}
private List<Step> steps;
private List<Embed> embeds;
private StepResult currentStepResult;
private Step currentStep;
private Throwable error;
private boolean configFailed;
private boolean skipped; // beforeScenario hook only
private boolean stopped;
private boolean aborted;
private int stepIndex;
public void stepBack() {
stopped = false;
stepIndex -= 2;
if (stepIndex < 0) {
stepIndex = 0;
}
}
public void stepReset() {
stopped = false;
stepIndex--;
if (stepIndex < 0) { // maybe not required
stepIndex = 0;
}
}
public void stepProceed() {
stopped = false;
}
private int nextStepIndex() {
return stepIndex++;
}
public Result evalAsStep(String expression) {
Step evalStep = new Step(scenario, -1);
try {
evalStep.parseAndUpdateFrom(expression);
} catch (Exception e) {
return Result.failed(System.currentTimeMillis(), 0, e, evalStep);
}
return StepRuntime.execute(evalStep, actions);
}
public boolean hotReload() {
boolean success = false;
Feature feature = scenario.getFeature();
feature = Feature.read(feature.getResource());
for (Step oldStep : steps) {
Step newStep = feature.findStepByLine(oldStep.getLine());
if (newStep == null) {
continue;
}
String oldText = oldStep.getText();
String newText = newStep.getText();
if (!oldText.equals(newText)) {
try {
oldStep.parseAndUpdateFrom(newStep.getText());
logger.info("hot reloaded line: {} - {}", newStep.getLine(), newStep.getText());
success = true;
} catch (Exception e) {
logger.warn("failed to hot reload step: {}", e.getMessage());
}
}
}
return success;
}
public Map<String, Object> getScenarioInfo() {
Map<String, Object> info = new HashMap(5);
File featureFile = featureRuntime.featureCall.feature.getResource().getFile();
if (featureFile != null) {
info.put("featureDir", featureFile.getParent());
info.put("featureFileName", featureFile.getName());
}
info.put("scenarioName", scenario.getName());
info.put("scenarioDescription", scenario.getDescription());
String errorMessage = error == null ? null : error.getMessage();
info.put("errorMessage", errorMessage);
return info;
}
protected void logError(String message) {
if (currentStep != null) {
message = currentStep.getDebugInfo()
+ "\n" + currentStep.toString()
+ "\n" + message;
}
logger.error("{}", message);
}
private void evalConfigJs(String js, String displayName) {
if (js == null || configFailed) {
return;
}
try {
Variable fun = engine.evalJs("(" + js + ")");
if (!fun.isJsFunction()) {
logger.warn("not a valid js function: {}", displayName);
return;
}
Map<String, Object> map = engine.getOrEvalAsMap(fun);
engine.setVariables(map);
} catch (Exception e) {
String message = ">> " + scenario.getDebugInfo() + "\n>> " + displayName + " failed\n>> " + e.getMessage();
error = JsEngine.fromJsEvalException(js, e, message);
stopped = true;
configFailed = true;
}
}
private static boolean isSelectedForExecution(FeatureRuntime fr, Scenario scenario, Tags tags) {
org.slf4j.Logger logger = FeatureRuntime.logger;
int callLine = fr.featureCall.callLine;
if (callLine != -1) {
int sectionLine = scenario.getSection().getLine();
int scenarioLine = scenario.getLine();
if (callLine == sectionLine || callLine == scenarioLine) {
logger.info("found scenario at line: {}", callLine);
return true;
}
logger.trace("skipping scenario at line: {}, needed: {}", scenario.getLine(), callLine);
return false;
}
String callName = fr.featureCall.callName;
if (callName != null) {
if (scenario.getName().matches(callName)) {
logger.info("found scenario at line: {} - {}", scenario.getLine(), callName);
return true;
}
logger.trace("skipping scenario at line: {} - {}, needed: {}", scenario.getLine(), scenario.getName(), callName);
return false;
}
String callTag = fr.featureCall.callTag;
if (callTag != null) {
if (tags.contains(callTag)) {
logger.info("{} - call by tag at line {}: {}", fr, scenario.getLine(), callTag);
return true;
} else {
logger.trace("skipping scenario at line: {} with call by tag effective: {}", scenario.getLine(), callTag);
return false;
}
}
if (fr.caller.isNone()) {
if (tags.evaluate(fr.suite.tagSelector, fr.suite.env)) {
logger.trace("matched scenario at line: {} with tags effective: {}", scenario.getLine(), tags.getTags());
return true;
}
logger.trace("skipping scenario at line: {} with tags effective: {}", scenario.getLine(), tags.getTags());
return false;
} else {
return true; // when called, tags are ignored, all scenarios will be run
}
}
//==========================================================================
//
public void beforeRun() {
if (featureRuntime.caller.isNone() && featureRuntime.suite.isAborted()) {
skipped = true;
return;
}
steps = skipBackground ? scenario.getSteps() : scenario.getStepsIncludingBackground();
ScenarioEngine.set(engine);
engine.init();
result.setExecutorName(Thread.currentThread().getName());
result.setStartTime(System.currentTimeMillis());
if (!dryRun) {
if (caller.isNone() && !caller.isKarateConfigDisabled()) {
// evaluate config js, variables above will apply !
evalConfigJs(featureRuntime.suite.karateBase, "karate-base.js");
evalConfigJs(featureRuntime.suite.karateConfig, "karate-config.js");
evalConfigJs(featureRuntime.suite.karateConfigEnv, "karate-config-" + featureRuntime.suite.env + ".js");
}
skipped = !featureRuntime.suite.hooks.stream()
.map(h -> h.beforeScenario(this))
.reduce(Boolean.TRUE, Boolean::logicalAnd);
if (skipped) {
logger.debug("beforeScenario hook returned false, will skip scenario: {}", scenario);
} else {
evaluateScenarioName();
}
}
}
@Override
public void run() {
try { // make sure we call afterRun() even on crashes
// and operate countdown latches, else we may hang the parallel runner
if (steps == null) {
beforeRun();
}
if (skipped) {
return;
}
int count = steps.size();
int index = 0;
while ((index = nextStepIndex()) < count) {
currentStep = steps.get(index);
execute(currentStep);
if (currentStepResult != null) { // can be null if debug step-back or hook skip
result.addStepResult(currentStepResult);
}
}
} catch (Exception e) {
if (currentStepResult != null) {
result.addStepResult(currentStepResult);
}
logError("scenario [run] failed\n" + StringUtils.throwableToString(e));
currentStepResult = result.addFakeStepResult("scenario [run] failed", e);
} finally {
if (!skipped) {
afterRun();
if (isFailed() && engine.getConfig().isAbortSuiteOnFailure()) {
featureRuntime.suite.abort();
}
}
if (caller.isNone()) {
logAppender.close(); // reclaim memory
}
}
}
public StepResult execute(Step step) {
if (!stopped && !dryRun) {
boolean shouldExecute = true;
for (RuntimeHook hook : featureRuntime.suite.hooks) {
if (!hook.beforeStep(step, this)) {
shouldExecute = false;
}
}
if (!shouldExecute) {
return null;
}
}
Result stepResult;
final boolean executed = !stopped;
if (stopped) {
if (aborted && engine.getConfig().isAbortedStepsShouldPass()) {
stepResult = Result.passed(System.currentTimeMillis(), 0);
} else if (configFailed) {
stepResult = Result.failed(System.currentTimeMillis(), 0, error, step);
} else {
stepResult = Result.skipped(System.currentTimeMillis());
}
} else if (dryRun && !step.isSetup()) {
stepResult = Result.passed(System.currentTimeMillis(), 0);
} else {
stepResult = StepRuntime.execute(step, actions);
}
currentStepResult = new StepResult(step, stepResult);
if (stepResult.isAborted()) { // we log only aborts for visibility
aborted = true;
stopped = true;
logger.debug("abort at {}", step.getDebugInfo());
} else if (stepResult.isFailed()) {
if (stepResult.getMatchingMethod() != null && engine.getConfig().getContinueOnStepFailureMethods().contains(stepResult.getMatchingMethod().method)) {
stopped = false;
ignoringFailureSteps = true;
currentStepResult.setErrorIgnored(true);
} else {
stopped = true;
}
if (stopped && (!this.engine.getConfig().isContinueAfterContinueOnStepFailure() || !engine.isIgnoringStepErrors())) {
error = stepResult.getError();
logError(error.getMessage());
}
if (engine.driver != null) {
engine.driver.onFailure(currentStepResult);
}
if (engine.robot != null) {
engine.robot.onFailure(currentStepResult);
}
} else {
boolean hidden = reportDisabled || (step.isPrefixStar() && !step.isPrint() && !engine.getConfig().isShowAllSteps());
currentStepResult.setHidden(hidden);
}
addStepLogEmbedsAndCallResults();
if (currentStepResult.isErrorIgnored()) {
engine.setFailedReason(null);
}
if (!engine.isIgnoringStepErrors() && ignoringFailureSteps) {
if (engine.getConfig().isContinueAfterContinueOnStepFailure()) {
// continue execution and reset failed reason for engine to null
engine.setFailedReason(null);
ignoringFailureSteps = false;
} else {
// stop execution
// keep failed reason for scenario as the last failed step that was ignored
stopped = true;
}
}
if (executed && !dryRun) {
featureRuntime.suite.hooks.forEach(h -> h.afterStep(currentStepResult, this));
}
return currentStepResult;
}
public void afterRun() {
try {
result.setEndTime(System.currentTimeMillis());
engine.logLastPerfEvent(result.getFailureMessageForDisplay());
if (currentStepResult == null) {
currentStepResult = result.addFakeStepResult("no steps executed", null);
}
if (!dryRun) {
engine.invokeAfterHookIfConfigured(false);
featureRuntime.suite.hooks.forEach(h -> h.afterScenario(this));
engine.stop(currentStepResult);
}
addStepLogEmbedsAndCallResults();
} catch (Exception e) {
error = e;
logError("scenario [cleanup] failed\n" + e.getMessage());
currentStepResult = result.addFakeStepResult("scenario [cleanup] failed", e);
}
}
private void addStepLogEmbedsAndCallResults() {
boolean showLog = !reportDisabled && engine.getConfig().isShowLog();
String stepLog = logAppender.collect();
if (showLog) {
currentStepResult.appendToStepLog(stepLog);
if (currentStepResult.isErrorIgnored()) {
currentStepResult.appendToStepLog(currentStepResult.getErrorMessage());
}
}
if (callResults != null) {
currentStepResult.addCallResults(callResults);
callResults = null;
}
if (embeds != null) {
currentStepResult.addEmbeds(embeds);
embeds = null;
}
}
@Override
public String toString() {
return scenario.toString();
}
public void evaluateScenarioName() {
String scenarioName = scenario.getName();
boolean wrappedByBackTick = scenarioName != null
&& scenarioName.length() > 1
&& '`' == scenarioName.charAt(0)
&& '`' == scenarioName.charAt((scenarioName.length() - 1));
boolean hasJavascriptPlaceholder = ScenarioEngine.hasJavaScriptPlacehoder(scenarioName);
if (wrappedByBackTick || hasJavascriptPlaceholder) {
String eval = scenarioName;
if (!wrappedByBackTick) {
eval = '`' + eval + '`';
}
String evaluatedScenarioName = engine.evalJs(eval).getAsString();
scenario.setName(evaluatedScenarioName);
}
}
}