Skip to content

Commit

Permalink
Merge pull request #66 from YamStranger/issue_ignore_filtering
Browse files Browse the repository at this point in the history
Implemented filtering using jbehave metafilters. Scenarios and Stories should be processed after filtering skip and ignore stories
  • Loading branch information
YamStranger committed Mar 29, 2016
2 parents 10818c5 + 55e4bed commit 70f7b41
Show file tree
Hide file tree
Showing 9 changed files with 1,281 additions and 66 deletions.
160 changes: 120 additions & 40 deletions src/main/java/net/serenitybdd/jbehave/SerenityReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public class SerenityReporter implements StoryReporter {
private static final String SKIP = "skip";
private static final String WIP = "wip";
private static final String IGNORE = "ignore";
private static final String BEFORE_STORIES = "BeforeStories";
private static final String AFTER_STORIES = "AfterStories";

private static Optional<TestResult> forcedScenarioResult;

Expand Down Expand Up @@ -107,8 +109,8 @@ public void storyCancelled(Story story, StoryDuration storyDuration) {

private Stack<String> activeScenarios = new Stack<>();
private List<String> givenStories = Lists.newArrayList();
private Map<String, Meta> scenarioMeta= new ConcurrentHashMap<>();
private Set<String> scenarioMetaProcessed= Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
private Map<String, Meta> scenarioMeta = new ConcurrentHashMap<>();
private Set<String> scenarioMetaProcessed = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());

private Story currentStory() {
return storyStack.peek();
Expand Down Expand Up @@ -155,7 +157,7 @@ public void beforeStory(Story story, boolean givenStory) {
startTestForFirstScenarioIn(story);
}
}
} else if(givenStory) {
} else if (givenStory) {
shouldNestScenarios(true);
}
registerStoryMeta(story.getMeta());
Expand All @@ -179,14 +181,16 @@ private void startTestForFirstScenarioIn(Story story) {
}

public void beforeScenario(String scenarioTitle) {
logger.debug("scenario started ".concat(scenarioTitle));
logger.debug("before scenario started ".concat(scenarioTitle));
clearScenarioResult();

if (managedDriverIsNotAlive()) {
WebdriverProxyFactory.resetDriver(ThucydidesWebDriverSupport.getDriver());
} else if (shouldRestartDriverBeforeEachScenario()
&& !shouldNestScenarios()) {
WebdriverProxyFactory.resetDriver(ThucydidesWebDriverSupport.getDriver());
if (isCandidateToBeExecuted(currentStory())) {
if (managedDriverIsNotAlive()) {
WebdriverProxyFactory.resetDriver(ThucydidesWebDriverSupport.getDriver());
} else if (shouldRestartDriverBeforeEachScenario()
&& !shouldNestScenarios()) {
WebdriverProxyFactory.resetDriver(ThucydidesWebDriverSupport.getDriver());
}
}

if (shouldResetStepsBeforeEachScenario()) {
Expand All @@ -201,10 +205,8 @@ public void beforeScenario(String scenarioTitle) {
startNewStep(scenarioTitle);
} else {
startScenarioCalled(scenarioTitle);
if(!isMetaProcessed(scenarioTitle)){
scenarioMeta(scenarioMeta.get(scenarioTitle));
scenarioMetaProcessed.add(scenarioTitle);
}
scenarioMeta(scenarioMeta.get(scenarioTitle));
scenarioMetaProcessed.add(scenarioTitle);
}
}

Expand Down Expand Up @@ -246,7 +248,7 @@ private void startTestSuiteForStory(Story story) {

private String getNarrativeFrom(Story story) {
return (!story.getNarrative().isEmpty()) ?
story.getNarrative().asString(new Keywords()).trim() : "";
story.getNarrative().asString(new Keywords()).trim() : "";
}

private void noteAnyGivenStoriesFor(Story story) {
Expand Down Expand Up @@ -294,7 +296,7 @@ private void registerTags(Story story) {
}

private boolean isFixture(Story story) {
return (story.getName().equals("BeforeStories") || story.getName().equals("AfterStories"));
return (story.getName().equals(BEFORE_STORIES) || story.getName().equals(AFTER_STORIES));
}

private String getRequestedDriver(Meta metaData) {
Expand Down Expand Up @@ -443,6 +445,8 @@ private void registerStoryMeta(Meta metaData) {
StepEventBus.getEventBus().suspendTest();
} else if (isSkipped(metaData)) {
StepEventBus.getEventBus().suspendTest();
}else if (isIgnored(metaData)) {
StepEventBus.getEventBus().suspendTest();
}
}

Expand All @@ -451,17 +455,19 @@ private Optional<TestResult> getStoryMetadataResult() {
return Optional.of(TestResult.PENDING);
} else if (isSkipped(currentStory().getMeta())) {
return Optional.of(TestResult.SKIPPED);
} else if (isIgnored(currentStory().getMeta())) {
return Optional.of(TestResult.IGNORED);
} else {
return Optional.absent();
}
}

private boolean isStoryManual(){
private boolean isStoryManual() {
return isManual(currentStory().getMeta());
}

private Optional<TestResult> getScenarioMetadataResult() {
return forcedScenarioResult;
return forcedScenarioResult;
}

private void registerScenarioMeta(Meta metaData) {
Expand All @@ -482,6 +488,7 @@ private String removeSuffixFrom(String name) {
}

public void afterStory(boolean given) {
logger.debug("afterStory " + given);
shouldNestScenarios(false);
if (given) {
givenStoryMonitor.exitingGivenStory();
Expand All @@ -508,7 +515,7 @@ private void closeBrowsersForThisStory() {
}

private boolean isAfterStory(Story currentStory) {
return (currentStory.getName().equals("AfterStories"));
return (currentStory.getName().equals(AFTER_STORIES));
}

private synchronized void generateReports() {
Expand Down Expand Up @@ -547,14 +554,9 @@ private boolean shouldResetStepsBeforeEachScenario() {
SerenityJBehaveSystemProperties.RESET_STEPS_EACH_SCENARIO.getName(), true);
}

private boolean isMetaProcessed(final String scenarioTitle) {
return scenarioMetaProcessed.contains(scenarioTitle);
}

public void scenarioMeta(Meta meta) {
final String title = activeScenarios.peek();
logger.debug("scenario:\"" + (StringUtils.isEmpty(title) ? " don't know name ": title) + "\" registering metadata for" + meta);
if (!isMetaProcessed(title)) {
logger.debug("scenario:\"" + (StringUtils.isEmpty(title) ? " don't know name " : title) + "\" registering metadata for" + meta);
registerIssues(meta);
registerFeaturesAndEpics(meta);
registerTags(meta);
Expand All @@ -565,7 +567,6 @@ public void scenarioMeta(Meta meta) {
} else if (isSkippedScenario()) {
StepEventBus.getEventBus().testSkipped();
}
}
}

private boolean isPending(Meta metaData) {
Expand All @@ -577,7 +578,15 @@ private boolean isManual(Meta metaData) {
}

private boolean isSkipped(Meta metaData) {
return (metaData.hasProperty(WIP)|| metaData.hasProperty(SKIP));
return (metaData.hasProperty(WIP) || metaData.hasProperty(SKIP));
}

private boolean isCandidateToBeExecuted(Meta metaData) {
return !isIgnored(metaData) && !isPending(metaData) && !isSkipped(metaData);
}

private boolean isCandidateToBeExecuted(Story story) {
return isCandidateToBeExecuted(story.getMeta());
}

private boolean isIgnored(Meta metaData) {
Expand All @@ -586,29 +595,25 @@ private boolean isIgnored(Meta metaData) {

public void afterScenario() {
final String scenarioTitle = activeScenarios.peek();
logger.debug("afterScenario : "+activeScenarios.peek());
if(!isMetaProcessed(scenarioTitle)){
logger.debug("afterScenario : " + activeScenarios.peek());
scenarioMeta(scenarioMeta.get(scenarioTitle));
scenarioMetaProcessed.add(scenarioTitle);
}


if (givenStoryMonitor.isInGivenStory() || shouldNestScenarios()) {
StepEventBus.getEventBus().stepFinished();
} else {
if(isIgnoredScenario()) {
StepEventBus.getEventBus().testIgnored();
}else{
StepEventBus.getEventBus().testFinished();
}
if (isPendingScenario() || isPendingStory()) {
StepEventBus.getEventBus().setAllStepsTo(TestResult.PENDING);
}
if (isSkippedScenario() || isSkippedStory()) {
StepEventBus.getEventBus().testPending();
} else if (isSkippedScenario() || isSkippedStory()) {
StepEventBus.getEventBus().setAllStepsTo(TestResult.SKIPPED);
}
if(isIgnoredScenario()){
StepEventBus.getEventBus().testSkipped();
} else if (isIgnoredScenario()) {
StepEventBus.getEventBus().testIgnored();
StepEventBus.getEventBus().setAllStepsTo(TestResult.IGNORED);
} else{
StepEventBus.getEventBus().testFinished();
}
activeScenarios.pop();
}
Expand Down Expand Up @@ -639,15 +644,18 @@ private boolean isSkippedStory() {


public void givenStories(GivenStories givenStories) {
logger.debug("givenStories " + givenStories);
givenStoryMonitor.enteringGivenStory();
}

public void givenStories(List<String> strings) {
logger.debug("givenStories " + strings);
}

int exampleCount = 0;

public void beforeExamples(List<String> steps, ExamplesTable table) {
logger.debug("beforeExamples " + steps + " " + table);
exampleCount = 0;
StepEventBus.getEventBus().useExamplesFrom(serenityTableFrom(table));
}
Expand All @@ -658,7 +666,9 @@ private DataTable serenityTableFrom(ExamplesTable table) {
}

public void example(Map<String, String> tableRow) {
if (shouldRestartDriverBeforeEachScenario()) {
logger.debug("example " + tableRow);
if (isCandidateToBeExecuted(currentStory())
&& shouldRestartDriverBeforeEachScenario()) {
WebdriverProxyFactory.resetDriver(ThucydidesWebDriverSupport.getDriver());
}

Expand Down Expand Up @@ -692,6 +702,7 @@ private void restartPeriodically() {
}

public void afterExamples() {
logger.debug("afterExamples:");
finishExample();
}

Expand Down Expand Up @@ -766,22 +777,91 @@ private boolean isAssumptionFailure(Throwable rootCause) {
return (AssumptionViolatedException.class.isAssignableFrom(rootCause.getClass()));
}

public List<String> processExcludedByFilter(final Story story, final Set<String> exclude) {
final Meta storyMeta = story.getMeta();
final List<Scenario> processing = new LinkedList<>();
final List<String> processed = new LinkedList<>();

if (isSkipped(storyMeta) || isIgnored(storyMeta)) { //this story should be excluded by filter
processing.addAll(story.getScenarios());
} else {
for (Scenario scenario : story.getScenarios()) {
final Meta scenarioMeta = scenario.getMeta();
if (isSkipped(scenarioMeta) || isIgnored(scenarioMeta)) { //this scenario should be excluded by filter
processing.add(scenario);
}
}
}
if (processing.size() > 0) {
final Story beforeStory = new Story();
beforeStory.namedAs(BEFORE_STORIES);
final Story afterStory = new Story();
afterStory.namedAs(AFTER_STORIES);

final Narrative narrative = story.getNarrative();
beforeStory(beforeStory, false);
afterStory(false);
beforeStory(story, false);
narrative(narrative);
for (final Scenario filtered : processing) {
final String scenarioKey = scenarioKey(story, filtered);
if (!exclude.contains(scenarioKey)) {

beforeScenario(filtered.getTitle());
scenarioMeta(filtered.getMeta());

final List<String> steps = filtered.getSteps();
if (ExamplesTable.EMPTY == filtered.getExamplesTable() || filtered.getExamplesTable().getRows().size() == 0) {
for (final String step : steps) {
beforeStep(step);
successful(step);
}
} else {
final ExamplesTable examples = filtered.getExamplesTable();
beforeExamples(steps, examples);
for (final Map<String, String> row : examples.getRows()) {
example(row);
for (final String step : steps) {
beforeStep(step);
successful(step);
}
}
afterExamples();
}
afterScenario();
processed.add(scenarioKey(story, filtered));
}
}
afterStory(false);
beforeStory(afterStory, false);
afterStory(false);
}
return processed;
}

private String scenarioKey(final Story story, final Scenario scenario){
return story.getPath().concat(scenario.getTitle());
}

public void failedOutcomes(String s, OutcomesTable outcomesTable) {
logger.debug("failedOutcomes");
}

public void restarted(String s, Throwable throwable) {
logger.debug("restarted");
}

@Override
public void restartedStory(Story story, Throwable cause) {

logger.debug("restartedStory");
}

public void dryRun() {
logger.debug("dryRun");
}

public void pendingMethods(List<String> strings) {
logger.debug("pendingMethods");
}

private String normalized(String value) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/serenitybdd/jbehave/SerenityStories.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.jbehave.core.reporters.Format;
import org.jbehave.core.steps.InjectableStepsFactory;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.MalformedURLException;
Expand All @@ -38,6 +40,8 @@
@RunWith(SerenityReportingRunner.class)
public class SerenityStories extends JUnitStories {

private static final Logger logger = LoggerFactory.getLogger(SerenityStories.class);

public static final String DEFAULT_STORY_NAME = "**/*.story";
public static final List<String> DEFAULT_GIVEN_STORY_PREFIX = ImmutableList.of("Given", "Precondition");

Expand Down
Loading

0 comments on commit 70f7b41

Please sign in to comment.