Skip to content

Commit

Permalink
Improved SessionContext related logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Reiche committed Nov 12, 2021
1 parent 0fba82a commit e318883
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ public class MethodEndWorker implements MethodEndEvent.Listener, Loggable {
@Subscribe
@Override
public void onMethodEnd(MethodEndEvent event) {
ExecutionContextController.clearCurrentTestResult();

ITestResult testResult = event.getTestResult();
ITestNGMethod testMethod = event.getTestMethod();
MethodContext methodContext = event.getMethodContext();
Expand All @@ -70,5 +68,7 @@ public void onMethodEnd(MethodEndEvent event) {
if (methodContext.getStatus() != Status.FAILED) {
TesterraListener.getEventBus().post(new TestStatusUpdateEvent(methodContext));
}

ExecutionContextController.clearCurrentTestResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ public void format(LogEvent event, StringBuilder toAppendTo ) {
}

// enhance with method context id
SessionContext currentSessionContext = ExecutionContextController.getCurrentSessionContext();
if (currentSessionContext != null) {
toAppendTo.append("[SCID:").append(currentSessionContext.getId()).append("]");
}
ExecutionContextController.getCurrentSessionContext().flatMap(SessionContext::getRemoteSessionId).ifPresent(s -> {
toAppendTo.append("[SCID:").append(s).append("]");
});
}

public static ContextIdsPatternConverter newInstance(String[] options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

public abstract class AbstractContext implements Loggable {
private String name;
private String id;
private final String id = IDUtils.getB64encXID();
private AbstractContext parentContext;
private final Date startTime = new Date();
private Date endTime;
Expand Down Expand Up @@ -75,10 +75,6 @@ protected void setName(String name) {
this.name = name;
}

protected void setId(String id) {
this.id = id;
}

protected void setParentContext(AbstractContext context) {
this.parentContext = context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class SessionContext extends AbstractContext {
private Map<String, Object> capabilities;
private final WebDriverRequest webDriverRequest;
private final Queue<MethodContext> methodContexts = new ConcurrentLinkedQueue<>();
private String remoteSessionId;

public SessionContext(WebDriverRequest webDriverRequest) {
try {
Expand All @@ -46,15 +47,6 @@ public SessionContext(WebDriverRequest webDriverRequest) {
throw new RuntimeException(e);
}
this.setName(webDriverRequest.getSessionKey());

// this.provider = provider;
//
// final MethodContext currentMethodContext = ExecutionContextController.getCurrentMethodContext();
// if (currentMethodContext != null) {
// this.name = currentMethodContext.getName() + "_";
// } else {
// this.name = "";
// }
}

public WebDriverRequest getWebDriverRequest() {
Expand All @@ -71,11 +63,11 @@ public SessionContext setSessionKey(String sessionKey) {
}

public Optional<String> getRemoteSessionId() {
return Optional.of(getId());
return Optional.ofNullable(this.remoteSessionId);
}

public SessionContext setRemoteSessionId(String sessionId) {
setId(sessionId);
this.remoteSessionId = sessionId;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,12 @@ public static void setCurrentSessionContext(final SessionContext sessionContext)
CURRENT_SESSION_CONTEXT.set(sessionContext);
}

public static SessionContext getCurrentSessionContext() {
return CURRENT_SESSION_CONTEXT.get();
public static Optional<SessionContext> getCurrentSessionContext() {
return Optional.ofNullable(CURRENT_SESSION_CONTEXT.get());
}

public static void clearCurrentSessionContext() {
CURRENT_SESSION_CONTEXT.remove();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,19 @@ build the final request (filled with all requested values)
if (rawDriver instanceof RemoteWebDriver) {
SessionId sessionId = ((RemoteWebDriver) rawDriver).getSessionId();
sessionContext.setRemoteSessionId(sessionId.toString());
} else {
sessionContext.setRemoteSessionId(sessionContext.getId());
}

sessionContext.setActualBrowserName(browserInformation.getBrowserName());
sessionContext.setActualBrowserVersion(browserInformation.getBrowserVersion());
log().info(String.format(
"Started %s (sessionKey=%s, sessionId=%s, node=%s, userAgent=%s) in %s",
"Started %s (sessionKey=%s, node=%s, userAgent=%s) in %s",
rawDriver.getClass().getSimpleName(),
sessionContext.getSessionKey(),
sessionContext.getRemoteSessionId().orElse("(local)"),
sessionContext.getNodeInfo().map(Object::toString).orElse("(unknown)"),
browserInformation.getBrowserName() + ":" + browserInformation.getBrowserVersion(),
sw.toString()
sw
));

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,10 @@
import eu.tsystems.mms.tic.testframework.report.model.context.SessionContext;
import eu.tsystems.mms.tic.testframework.report.utils.ExecutionContextController;
import eu.tsystems.mms.tic.testframework.report.utils.ExecutionContextUtils;
import eu.tsystems.mms.tic.testframework.utils.StringUtils;
import eu.tsystems.mms.tic.testframework.utils.WebDriverUtils;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Queue;
Expand All @@ -47,6 +44,7 @@
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Consumer;
import java.util.stream.Stream;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
Expand Down Expand Up @@ -118,6 +116,8 @@ private static void unlinkFromThread(String sessionKey, WebDriver eventFiringWeb
final long threadId = Thread.currentThread().getId();
WEBDRIVER_THREAD_ID_MAP.remove(eventFiringWebDriver, threadId);

ExecutionContextController.clearCurrentSessionContext();

/*
storing driver into driver storage, for whatever reason
*/
Expand Down

0 comments on commit e318883

Please sign in to comment.