Skip to content

Instantly share code, notes, and snippets.

@mallim
Created October 9, 2013 01:49
Show Gist options
  • Save mallim/6894896 to your computer and use it in GitHub Desktop.
Save mallim/6894896 to your computer and use it in GitHub Desktop.
JBehave, Selenium with Spring
import com.thoughtworks.selenium.Selenium;
import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.embedder.StoryControls;
import org.jbehave.core.failures.FailingUponPendingStep;
import org.jbehave.core.failures.PendingStepStrategy;
import org.jbehave.core.io.CodeLocations;
import org.jbehave.core.io.LoadFromClasspath;
import org.jbehave.core.io.StoryFinder;
import org.jbehave.core.junit.JUnitStories;
import org.jbehave.core.reporters.CrossReference;
import org.jbehave.core.reporters.Format;
import org.jbehave.core.reporters.StoryReporterBuilder;
import org.jbehave.core.steps.InjectableStepsFactory;
import org.jbehave.core.steps.ParameterConverters;
import org.jbehave.core.steps.spring.SpringApplicationContextFactory;
import org.jbehave.core.steps.spring.SpringStepsFactory;
import org.jbehave.web.selenium.*;
import org.springframework.context.ApplicationContext;
import java.util.ArrayList;
import java.util.List;
import static org.jbehave.core.io.CodeLocations.codeLocationFromPath;
import static org.jbehave.core.reporters.Format.CONSOLE;
import static org.jbehave.web.selenium.WebDriverHtmlOutput.WEB_DRIVER_HTML;
/**
* Use for running of JBehave test cases during development
*
* Take note that Jenkins should not be running this class
*
* Source of reference
* 1. https://github.com/jbehave/jbehave-tutorial/tree/master/etsy-selenium/java-spring
*
*/
public class TestWebStories extends JUnitStories{
private ApplicationContext context;
ParameterConverters parameterConverters = new ParameterConverters();
PendingStepStrategy pendingStepStrategy = new FailingUponPendingStep();
CrossReference crossReference = new CrossReference().withJsonOnly().withPendingStepStrategy(pendingStepStrategy)
.withOutputAfterEachStory(true).excludingStoriesWithNoExecutedScenarios(true);
ContextView contextView = new LocalFrameContextView()
.located( 0, 0 );
SeleniumContext seleniumContext = new SeleniumContext();
Selenium selenium = SeleniumConfiguration.defaultSelenium();
Format[] formats = new Format[] { new SeleniumContextOutput(seleniumContext), CONSOLE, WEB_DRIVER_HTML };
SeleniumStepMonitor stepMonitor = new SeleniumStepMonitor(contextView, seleniumContext,
crossReference.getStepMonitor());
@Override
public Configuration configuration() {
StoryReporterBuilder reporterBuilder = new StoryReporterBuilder()
.withCodeLocation(CodeLocations.codeLocationFromClass(this.getClass()))
.withDefaultFormats()
.withFailureTrace(true)
.withFailureTraceCompression(true)
.withDefaultFormats()
.withFormats(formats)
.withCrossReference(crossReference);
Configuration configuration = new SeleniumConfiguration()
.useSelenium( selenium )
.useSeleniumContext(seleniumContext)
.usePendingStepStrategy(pendingStepStrategy)
.useStoryControls(new StoryControls().doResetStateBeforeScenario(false)).useStepMonitor(stepMonitor)
.useStoryLoader(new LoadFromClasspath(this.getClass()))
.useStoryReporterBuilder(reporterBuilder)
.useParameterConverters(parameterConverters);
return configuration;
}
@Override
public InjectableStepsFactory stepsFactory() {
this.context = new SpringApplicationContextFactory("classpath:TestWebStories-context.xml").createApplicationContext();
return new SpringStepsFactory(configuration(), this.context);
}
/**
* The stories to be exeucted is being configured in the spring context
*
* <pre>
* {@code
* <util:list id="storyPaths" value-type="java.lang.String">
* <!-- the stories are listed here -->
* </util:list>
* }
* </pre>
*/
@Override
protected List<String> storyPaths() {
List<String> storyPaths1 = (List<String>) this.context.getBean( "webStoryPaths" );
List<String> allPaths = new ArrayList<String>();
for( String aPath: storyPaths1 )
{
List<String> storyPaths = new StoryFinder().findPaths( codeLocationFromPath( "{{YOUR_ROOT_PATH_TO_STORIES}}" ), aPath, "" );
allPaths.addAll( storyPaths );
}
return allPaths;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment