Best Serenity jBehave code snippet using net.serenitybdd.jbehave.SerenityReporter.afterExamples
Source:SerenityReporter.java
...551 private boolean executingExamples() {552 return (exampleCount > 0);553 }554 @Override555 public void afterExamples() {556 if (givenStoryMonitor.isInGivenStory()) {557 return;558 }559 finishExample();560 }561 @Override562 public void beforeStep(String stepTitle) {563 StepEventBus.getEventBus().stepStarted(ExecutedStepDescription.withTitle(stepTitle));564 }565 @Override566 public void successful(String title) {567 if (annotatedResultTakesPriority()) {568 processAnnotatedResult();569 } else {570 StepEventBus.getEventBus().updateCurrentStepTitle(normalized(title));571 StepEventBus.getEventBus().stepFinished();572 }573 }574 private void processAnnotatedResult() {575 TestResult forcedResult = StepEventBus.getEventBus().getForcedResult().get();576 switch (forcedResult) {577 case PENDING:578 StepEventBus.getEventBus().stepPending();579 break;580 case IGNORED:581 StepEventBus.getEventBus().stepIgnored();582 break;583 case SKIPPED:584 StepEventBus.getEventBus().stepIgnored();585 break;586 default:587 StepEventBus.getEventBus().stepIgnored();588 }589 }590 private boolean annotatedResultTakesPriority() {591 return StepEventBus.getEventBus().getForcedResult().isPresent();592 }593 @Override594 public void ignorable(String title) {595 StepEventBus.getEventBus().updateCurrentStepTitle(normalized(title));596 StepEventBus.getEventBus().stepIgnored();597 }598 @Override599 public void comment(String step) {600 StepEventBus.getEventBus().stepStarted(ExecutedStepDescription.withTitle(step));601 StepEventBus.getEventBus().stepIgnored();602 }603 @Override604 public void pending(String stepTitle) {605 StepEventBus.getEventBus().stepStarted(ExecutedStepDescription.withTitle(normalized(stepTitle)));606 StepEventBus.getEventBus().stepPending();607 }608 @Override609 public void notPerformed(String stepTitle) {610 StepEventBus.getEventBus().stepStarted(ExecutedStepDescription.withTitle(normalized(stepTitle)));611 StepEventBus.getEventBus().stepIgnored();612 }613 @Override614 public void failed(String stepTitle, Throwable cause) {615 if (!StepEventBus.getEventBus().testSuiteHasStarted()) {616 declareOutOfSuiteFailure();617 }618 if (!errorOrFailureRecordedForStep(cause.getCause())) {619 StepEventBus.getEventBus().updateCurrentStepTitle(stepTitle);620 Throwable rootCause = new RootCauseAnalyzer(cause.getCause()).getRootCause().toException();621 if (isAssumptionFailure(rootCause)) {622 StepEventBus.getEventBus().assumptionViolated(rootCause.getMessage());623 } else {624 StepEventBus.getEventBus().stepFailed(new StepFailure(ExecutedStepDescription.withTitle(normalized(stepTitle)), rootCause));625 }626 }627 }628 private void declareOutOfSuiteFailure() {629 String storyName = !storyStack.isEmpty() ? storyStack.peek().getName() : "Before or After Story";630 String storyId = !storyStack.isEmpty() ? storyStack.peek().getPath() : null;631 StepEventBus.getEventBus().testStarted(storyName, storyId);632 }633 private boolean isAssumptionFailure(Throwable rootCause) {634 return (AssumptionViolatedException.class.isAssignableFrom(rootCause.getClass()));635 }636 public List<String> processExcludedByFilter(final Story story, final Set<String> exclude) {637 final Meta storyMeta = story.getMeta();638 final List<Scenario> processing = new LinkedList<>();639 final List<String> processed = new LinkedList<>();640 if (isSkipped(storyMeta) || isIgnored(storyMeta)) { //this story should be excluded by filter641 processing.addAll(story.getScenarios());642 } else {643 for (Scenario scenario : story.getScenarios()) {644 final Meta scenarioMeta = scenario.getMeta();645 if (isSkipped(scenarioMeta) || isIgnored(scenarioMeta)) { //this scenario should be excluded by filter646 processing.add(scenario);647 }648 }649 }650 if (processing.size() > 0) {651 final Story beforeStory = new Story();652 beforeStory.namedAs(BEFORE_STORIES);653 final Story afterStory = new Story();654 afterStory.namedAs(AFTER_STORIES);655 final Narrative narrative = story.getNarrative();656 beforeStory(beforeStory, false);657 afterStory(false);658 beforeStory(story, false);659 narrative(narrative);660 for (final Scenario filtered : processing) {661 final String scenarioKey = scenarioKey(story, filtered);662 if (!exclude.contains(scenarioKey)) {663 beforeScenario(filtered);664 final List<String> steps = filtered.getSteps();665 if (ExamplesTable.EMPTY == filtered.getExamplesTable() || filtered.getExamplesTable().getRows().size() == 0) {666 for (final String step : steps) {667 beforeStep(step);668 successful(step);669 }670 } else {671 final ExamplesTable examples = filtered.getExamplesTable();672 beforeExamples(steps, examples);673 for (final Map<String, String> row : examples.getRows()) {674 example(row);675 for (final String step : steps) {676 beforeStep(step);677 successful(step);678 }679 }680 afterExamples();681 }682 afterScenario();683 processed.add(scenarioKey(story, filtered));684 }685 }686 afterStory(false);687 beforeStory(afterStory, false);688 afterStory(false);689 }690 return processed;691 }692 private String scenarioKey(final Story story, final Scenario scenario) {693 return story.getPath().concat(scenario.getTitle());694 }...
afterExamples
Using AI Code Generation
1 public void afterExamples() {2 if (currentStoryContext().isPresent()) {3 currentStoryContext().get().afterExamples();4 }5 }6 public void afterScenario() {7 if (currentStoryContext().isPresent()) {8 currentStoryContext().get().afterScenario();9 }10 }11 public void beforeScenario(String scenarioTitle) {12 if (currentStoryContext().isPresent()) {13 currentStoryContext().get().beforeScenario(scenarioTitle);14 }15 }16 public void beforeStory(Story story, boolean givenStory) {17 if (currentStoryContext().isPresent()) {18 currentStoryContext().get().beforeStory(story, givenStory);19 }20 }21 public void afterStory(boolean givenStory) {22 if (currentStoryContext().isPresent()) {23 currentStoryContext().get().afterStory(givenStory);24 }25 }26 public void beforeScenario(String scenarioTitle) {27 if (currentStoryContext().isPresent()) {28 currentStoryContext().get().beforeScenario(scenarioTitle);29 }30 }31}32 public void afterScenario() {33 if (currentStoryContext().isPresent()) {34 currentStoryContext().get().afterScenario();35 }36 }37 public void afterExamples() {38 if (currentStoryContext().isPresent()) {39 currentStoryContext().get().afterExamples();40 }41 }42 public void beforeExamples(List<String> steps, ExamplesTable table) {43 if (currentStoryContext().isPresent()) {44 currentStoryContext().get().beforeExamples
afterExamples
Using AI Code Generation
1package net.serenitybdd.jbehave;2import net.thucydides.core.ThucydidesSystemProperty;3import net.thucydides.core.annotations.Step;4import net.thucydides.core.guice.Injectors;5import net.thucydides.core.model.TestOutcome;6import net.thucydides.core.reports.ReportService;7import net.thucydides.core.screenshots.ScreenshotAndHtmlSource;8import net.thucydides.core.screenshots.ScreenshotException;9import net.thucydides.core.screenshots.ScreenshotTaker;10import net.thucydides.core.steps.StepEventBus;11import net.thucydides.core.util.EnvironmentVariables;12import net.thucydides.core.webdriver.Configuration;13import net.thucydides.core.webdriver.WebDriverFacade;14import org.apache.commons.lang3.StringUtils;15import org.jbehave.core.annotations.AfterScenario;16import org.jbehave.core.annotations.AfterStory;17import org.jbehave.core.annotations.BeforeScenario;18import org.jbehave.core.annotations.ScenarioType;19import org.jbehave.core.embedder.Embedder;20import org.jbehave.core.embedder.PerformableTree;21import org.jbehave.core.embedder.StoryManager;22import org.jbehave.core.failures.UUIDExceptionWrapper;23import org.jbehave.core.model.*;24import org.jbehave.core.reporters.ConcurrentStoryReporter;25import org.jbehave.core.reporters.StoryReporter;26import org.jbehave.core.steps.StepCandidate;27import org.jbehave.core.steps.StepCreator;28import org.jbehave.core.steps.StepCreator.PendingStepFound;29import org.jbehave.core.steps.StepCreator.StepExecution
afterExamples
Using AI Code Generation
1public void afterScenario() {2 Scenario scenario = Serenity.getCurrentSession().getMetaData().get("currentScenario");3 List<Step> steps = scenario.getSteps();4 Step lastStep = steps.get(steps.size() - 1);5 TestOutcome testOutcome = Serenity.getCurrentSession().getMetaData().get("currentTestOutcome");6 TestResult result = testOutcome.getResult();7 TestResult stepResult = lastStep.getResult();8 if (!stepResult.equals(TestResult.FAILURE) && result.equals(TestResult.FAILURE)) {9 TestOutcome stepTestOutcome = Serenity.getCurrentSession().getMetaData().get("currentStepTestOutcome");10 Throwable stepException = stepTestOutcome.getTestFailureCause();11 testOutcome.recordStepFailure(stepException);12 }13}
afterExamples
Using AI Code Generation
1import java.util.ArrayList;2import java.util.List;3import java.util.Map;4import org.apache.commons.lang3.StringUtils;5import org.jbehave.core.model.ExamplesTable;6import org.jbehave.core.model.Story;7import org.jbehave.core.reporters.StoryReporter;8import org.jbehave.core.steps.Step;9import org.jbehave.core.steps.StepType;10import org.jbehave.core.steps.Timing;11import net.thucydides.core.model.TestOutcome;12import net.thucydides.core.model.TestStep;13import net.thucydides.core.model.TestTag;14import net.thucydides.core.model.TestType;15import net.thucydides.core.steps.StepEventBus;16import net.thucydides.core.steps.StepFailure;17import net.thucydides.core.steps.StepListener;18import net.thucydides.core.steps.StepTypeConverter;19public class CustomizedSerenityReporter implements StoryReporter, StepListener {20 private final SerenityReporter delegate;21 private final List<TestOutcome> testOutcomes = new ArrayList<TestOutcome>();22 private TestOutcome currentTestOutcome;23 private List<TestStep> currentTestSteps;24 private List<TestStep> currentBeforeOrAfterSteps;25 private List<TestStep> currentBeforeOrAfterExamplesSteps;26 private List<TestStep> currentBeforeOrAfterStoriesSteps;27 private TestOutcome beforeOrAfterStory;28 private int stepCount;
afterExamples
Using AI Code Generation
1public class SerenityReporter extends SerenityStoryReporter {2private static final String SCREENSHOT_DIRECTORY = "target/site/serenity";3private static final String SCREENSHOT_EXTENSION = "png";4private static final String SCREENSHOT_FILENAME = "screenshot." + SCREENSHOT_EXTENSION;5public SerenityReporter() {6 super();7}8public SerenityReporter(PrintStream out) {9 super(out);10}11public void afterExamples() {12 super.afterExamples();13 takeScreenshot();14}15public void afterScenario() {16 super.afterScenario();17 takeScreenshot();18}19private void takeScreenshot() {20 String screenshotPath = SCREENSHOT_DIRECTORY + "/" + SCREENSHOT_FILENAME;21 try {22 WebDriver driver = Thucydides.getWebdriverManager().getCurrentDriver();23 File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);24 FileUtils.copyFile(scrFile, new File(screenshotPath));25 Thucydides.getStepEventBus().stepFinished();26 } catch (Exception e) {27 e.printStackTrace();28 }29}30}31public class SerenityReporter extends SerenityStoryReporter {32private static final String SCREENSHOT_DIRECTORY = "target/site/serenity";33private static final String SCREENSHOT_EXTENSION = "png";34private static final String SCREENSHOT_FILENAME = "screenshot." + SCREENSHOT_EXTENSION;35public SerenityReporter() {36 super();37}38public SerenityReporter(PrintStream out) {39 super(out);40}41public void afterExamples() {42 super.afterExamples();43 takeScreenshot();44}45public void afterScenario() {46 super.afterScenario();47 takeScreenshot();48}49private void takeScreenshot() {50 String screenshotPath = SCREENSHOT_DIRECTORY + "/" + SCREENSHOT_FILENAME;51 try {52 WebDriver driver = Thucydides.getWebdriverManager().getCurrentDriver();53 File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!