Best Serenity jBehave code snippet using net.serenitybdd.jbehave.SerenityStories.setEnvironmentVariables
Source:SerenityStories.java  
...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    }...Source:WhenRunningASelectionOfJBehaveStories.java  
...61        // Given62        systemConfiguration.getEnvironmentVariables().setProperty("metafilter", "+environment uat");63        SerenityStories uatStory = new ASampleBehaviorForUatOnly(systemConfiguration);64//        uatStory.setSystemConfiguration(systemConfiguration);65        uatStory.setEnvironmentVariables(environmentVariables);66        // When67        run(uatStory);68        // Then69        List<TestOutcome> outcomes = loadTestOutcomes();70        assertThat(outcomes.size(), is(1));71        assertThat(outcomes.get(0).getResult(), is(TestResult.SUCCESS));72    }73    @Test74    public void should_be_possible_to_define_multiple_metafilters() {75        // Given76        systemConfiguration.getEnvironmentVariables().setProperty("metafilter", "+environment uat, +speed fast");77        SerenityStories allStories = new SerenityStories(systemConfiguration);78        allStories.setSystemConfiguration(systemConfiguration);79        allStories.setEnvironmentVariables(environmentVariables);80        // When81        run(allStories);82        // Then83        List<TestOutcome> outcomes = loadTestOutcomes();84        assertThat(excludeSkippedAndIgnored(outcomes).size(), is(4));85    }86    @Test87    public void should_be_possible_to_define_metafilters_in_annotations() {88        // Given89        SerenityStories allStories = new WithAnAnnotatedMetafilter();90        allStories.setSystemConfiguration(systemConfiguration);91        allStories.setEnvironmentVariables(environmentVariables);92        // When93        run(allStories);94        // Then95        List<TestOutcome> outcomes = loadTestOutcomes();96        assertThat(excludeSkippedAndIgnored(outcomes).size(), is(2));97    }98    @Test99    public void system_property_metafilters_should_override_annotations() {100        // Given101        systemConfiguration.getEnvironmentVariables().setProperty("metafilter", "+environment uat, +speed fast");102        SerenityStories allStories = new WithAnAnnotatedMetafilter();103        allStories.setSystemConfiguration(systemConfiguration);104        allStories.setEnvironmentVariables(environmentVariables);105        // When106        run(allStories);107        // Then108        List<TestOutcome> outcomes = loadTestOutcomes();109        assertThat(excludeSkippedAndIgnored(outcomes).size(), is(4));110    }111    @Test112    public void should_be_possible_to_define_groovy_metafilters() {113        // Given114        systemConfiguration.getEnvironmentVariables().setProperty("webdriver.driver", "htmlunit");115        systemConfiguration.getEnvironmentVariables().setProperty("metafilter", "groovy:('a'=='b')");116        SerenityStories allStories = new SerenityStories(systemConfiguration);117        allStories.setSystemConfiguration(systemConfiguration);118        allStories.setEnvironmentVariables(environmentVariables);119        // When120        run(allStories);121        // Then122        List<TestOutcome> outcomes = loadTestOutcomes();123        assertThat(excludeSkippedAndIgnored(outcomes).size(), is(0));124    }125    @Test126    public void environment_specific_stories_should_not_be_executed_if_a_filter_excludes_it() {127        systemConfiguration.getEnvironmentVariables().setProperty("metafilter", "-environment uat");128        // Given129        SerenityStories uatStory = new ASampleBehaviorForUatOnly(systemConfiguration);130        // When131        run(uatStory);132        // Then...Source:WhenRunningWebJBehaveStories.java  
...32    @Test33    public void should_be_able_to_set_thucydides_properties_in_the_base_test() {34        // Given35        SerenityStories story = new APassingWebTestSampleWithThucydidesPropertiesDefined(systemConfiguration);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();...Source:WhenRunningJBehaveStoriesWithScreenshots.java  
...26    @Test27    public void web_tests_should_take_screenshots_with_multiple_scenarios() {28        // Given29        SerenityStories story = newStory("aPassingBehaviorWithSeleniumAndSeveralScenarios.story");30        story.setEnvironmentVariables(environmentVariables);31        // When32        run(story);33        // Then34        List<TestOutcome> outcomes = loadTestOutcomes();35        assertThat(outcomes.get(0).getScreenshots().size(), greaterThan(0));36    }37    @Test38    public void web_tests_should_take_screenshots_for_multiple_tests() {39        // Given40        SerenityStories story = newStory("*PassingBehaviorWithSeleniumAndSeveralScenarios.story");41        // When42        run(story);43        // Then44        List<TestOutcome> outcomes = loadTestOutcomes();45        assertThat(outcomes.get(0).getScreenshots().size(), greaterThan(0));46        assertThat(outcomes.get(1).getScreenshots().size(), greaterThan(0));47        assertThat(outcomes.get(2).getScreenshots().size(), greaterThan(0));48        assertThat(outcomes.get(3).getScreenshots().size(), greaterThan(0));49    }50    @Test51    public void web_tests_should_take_screenshots_with_nested_step_libraries() {52        // Given53        SerenityStories story = newStory("**/aPassingWebTestSampleWithNestedSteps.story");54        story.setEnvironmentVariables(environmentVariables);55        // When56        run(story);57        // Then58        List<TestOutcome> outcomes = loadTestOutcomes();59        assertThat(outcomes.get(0).getScreenshots().size(), greaterThan(0));60    }61}...Source:WhenRunningWebJBehaveStoriesUsingAdvancedBrowserManagement.java  
...35    @Test36    public void should_be_able_to_specify_the_browser_in_the_base_test() {37        // Given38        SerenityStories story = new APassingWebTestSampleWithASpecifiedBrowser();39        story.setEnvironmentVariables(environmentVariables);40        System.out.println("Output dir = " + outputDirectory.getAbsolutePath());41        // When42        run(story);43        // Then44        System.out.println("Loading from output dir = " + outputDirectory.getAbsolutePath());45        List<TestOutcome> outcomes = loadTestOutcomes();46        assertThat(outcomes.get(0).getResult(), is(TestResult.SUCCESS));47    }48}...Source:AStorySample.java  
2import net.thucydides.core.util.EnvironmentVariables;3import net.thucydides.core.webdriver.DriverConfiguration;4public class AStorySample extends SerenityStories {5    public AStorySample(String storyName, DriverConfiguration configuration, EnvironmentVariables environmentVariables) {6        this.setEnvironmentVariables(environmentVariables);7        setSystemConfiguration(configuration);8        findStoriesCalled(storyName);9    }10}...setEnvironmentVariables
Using AI Code Generation
1setEnvironmentVariables();2setEnvironmentVariables();3setEnvironmentVariables();4setEnvironmentVariables();5setEnvironmentVariables();6setEnvironmentVariables();7setEnvironmentVariables();8setEnvironmentVariables();9setEnvironmentVariables();setEnvironmentVariables
Using AI Code Generation
1public class SerenityStories extends SerenityStory {2    public void run() throws Throwable {3        setEnvironmentVariables();4        super.run();5    }6}7public class SerenityStories extends SerenityStory {8    public void run() throws Throwable {9        setEnvironmentVariables();10        super.run();11    }12}setEnvironmentVariables
Using AI Code Generation
1public void setEnvironmentVariables() {2    String testUrl = System.getProperty("testUrl");3    if (testUrl != null && !testUrl.isEmpty()) {4        Serenity.setSessionVariable("baseUrl").to(testUrl);5    }6}7public void setEnvironmentVariables() {8    String testUrl = System.getProperty("testUrl");9    if (testUrl != null && !testUrl.isEmpty()) {10        Serenity.setSessionVariable("baseUrl").to(testUrl);11    }12}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!!
