Best Serenity jBehave code snippet using net.serenitybdd.jbehave.SerenityStories.getSystemConfiguration
Source:SerenityReportingRunner.java
...58 ConfigurableEmbedder embedder) {59 this.configurableEmbedder = embedder;60 ExtendedEmbedder extendedEmbedder = new ExtendedEmbedder(this.configurableEmbedder.configuredEmbedder());61 extendedEmbedder.getEmbedderMonitor().subscribe(new ReportingEmbedderMonitor(62 ((SerenityStories) embedder).getSystemConfiguration(), extendedEmbedder));63 this.configurableEmbedder.useEmbedder(extendedEmbedder);64 this.testClass = testClass;65 this.environmentVariables = environmentVariablesFrom(configurableEmbedder);66 }67 protected List<Description> getDescriptions() {68 if (storyDescriptions == null) {69 storyDescriptions = buildDescriptionFromStories();70 }71 return storyDescriptions;72 }73 protected Configuration getConfiguration() {74 if (configuration == null) {75 configuration = getConfiguredEmbedder().configuration();76 }...
Source:SerenityStories.java
...60 }61 @Override62 public Configuration configuration() {63 if (configuration == null) {64 net.thucydides.core.webdriver.DriverConfiguration<DriverConfiguration> thucydidesConfiguration = getSystemConfiguration();65 if (environmentVariables != null) {66 thucydidesConfiguration = thucydidesConfiguration.withEnvironmentVariables(environmentVariables);67 }68 configuration = SerenityJBehave.defaultConfiguration(thucydidesConfiguration, formats, this);69 }70 return configuration;71 }72 @Override73 public InjectableStepsFactory stepsFactory() {74 return SerenityStepFactory.withStepsFromPackage(getRootPackage(), configuration()).andClassLoader(getClassLoader());75 }76 /**77 * The class loader used to obtain the JBehave and Step implementation classes.78 * You normally don't need to worry about this, but you may need to override it if your application79 * is doing funny business with the class loaders.80 */81 public ClassLoader getClassLoader() {82 return Thread.currentThread().getContextClassLoader();83 }84 @Override85 public List<String> storyPaths() {86 Set<String> storyPaths = new HashSet<>();87 List<String> pathExpressions = getStoryPathExpressions();88 StoryFinder storyFinder = new StoryFinder();89 for (String pathExpression : pathExpressions) {90 if (absolutePath(pathExpression)) {91 storyPaths.add(pathExpression);92 }93 for (URL classpathRootUrl : allClasspathRoots()) {94 storyPaths.addAll(storyFinder.findPaths(classpathRootUrl, pathExpression, ""));95 }96 storyPaths = removeDuplicatesFrom(storyPaths);97 storyPaths = pruneGivenStoriesFrom(storyPaths);98 }99 return sorted(storyPaths);100 }101 private List<String> sorted(Set<String> storyPaths) {102 List<String> sortedStories = Lists.newArrayList(storyPaths);103 Collections.sort(sortedStories);104 return sortedStories;105 }106 private Set<String> removeDuplicatesFrom(Set<String> storyPaths) {107 Set<String> trimmedPaths = new HashSet<>();108 for(String storyPath : storyPaths) {109 if (!thereExistsALongerVersionOf(storyPath, storyPaths)) {110 trimmedPaths.add(storyPath);111 }112 }113 return trimmedPaths;114 }115 private boolean thereExistsALongerVersionOf(String storyPath, Set<String> storyPaths) {116 for(String existingPath : storyPaths) {117 if ((existingPath.endsWith("/" + storyPath)) || (existingPath.endsWith("\\" + storyPath))) {118 return true;119 }120 }121 return false;122 }123 private Set<String> pruneGivenStoriesFrom(Set<String> storyPaths) {124 List<String> filteredPaths = Lists.newArrayList(storyPaths);125 for (String skippedPrecondition : skippedPreconditions()) {126 filteredPaths = removeFrom(filteredPaths)127 .pathsNotStartingWith(skippedPrecondition)128 .and().pathsNotStartingWith("/" + skippedPrecondition)129 .filter();130 }131 return new HashSet<>(filteredPaths);132 }133 class FilterBuilder {134 private final List<String> paths;135 public FilterBuilder(List<String> paths) {136 this.paths = paths;137 }138 public FilterBuilder pathsNotStartingWith(String skippedPrecondition) {139 List<String> filteredPaths = new ArrayList<>();140 for (String path : paths) {141 if (!startsWith(skippedPrecondition, path)) {142 filteredPaths.add(path);143 }144 }145 return new FilterBuilder(filteredPaths);146 }147 public FilterBuilder and() {148 return this;149 }150 public List<String> filter() {151 return ImmutableList.copyOf(paths);152 }153 private boolean startsWith(String skippedPrecondition, String path) {154 return path.toLowerCase().startsWith(skippedPrecondition.toLowerCase());155 }156 }157 private FilterBuilder removeFrom(List<String> filteredPaths) {158 return new FilterBuilder(filteredPaths);159 }160 private List<String> skippedPreconditions() {161 return DEFAULT_GIVEN_STORY_PREFIX;162 }163 private boolean absolutePath(String pathExpression) {164 return (!pathExpression.contains("*"));165 }166 private Set<URL> allClasspathRoots() {167 try {168 Set<URL> baseRoots = new HashSet<>(Collections.list(getClassLoader().getResources(".")));169 return addGradleResourceRootsTo(baseRoots);170 } catch (IOException e) {171 throw new IllegalArgumentException("Could not load the classpath roots when looking for story files", e);172 }173 }174 private Set<URL> addGradleResourceRootsTo(Set<URL> baseRoots) throws MalformedURLException {175 Set<URL> rootsWithGradleResources = new HashSet<>(baseRoots);176 for (URL baseUrl : baseRoots) {177 String gradleResourceUrl = baseUrl.toString().replace("/build/classes/", "/build/resources/");178 rootsWithGradleResources.add(new URL(gradleResourceUrl));179 }180 return rootsWithGradleResources;181 }182 /**183 * The root package on the classpath containing the JBehave stories to be run.184 */185 protected String getRootPackage() {186 return RootPackage.forPackage(getClass().getPackage());187 }188 protected List<String> getStoryPathExpressions() {189 return Lists.newArrayList(Splitter.on(';').trimResults().omitEmptyStrings().split(getStoryPath()));190 }191 /**192 * The root package on the classpath containing the JBehave stories to be run.193 */194 protected String getStoryPath() {195 return (StringUtils.isEmpty(storyFolder)) ? storyNamePattern : storyFolder + "/" + storyNamePattern;196 }197 /**198 * Define the folder on the class path where the stories should be found199 */200 public void findStoriesIn(String storyFolder) {201 this.storyFolder = storyFolder;202 }203 public void useFormats(Format... formats) {204 this.formats = Arrays.asList(formats);205 }206 public void findStoriesCalled(String storyNames) {207 Set<String> storyPathElements = new StoryPathFinder(getEnvironmentVariables(), storyNames).findAllElements();208 storyNamePattern = Joiner.on(";").join(storyPathElements);209 }210 private String storyFilter;211 public void matchStories(String storyFilter) {212 this.storyFilter = storyFilter;213 }214 public String getStoryFilter() {215 return storyFilter;216 }217 /**218 * Use this to override the default ThucydidesWebdriverIntegration configuration - for testing purposes only.219 */220 protected void setSystemConfiguration(net.thucydides.core.webdriver.DriverConfiguration systemConfiguration) {221 this.systemConfiguration = systemConfiguration;222 }223 protected void setEnvironmentVariables(EnvironmentVariables environmentVariables) {224 this.environmentVariables = environmentVariables;225 }226 public net.thucydides.core.webdriver.DriverConfiguration getSystemConfiguration() {227 if (systemConfiguration == null) {228 systemConfiguration = WebDriverInjectors.getInjector().getInstance(net.thucydides.core.webdriver.DriverConfiguration.class);229 }230 return systemConfiguration;231 }232 protected void useDriver(String driver) {233 getSystemConfiguration().setIfUndefined(ThucydidesSystemProperty.DRIVER.getPropertyName(), driver);234 }235 protected void useUniqueSession() {236 getSystemConfiguration().setIfUndefined(ThucydidesSystemProperty.THUCYDIDES_USE_UNIQUE_BROWSER.getPropertyName(), "true");237 }238 public ThucydidesConfigurationBuilder runSerenity() {239 return new ThucydidesConfigurationBuilder(this);240 }241 public class ThucydidesConfigurationBuilder {242 private final SerenityStories serenityStories;243 public ThucydidesConfigurationBuilder(SerenityStories serenityStories) {244 this.serenityStories = serenityStories;245 }246 public ThucydidesConfigurationBuilder withDriver(String driver) {247 useDriver(driver);248 return this;249 }250 public ThucydidesPropertySetter withProperty(ThucydidesSystemProperty property) {251 return new ThucydidesPropertySetter(serenityStories, property);252 }253 public ThucydidesPropertySetter withProperty(String property) {254 return new ThucydidesPropertySetter(serenityStories, property);255 }256 public ThucydidesConfigurationBuilder inASingleSession() {257 useUniqueSession();258 return this;259 }260 }261 public class ThucydidesPropertySetter {262 private final SerenityStories serenityStories;263 private final String propertyToSet;264 public ThucydidesPropertySetter(SerenityStories serenityStories,265 ThucydidesSystemProperty propertyToSet) {266 this.serenityStories = serenityStories;267 this.propertyToSet = propertyToSet.getPropertyName();268 }269 public ThucydidesPropertySetter(SerenityStories serenityStories,270 String propertyToSet) {271 this.serenityStories = serenityStories;272 this.propertyToSet = propertyToSet;273 }274 public void setTo(boolean value) {275 serenityStories.getSystemConfiguration().setIfUndefined(propertyToSet,276 Boolean.toString(value));277 }278 public void setTo(String value) {279 serenityStories.getSystemConfiguration().setIfUndefined(propertyToSet, value);280 }281 public void setTo(Integer value) {282 serenityStories.getSystemConfiguration().setIfUndefined(propertyToSet,283 Integer.toString(value));284 }285 }286}...
Source:RunTestSuite.java
...70 e.printStackTrace();71 }72*/73 // dumpConfigToLog();74// driver.get(getSystemConfiguration().getBaseUrl() + "/sources/new");75 }76 /*77 private void createDashboardForTesting() {78 driver.get(getSystemConfiguration().getBaseUrl());79 WebElement dashboardPage = driver.findElement(By.cssSelector("nav.sidebar div a[class='sidebar--square'][href$='/dashboards"));80 dashboardPage.click();81 WebElement createDashboardButton = driver.findElement(By.xpath("//button[contains(text(),'Create Dashboard')]"));82 createDashboardButton.click();83 }*/84 private void dumpConfigToLog(){85 System.out.println("DEBUG dumpConfigToLog(): ");86 logger.info("SERENITY CONFIG");87 logger.info(" BASE URL: " + getSystemConfiguration().getBaseUrl());88 logger.info(" TEST COUNT: " + getSystemConfiguration().getCurrentTestCount());89 logger.info(" SCREEN:");90 logger.info(" WIDTH: " + driver.manage().window().getSize().width);91 logger.info(" HEIGHT: " + driver.manage().window().getSize().height);92 logger.info(" ENVARS: ");93 for(String key : getSystemConfiguration().getEnvironmentVariables().getKeys()){94 logger.info(" " + key + ": " + getSystemConfiguration().getEnvironmentVariables().getProperty(key));95 }96 }97 @AfterStories98 public void afterStories() {99 }100 @BeforeScenario101 public void setupProfile() throws IOException, URISyntaxException, InterruptedException {102 // all code for setting up Browser profile belongs in here - for example:103 /*104 * org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();105 * proxy.setHttpProxy("localhost:3128"); FirefoxProfile profile = new106 * FirefoxProfile(); profile.setPreference("network.proxy.type", 1);107 * profile.setPreference("network.proxy.http", "localhost");108 * profile.setPreference("network.proxy.http_port", 3128);...
Source:WhenRunningWebJBehaveStories.java
...36 story.setEnvironmentVariables(environmentVariables);37 // When38 run(story);39 // Then40 assertThat(story.getSystemConfiguration().getBaseUrl(), is("some-base-url"));41 assertThat(story.getSystemConfiguration().getElementTimeout(), is(5));42 assertThat(story.getSystemConfiguration().shouldUseAUniqueBrowser(), is(true));43 }44 @Test(expected = Throwable.class)45 public void stories_with_errors_run_in_junit_should_fail() {46 // Given47 SerenityStories failingStory = newStory("aFailingBehaviorWithSelenium.story");48 // When49 failingStory.run();50 assert !raisedErrors.isEmpty();51 }52 @Test53 public void stories_with_errors_in_one_scenario_should_still_run_subsequent_scenarios() {54 //environmentVariables.setProperty("restart.browser.each.scenario","true");55 // Given56 SerenityStories story = newStory("failingAndPassingBehaviorsWithSelenium.story");...
Source:AcceptanceTestSuite.java
...7@Metafilter("+my")8public class AcceptanceTestSuite extends SerenityStories {9 private DriverConfiguration configuration;10 public AcceptanceTestSuite() {11 configuration = super.getSystemConfiguration();12 configuration.setIfUndefined(WEBDRIVER_DRIVER.getPropertyName(), "provided");13 configuration.setIfUndefined(WEBDRIVER_PROVIDED_TYPE.getPropertyName(), "mydriver");14 configuration.setIfUndefined("webdriver.provided.mydriver", "architype2test.divers.ChromeWemDriver");15 }16}...
getSystemConfiguration
Using AI Code Generation
1import net.serenitybdd.jbehave.SerenityStories;2public class MyJBehaveStory extends SerenityStories {3 public MyJBehaveStory() {4 runSerenity().withDriver("chrome");5 }6}7import net.serenitybdd.jbehave.SerenityStory;8public class MyJBehaveStory extends SerenityStory {9 public MyJBehaveStory() {10 runSerenity().withDriver("chrome");11 }12}13import net.serenitybdd.jbehave.SerenityStory;14public class MyJBehaveStory extends SerenityStory {15 public MyJBehaveStory() {16 runSerenity().withDriver("chrome");17 }18}19import net.serenitybdd.jbehave.SerenityStory;20public class MyJBehaveStory extends SerenityStory {21 public MyJBehaveStory() {22 runSerenity().withDriver("chrome");23 }24}25import net.serenitybdd.jbehave.SerenityStory;26public class MyJBehaveStory extends SerenityStory {27 public MyJBehaveStory() {28 runSerenity().withDriver("chrome");29 }30}31import net.serenitybdd.jbehave.SerenityStory;32public class MyJBehaveStory extends SerenityStory {33 public MyJBehaveStory() {34 runSerenity().withDriver("chrome");35 }36}37import net.serenitybdd.jbehave.SerenityStory;38public class MyJBehaveStory extends SerenityStory {39 public MyJBehaveStory() {40 runSerenity().withDriver("chrome");41 }42}
getSystemConfiguration
Using AI Code Generation
1 public void testGetSystemConfiguration() {2 SerenityStories serenityStories = new SerenityStories();3 serenityStories.getSystemConfiguration();4 }5}6testGetSystemConfiguration(net.serenitybdd.jbehave.SerenityStoriesTest) Time elapsed: 0.002 sec <<< ERROR!7 at net.serenitybdd.jbehave.SerenityStories.getSystemConfiguration(SerenityStories.java:73)8 at net.serenitybdd.jbehave.SerenityStoriesTest.testGetSystemConfiguration(SerenityStoriesTest.java:9)9testGetSystemConfiguration(net.serenitybdd.jbehave.SerenityStoriesTest) Time elapsed: 0.002 sec <<< ERROR!10 at net.serenitybdd.jbehave.SerenityStories.getSystemConfiguration(SerenityStories.java:73)11 at net.serenitybdd.jbehave.SerenityStoriesTest.testGetSystemConfiguration(SerenityStoriesTest.java:9)
getSystemConfiguration
Using AI Code Generation
1 private String getSystemConfiguration(String property) {2 return SerenityStories.getSystemConfiguration(property);3 }4 public void configure() {5 String baseUrl = getSystemConfiguration("base.url");6 String username = getSystemConfiguration("username");7 String password = getSystemConfiguration("password");8 RestAssured.baseURI = baseUrl;9 RestAssured.authentication = basic(username, password);10 }11}12 public void run() throws Throwable {13 configure();14 super.run();15 }16}17 @Given("I am logged in to the application")18 public void givenIAmLoggedInToTheApplication() {19 String baseUrl = getSystemConfiguration("base.url");20 String username = getSystemConfiguration("username");21 String password = getSystemConfiguration("password");22 RestAssured.baseURI = baseUrl;23 RestAssured.authentication = basic(username, password);24 }
getSystemConfiguration
Using AI Code Generation
1SerenityStories stories = new SerenityStories();2Map<String, String> systemConfiguration = stories.getSystemConfiguration();3System.setProperty("webdriver.driver", systemConfiguration.get("webdriver.driver"));4System.setProperty("webdriver.chrome.driver", systemConfiguration.get("webdriver.chrome.driver"));5System.setProperty("webdriver.firefox.bin", systemConfiguration.get("webdriver.firefox.bin"));6System.setProperty("webdriver.firefox.profile", systemConfiguration.get("webdriver.firefox.profile"));7System.setProperty("webdriver.ie.driver", systemConfiguration.get("webdriver.ie.driver"));8System.setProperty("webdriver.edge.driver", systemConfiguration.get("webdriver.edge.driver"));9System.setProperty("webdriver.safari.noinstall", systemConfiguration.get("webdriver.safari.noinstall"));10System.setProperty("webdriver.safari.driver", systemConfiguration.get("webdriver.safari.driver"));11System.setProperty("webdriver.phantomjs.driver", systemConfiguration.get("webdriver.phantomjs.driver"));12System.setProperty("webdriver.opera.driver", systemConfiguration.get("webdriver.opera.driver"));13System.setProperty("webdriver.opera.binary", systemConfiguration.get("webdriver.opera.binary"));14System.setProperty("webdriver.opera.logfile", systemConfiguration.get("webdriver.opera.logfile"));15System.setProperty("webdriver.opera.port", systemConfiguration.get("webdriver.opera.port"));16System.setProperty("webdriver.opera.autostart", systemConfiguration.get("webdriver.opera.autostart"));17System.setProperty("webdriver.opera.profile", systemConfiguration.get("webdriver.opera.profile"));18System.setProperty("webdriver.opera.verbose", systemConfiguration.get("webdriver.opera.verbose"));19System.setProperty("webdriver.opera.args", systemConfiguration.get("webdriver.opera.args"));20System.setProperty("webdriver.opera.host", systemConfiguration.get("webdriver.opera.host"));21System.setProperty("webdriver.opera.display", systemConfiguration.get("webdriver.opera.display"));22System.setProperty("webdriver.opera.loglevel", systemConfiguration.get("webdriver.opera.loglevel"));23System.setProperty("webdriver.opera.logfile", systemConfiguration.get("webdriver.opera.logfile"));24System.setProperty("webdriver.opera.port", systemConfiguration.get("webdriver.opera.port"));25System.setProperty("webdriver.opera.autostart", systemConfiguration.get("webdriver.opera.autostart"));26System.setProperty("webdriver.opera.profile", systemConfiguration.get("webdriver.opera.profile"));27System.setProperty("webdriver.opera.verbose", systemConfiguration.get("webdriver.opera.verbose"));28System.setProperty("webdriver.opera.args", systemConfiguration.get("webdriver.opera.args"));
getSystemConfiguration
Using AI Code Generation
1System.setProperty("serenity.browser", getSystemConfiguration().getEnvironmentVariables().getProperty("browser"));2System.setProperty("serenity.browser", new SerenityStories().getMetaData().getProperty("browser"));3package net.serenitybdd.jbehave;4import java.util.ArrayList;5import java.util.List;6import org.jbehave.core.ConfigurableEmbedder;7import org.jbehave.core.configuration.Configuration;8import org.jbehave.core.configuration.MostUsefulConfiguration;9import org.jbehave.core.embedder.Embedder;10import org.jbehave.core.embedder.MetaFilter;11import org.jbehave.core.embedder.MetaMatcher;12import org.jbehave.core.embedder.MetaMatcherFilter;13import org.jbehave.core.embedder.MetaParam;14import org.jbehave.core.embedder.MetaParam.MetaParams;15import org.jbehave.core.embedder.MetaParam.MetaProperty;16import org.jbehave.core.embedder.MetaParam.PropertyBasedMetaParam;17import org.jbehave.core.embedder.MetaParam.PropertyBasedMetaParams;18import org.jbehave.core.embedder.MetaParam.PropertyBasedMetaProperty;19import org.jbehave.core.embedder.MetaParam.PropertyBasedMetaProperty.PropertyBasedMetaPropertyBuilder;20import org.jbehave.core.embedder.MetaParam.PropertyBasedMetaProperty.PropertyBasedMetaPropertyBuilder.PropertyBasedMetaPropertyBuilderImpl;21import org.jbehave.core.embedder.MetaParam.StoryBasedMetaParam;22import org.jbehave.core.embedder.MetaParam.StoryBasedMetaParams;23import org.jbehave.core.embedder.MetaParam.StoryBasedMetaProperty;24import org.jbehave.core.embedder.MetaParam.StoryBasedMetaProperty.StoryBasedMetaPropertyBuilder;25import org.jbehave.core.embedder.MetaParam.StoryBasedMetaProperty.StoryBasedMetaPropertyBuilder.StoryBasedMetaPropertyBuilderImpl;26import org.jbehave.core.embedder.MetaParam.StoryBasedMetaProperty.StoryBasedMetaPropertyBuilder.StoryBasedMetaPropertyBuilder
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!!