Skip to content

Commit

Permalink
log collection for dynamic scenario outline #1003
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Dec 20, 2019
1 parent 3bf5bea commit 11165fd
Show file tree
Hide file tree
Showing 17 changed files with 38 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3153,7 +3153,7 @@ Operation | Description
--------- | -----------
<a name="karate-abort"><code>karate.abort()</code></a> | you can prematurely exit a `Scenario` by combining this with [conditional logic](#conditional-logic) like so: `* if (condition) karate.abort()` - please use [sparingly](https://martinfowler.com/articles/nonDeterminism.html) ! and also see [`configure abortedStepsShouldPass`](#configure)
<a name="karate-append"><code>karate.append(... items)</code></a> | useful to create lists out of items (which can be lists as well), see [JSON transforms](#json-transforms)
<a name="karate-appendto"><code>karate.appendTo(name, ... items)</code></a> | useful to append to a list-like variable (that has to exist) in scope, see [JSON transforms](#json-transforms)
<a name="karate-appendto"><code>karate.appendTo(name, ... items)</code></a> | useful to append to a list-like variable (that has to exist) in scope, see [JSON transforms](#json-transforms) - the first argument can be a reference to an array-like variable or even the name (string) of an existing variable which is list-like
<a name="karate-call"><code>karate.call(fileName, [arg])</code></a> | invoke a [`*.feature` file](#calling-other-feature-files) or a [JavaScript function](#calling-javascript-functions) the same way that [`call`](#call) works (with an optional solitary argument)
<a name="karate-callsingle"><code>karate.callSingle(fileName, [arg])</code></a> | like the above, but guaranteed to run **only once** even across multiple features *and* parallel threads (recommended only for advanced users) - refer to this example: [`karate-config.js`](karate-demo/src/test/java/karate-config.js) / [`headers-single.feature`](karate-demo/src/test/java/demo/headers/headers-single.feature)
<a name="karate-configure"><code>karate.configure(key, value)</code></a> | does the same thing as the [`configure`](#configure) keyword, and a very useful example is to do `karate.configure('connectTimeout', 5000);` in [`karate-config.js`](#configuration) - which has the 'global' effect of not wasting time if a connection cannot be established within 5 seconds
Expand Down
14 changes: 7 additions & 7 deletions karate-core/src/main/java/com/intuit/karate/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ public class Logger {
// not static, has to be per thread
private final DateFormat dateFormatter = new SimpleDateFormat("HH:mm:ss.SSS");

private LogAppender logAppender = LogAppender.NO_OP;
private LogAppender appender = LogAppender.NO_OP;

private boolean appendOnly;

public void setLogAppender(LogAppender logAppender) {
this.logAppender = logAppender;
public void setAppender(LogAppender appender) {
this.appender = appender;
}

public LogAppender getLogAppender() {
return logAppender;
public LogAppender getAppender() {
return appender;
}

public boolean isTraceEnabled() {
Expand Down Expand Up @@ -133,7 +133,7 @@ private String getFormattedDate() {
}

private void formatAndAppend(String format, Object... arguments) {
if (logAppender == null) {
if (appender == null) {
return;
}
FormattingTuple tp = MessageFormatter.arrayFormat(format, arguments);
Expand All @@ -143,7 +143,7 @@ private void formatAndAppend(String format, Object... arguments) {
private void append(String message) {
StringBuilder buf = new StringBuilder();
buf.append(getFormattedDate()).append(' ').append(message).append('\n');
logAppender.append(buf.toString());
appender.append(buf.toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@
*/
public class ScenarioContext {

public final Logger logger;
public final LogAppender appender;
// public but mutable, just for dynamic scenario outline, see who calls setLogger()
public Logger logger;
public LogAppender appender;

public final ScriptBindings bindings;
public final int callDepth;
public final boolean reuseParentContext;
Expand Down Expand Up @@ -127,6 +129,11 @@ public class ScenarioContext {
// websocket
private List<WebSocketClient> webSocketClients;

public void setLogger(Logger logger) {
this.logger = logger;
this.appender = logger.getAppender();
}

public void logLastPerfEvent(String failureMessage) {
if (prevPerfEvent != null && executionHooks != null) {
if (failureMessage != null) {
Expand Down Expand Up @@ -265,7 +272,7 @@ public ScenarioContext(FeatureContext featureContext, CallContext call, ClassLoa
if (appender == null) {
appender = LogAppender.NO_OP;
}
logger.setLogAppender(appender);
logger.setAppender(appender);
this.appender = appender;
callDepth = call.callDepth;
reuseParentContext = call.reuseParentContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.intuit.karate.FileUtils;
import com.intuit.karate.LogAppender;
import com.intuit.karate.Logger;
import com.intuit.karate.StepActions;
import com.intuit.karate.StringUtils;
import com.intuit.karate.shell.FileLogAppender;
Expand Down Expand Up @@ -57,11 +58,6 @@ public class ScenarioExecutionUnit implements Runnable {

private LogAppender appender;

// for UI
public void setAppender(LogAppender appender) {
this.appender = appender;
}

// for debug
public Step getCurrentStep() {
return currentStep;
Expand Down Expand Up @@ -138,6 +134,10 @@ public void init() {
initFailed = true;
result.addError("scenario init failed", e);
}
} else { // dynamic scenario outline, hack to swap logger for current thread
Logger logger = new Logger();
logger.setAppender(appender);
actions.context.setLogger(logger);
}
// this is not done in the constructor as we need to be on the "executor" thread
hooks = exec.callContext.resolveHooks();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public boolean beforeScenario(Scenario scenario, ScenarioContext context) {
handler.THREADS.put(id, this);
}
appender = context.appender;
context.logger.setLogAppender(this); // wrap
context.logger.setAppender(this); // wrap
return true;
}

Expand All @@ -137,7 +137,7 @@ public void afterScenario(ScenarioResult result, ScenarioContext context) {
if (context.callDepth == 0) {
handler.THREADS.remove(id);
}
context.logger.setLogAppender(appender); // unwrap
context.logger.setAppender(appender); // unwrap
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public DriverOptions(ScenarioContext context, Map<String, Object> options, LogAp
this.options = options;
this.appender = appender;
logger = new Logger(getClass());
logger.setLogAppender(appender);
logger.setAppender(appender);
timeout = get("timeout", DEFAULT_TIMEOUT);
type = get("type", null);
start = get("start", true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static AndroidDriver start(ScenarioContext context, Map<String, Object> m
options.arg("--port=" + options.port);
Command command = options.startProcess();
String urlBase = "http://" + options.host + ":" + options.port + "/wd/hub";
Http http = Http.forUrl(options.driverLogger.getLogAppender(), urlBase);
Http http = Http.forUrl(options.driverLogger.getAppender(), urlBase);
http.config("readTimeout","120000");
String sessionId = http.path("session")
.post(Collections.singletonMap("desiredCapabilities", map))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static Chrome start(ScenarioContext context, Map<String, Object> map, Log
}
Command command = options.startProcess();
String url = "http://" + options.host + ":" + options.port;
Http http = Http.forUrl(options.driverLogger.getLogAppender(), url);
Http http = Http.forUrl(options.driverLogger.getAppender(), url);
Http.Response res = http.path("json").get();
if (res.body().asList().isEmpty()) {
if (command != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static ChromeWebDriver start(ScenarioContext context, Map<String, Object>
options.arg("--user-data-dir=" + options.workingDirPath);
Command command = options.startProcess();
String urlBase = "http://" + options.host + ":" + options.port;
Http http = Http.forUrl(options.driverLogger.getLogAppender(), urlBase);
Http http = Http.forUrl(options.driverLogger.getAppender(), urlBase);
String sessionId = http.path("session")
.post(options.getCapabilities())
.jsonPath("get[0] response..sessionId").asString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static EdgeDevToolsDriver start(ScenarioContext context, Map<String, Obje
options.arg(options.port + "");
options.arg("about:blank");
Command command = options.startProcess();
Http http = Http.forUrl(options.driverLogger.getLogAppender(), "http://" + options.host + ":" + options.port);
Http http = Http.forUrl(options.driverLogger.getAppender(), "http://" + options.host + ":" + options.port);
String webSocketUrl = http.path("json", "list").get()
.jsonPath("get[0] $[?(@.type=='Page')].webSocketDebuggerUrl").asString();
EdgeDevToolsDriver edge = new EdgeDevToolsDriver(options, command, webSocketUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static MicrosoftWebDriver start(ScenarioContext context, Map<String, Obje
options.arg("--port=" + options.port);
Command command = options.startProcess();
String urlBase = "http://" + options.host + ":" + options.port;
Http http = Http.forUrl(options.driverLogger.getLogAppender(), urlBase);
Http http = Http.forUrl(options.driverLogger.getAppender(), urlBase);
String sessionId = http.path("session")
.post(options.getCapabilities())
.jsonPath("get[0] response..sessionId").asString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static GeckoWebDriver start(ScenarioContext context, Map<String, Object>
options.arg("--port=" + options.port);
Command command = options.startProcess();
String urlBase = "http://" + options.host + ":" + options.port;
Http http = Http.forUrl(options.driverLogger.getLogAppender(), urlBase);
Http http = Http.forUrl(options.driverLogger.getAppender(), urlBase);
String sessionId = http.path("session")
.post(options.getCapabilities())
.jsonPath("get[0] response..sessionId").asString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static IosDriver start(ScenarioContext context, Map<String, Object> map,
options.arg("--port=" + options.port);
Command command = options.startProcess();
String urlBase = "http://" + options.host + ":" + options.port + "/wd/hub";
Http http = Http.forUrl(options.driverLogger.getLogAppender(), urlBase);
Http http = Http.forUrl(options.driverLogger.getAppender(), urlBase);
http.config("readTimeout","120000");
String sessionId = http.path("session")
.post(Collections.singletonMap("desiredCapabilities", map))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static SafariWebDriver start(ScenarioContext context, Map<String, Object>
options.arg("--port=" + options.port);
Command command = options.startProcess();
String urlBase = "http://" + options.host + ":" + options.port;
Http http = Http.forUrl(options.driverLogger.getLogAppender(), urlBase);
Http http = Http.forUrl(options.driverLogger.getAppender(), urlBase);
String sessionId = http.path("session")
.post(options.getCapabilities())
.jsonPath("get[0] response..sessionId").asString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static WinAppDriver start(ScenarioContext context, Map<String, Object> ma
options.arg(options.port + "");
Command command = options.startProcess();
String urlBase = "http://" + options.host + ":" + options.port;
Http http = Http.forUrl(options.driverLogger.getLogAppender(), urlBase);
Http http = Http.forUrl(options.driverLogger.getAppender(), urlBase);
Map<String, Object> capabilities = options.newMapWithSelectedKeys(map, "app", "appArguments", "appTopLevelWindow", "appWorkingDir");
String sessionId = http.path("session")
.post(Collections.singletonMap("desiredCapabilities", capabilities))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private JobExecutor(String serverUrl) {
String targetDir = FileUtils.getBuildDir();
appender = new FileLogAppender(new File(targetDir + File.separator + "karate-executor.log"));
logger = new Logger();
logger.setLogAppender(appender);
logger.setAppender(appender);
if (!Command.waitForHttp(serverUrl)) {
logger.error("unable to connect to server, aborting");
System.exit(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,13 @@ public Command(Logger logger, String uniqueName, String logFile, File workingDir
appender = new StringLogAppender(useLineFeed);
sharedAppender = false;
} else { // don't create new file if re-using an existing appender
LogAppender temp = this.logger.getLogAppender();
LogAppender temp = this.logger.getAppender();
sharedAppender = temp != null;
if (sharedAppender) {
appender = temp;
} else {
appender = new FileLogAppender(new File(logFile));
this.logger.setLogAppender(appender);
this.logger.setAppender(appender);
}
}
}
Expand Down

0 comments on commit 11165fd

Please sign in to comment.