Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaleo committed Jun 26, 2020
1 parent 34c788e commit 4d9cd4d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,17 @@ public class BaseStepListener implements StepListener, StepPublisher {
*/
private final List<TestOutcome> testOutcomes;

private ThreadLocal<TestOutcome> currentTestOutcome;

/**
* Keeps track of what steps have been started but not finished, in order to structure nested steps.
*/
private final Stack<TestStep> currentStepStack;
private final ThreadLocal<Stack<TestStep>> currentStepStack;

/**
* Keeps track of the current step group, if any.
*/
private final Stack<TestStep> currentGroupStack;
private final ThreadLocal<Stack<TestStep>> currentGroupStack;

private StepEventBus eventBus;
/**
Expand Down Expand Up @@ -274,8 +276,9 @@ public BaseStepListener(final File outputDirectory) {
public BaseStepListener(final File outputDirectory, Injector injector) {
this.proxyFactory = WebdriverProxyFactory.getFactory();
this.testOutcomes = new ArrayList<>();
this.currentStepStack = new Stack<>();
this.currentGroupStack = new Stack<>();
this.currentTestOutcome = new ThreadLocal<>();
this.currentStepStack = ThreadLocal.withInitial(Stack<TestStep>::new);
this.currentGroupStack = ThreadLocal.withInitial(Stack<TestStep>::new);
this.outputDirectory = outputDirectory;
this.storywideIssues = new ArrayList<>();
this.storywideTags = new ArrayList<>();
Expand Down Expand Up @@ -351,7 +354,7 @@ protected WebdriverProxyFactory getProxyFactory() {
}

public TestOutcome getCurrentTestOutcome() {
return latestTestOutcome().orElse(unavailableTestOutcome());
return latestTestOutcome().orElse(unavailableTestOutcome());
}

private static final TestOutcome UNAVAILABLE_TEST_OUTCOME = new TestOutcome("Test outcome unavailable"); // new UnavailableTestOutcome("Test outcome unavailable");
Expand All @@ -368,8 +371,7 @@ public java.util.Optional<TestOutcome> latestTestOutcome() {
if (testOutcomes.isEmpty()) {
return java.util.Optional.empty();
} else {
TestOutcome latestOutcome = testOutcomes.get(testOutcomes.size() - 1);
return java.util.Optional.of(latestOutcome);
return java.util.Optional.ofNullable(currentTestOutcome.get());
}
}

Expand Down Expand Up @@ -442,17 +444,21 @@ public void testSuiteFinished() {
*/
public void testStarted(final String testMethod) {
TestOutcome newTestOutcome = TestOutcome.forTestInStory(testMethod, testSuite, testedStory);
recordNewTestOutcome(testMethod, newTestOutcome);
this.currentTestOutcome.set(newTestOutcome);
recordNewTestOutcome(testMethod, currentTestOutcome.get());
}

public void testStarted(final String testMethod, final String id) {
TestOutcome newTestOutcome = TestOutcome.forTestInStory(testMethod, testSuite, testedStory).withId(id);
recordNewTestOutcome(testMethod, newTestOutcome);
this.currentTestOutcome.set(newTestOutcome);
recordNewTestOutcome(testMethod, currentTestOutcome.get());
}

private void recordNewTestOutcome(String testMethod, TestOutcome newTestOutcome) {
newTestOutcome.setTestSource(StepEventBus.getEventBus().getTestSource());
testOutcomes.add(newTestOutcome);
synchronized(testOutcomes) {
testOutcomes.add(newTestOutcome);
}
setAnnotatedResult(testMethod);
}

Expand Down Expand Up @@ -539,7 +545,7 @@ public void testFinished(final TestOutcome outcome, boolean inDataDrivenTest) {

}

currentStepStack.clear();
currentStepStack.get().clear();
}

private void testAndTopLevelStepsShouldBeIgnored() {
Expand All @@ -562,7 +568,7 @@ private boolean currentTestIsABrowserTest() {
}

public void testRetried() {
currentStepStack.clear();
currentStepStack.get().clear();
testOutcomes.remove(getCurrentTestOutcome());
}

Expand Down Expand Up @@ -629,7 +635,7 @@ private void recordStep(ExecutedStepDescription description) {
startNewGroupIfNested();
setDefaultResultFromAnnotations(step, description);

currentStepStack.push(step);
currentStepStack.get().push(step);
recordStepToCurrentTestOutcome(step);
}

Expand All @@ -656,18 +662,18 @@ private void startNewGroupIfNested() {

private void startNewGroup() {
getCurrentTestOutcome().startGroup();
currentGroupStack.push(getCurrentStep());
currentGroupStack.get().push(getCurrentStep());
}

private java.util.Optional<TestStep> currentStep() {
if (currentStepStack.isEmpty()) {
if (currentStepStack.get() == null || currentStepStack.get().isEmpty()) {
return java.util.Optional.empty();
}
return (java.util.Optional.of(currentStepStack.peek()));
return (java.util.Optional.of(currentStepStack.get().peek()));
}

private TestStep getCurrentStep() {
return currentStepStack.peek();
return currentStepStack.get().peek();
}

private java.util.Optional<TestStep> getPreviousStep() {
Expand All @@ -680,15 +686,15 @@ private java.util.Optional<TestStep> getPreviousStep() {
}

private TestStep getCurrentGroup() {
if (currentGroupStack.isEmpty()) {
if (currentGroupStack.get().isEmpty()) {
return null;
} else {
return currentGroupStack.peek();// findLastChildIn(currentGroupStack.peek());
return currentGroupStack.get().peek();// findLastChildIn(currentGroupStack.peek());
}
}

private boolean thereAreUnfinishedSteps() {
return !currentStepStack.isEmpty();
return !currentStepStack.get().isEmpty();
}

public void stepFinished() {
Expand All @@ -704,7 +710,7 @@ private void updateExampleTableIfNecessary(TestResult result) {
}

private void finishGroup() {
currentGroupStack.pop();
currentGroupStack.get().pop();
getCurrentTestOutcome().endGroup();
}

Expand Down Expand Up @@ -801,7 +807,7 @@ private void currentStepDone(TestResult result) {
currentStepMethodStack.pop();
}
if (currentStepExists()) {
TestStep finishedStep = currentStepStack.pop();
TestStep finishedStep = currentStepStack.get().pop();
finishedStep.recordDuration();
if (result != null) {
finishedStep.setResult(result);
Expand All @@ -814,10 +820,10 @@ private void currentStepDone(TestResult result) {
}

private boolean currentStepExists() {
return !currentStepStack.isEmpty();
return !currentStepStack.get().isEmpty();
}

public int getCurrentLevel() { return currentStepStack.size(); }
public int getCurrentLevel() { return currentStepStack.get().size(); }

private void takeEndOfStepScreenshotFor(final TestResult result) {
if (currentTestIsABrowserTest() && shouldTakeEndOfStepScreenshotFor(result)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ public class JUnitStepListener extends RunListener {
private StepListener[] extraListeners;
private Map<String,List<String>> failedTests = Collections.synchronizedMap(new HashMap<String,List<String>>());
private Class<?> testClass;
private boolean testStarted;
private ThreadLocal<Boolean> testStarted;

public static JUnitStepListenerBuilder withOutputDirectory(File outputDirectory) {
return new JUnitStepListenerBuilder(outputDirectory);
}

protected JUnitStepListener(Class<?> testClass, BaseStepListener baseStepListener, StepListener... listeners) {
testStarted = false;
testStarted = new ThreadLocal<>();
testStarted.set(new Boolean(false));
this.baseStepListener = baseStepListener;
this.extraListeners = listeners;
this.testClass = testClass;
Expand Down Expand Up @@ -133,7 +134,7 @@ private void updateFailureList(Failure failure) {
}

private void startTestIfNotYetStarted(Description description) {
if (!testStarted) {
if (!testStarted.get()) {
testStarted(description);
}
}
Expand Down Expand Up @@ -166,10 +167,10 @@ public void dropListeners() {
}

private void startTest() {
testStarted = true;
testStarted.set(true);
}
private void endTest() {
testStarted = false;
testStarted.set(false);
}

private boolean testingThisTest(Description description) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1958,6 +1958,14 @@ public String getContext() {

return context;
}

/**
* Setting the context
* @param context
*/
public void setContext(String context) {
this.context = context;
}

/**
* Returns the name of the test prefixed by the name of the story.
Expand Down

0 comments on commit 4d9cd4d

Please sign in to comment.