Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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 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 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 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 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 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 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);
}
public 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 callResults;
public void addCallResult(FeatureResult fr) {
if (callResults == null) {
callResults = new ArrayList();
}
callResults.add(fr);
}
public LogAppender getLogAppender() {
return logAppender;
}
private List steps;
private List