How to use SerenityRunner class of net.serenitybdd.junit.runners package

Best Serenity JUnit code snippet using net.serenitybdd.junit.runners.SerenityRunner

Run Serenity JUnit automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1package net.serenitybdd.junit5.extension;
2
3import com.google.inject.Key;
4import net.serenitybdd.core.injectors.EnvironmentDependencyInjector;
5import net.serenitybdd.junit5.counter.TestCounter;
6import net.serenitybdd.junit5.guice.JUnitInjectors;
7import net.thucydides.core.steps.BaseStepListener;
8import net.thucydides.core.steps.Listeners;
9import net.thucydides.core.steps.StepEventBus;
10import net.thucydides.core.steps.StepListener;
11import org.junit.jupiter.api.extension.AfterAllCallback;
12import org.junit.jupiter.api.extension.BeforeAllCallback;
13import org.junit.jupiter.api.extension.BeforeEachCallback;
14import org.junit.jupiter.api.extension.ExtensionContext;
15
16import static net.serenitybdd.core.environment.ConfiguredEnvironment.getConfiguration;
17import static net.thucydides.core.steps.StepEventBus.getEventBus;
18
19// Junit4: net.serenitybdd.junit.runners.SerenityRunner.initStepEventBus
20// Junit4: net.serenitybdd.junit.runners.SerenityRunner.initListeners
21// (no separate net.serenitybdd.junit.runners.SerenityRunner.initListenersUsing as pages will be configured via net.serenitybdd.junit.extension.page.SerenityPageExtension)
22public class SerenityExtension implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback {
23
24    @Override
25    public void beforeAll(final ExtensionContext extensionContext) {
26        getEventBus().clear();
27
28        registerListenersOnEventBus(
29                createBaseStepListener(),
30                Listeners.getLoggingListener(),
31                testCountListener());
32    }
33
34    @Override
35    public void beforeEach(final ExtensionContext extensionContext) {
36        injectEnvironmentVariablesInto(extensionContext.getRequiredTestInstance());
37    }
38
39    @Override
40    // JUnit4: net.serenitybdd.junit.runners.SerenityRunner.run
41    public void afterAll(final ExtensionContext extensionContext) {
42        StepEventBus.getEventBus().dropAllListeners();
43    }
44
45    private BaseStepListener createBaseStepListener() {
46        return Listeners.getBaseStepListener().withOutputDirectory(getConfiguration().getOutputDirectory());
47    }
48
49    private void registerListenersOnEventBus(final StepListener... stepListeners) {
50        for (StepListener currentStepListener : stepListeners) {
51            getEventBus().registerListener(currentStepListener);
52        }
53    }
54
55    private StepListener testCountListener() {
56        return JUnitInjectors.getInjector().getInstance(Key.get(StepListener.class, TestCounter.class));
57    }
58
59    private void injectEnvironmentVariablesInto(final Object testCase) {
60        new EnvironmentDependencyInjector().injectDependenciesInto(testCase);
61    }
62}
63
Full Screen
copy
1package org.fasttrackit.Util;
2
3import net.serenitybdd.junit.runners.SerenityParameterizedRunner;
4import net.serenitybdd.junit.runners.SerenityRunner;
5import net.thucydides.core.annotations.Managed;
6import net.thucydides.core.webdriver.WebDriverFacade;
7import org.junit.Before;
8import org.junit.runner.RunWith;
9import org.openqa.selenium.WebDriver;
10
11@RunWith(SerenityRunner.class)
12    public class BaseTest  {
13
14    @Managed(uniqueSession = false)
15    private WebDriver webDriver;
16
17    @Before
18    public void setup(){
19       webDriver.manage().window().maximize();
20    }
21
22}
23
Full Screen
copy
1package com.dinosaurs.features.search;
2
3import com.dinosaurs.steps.serenity.EndUserSteps;
4import net.serenitybdd.junit.runners.SerenityParameterizedRunner;
5import net.serenitybdd.junit.runners.SerenityRunner;
6import net.thucydides.core.annotations.Managed;
7import net.thucydides.core.annotations.Steps;
8import net.thucydides.junit.annotations.UseTestDataFrom;
9import org.junit.Test;
10import org.junit.runner.RunWith;
11import org.openqa.selenium.WebDriver;
12
13import static org.junit.Assert.assertTrue;
14
15//@RunWith(SerenityParameterizedRunner.class)
16@RunWith(SerenityRunner.class)
17@UseTestDataFrom("src/test/resources/login_good_data.csv")
18public class TestLoginGood {
19
20    @Managed(uniqueSession = true)
21    public WebDriver webdriver;
22
23    private String word, description;
24
25    @Steps
26    public EndUserSteps anna;
27
28    @Test
29    public void login_good_data() throws InterruptedException {
30//        webdriver.get("https://www.scs.ubbcluj.ro/webmail/");
31//        webdriver.findElement(By.id("rcmloginuser")).sendKeys(word);
32//        webdriver.findElement(By.id("rcmloginpwd")).sendKeys(description);
33//        webdriver.findElement(By.id("rcmloginsubmit")).click();
34
35        anna.is_the_home_page();
36//        anna.login_action(word, description);
37        anna.login_action("cgir2476","ebb54#24d5");
38
39        Thread.sleep(2000);
40//        assertTrue(webdriver.findElement(By.id("rcmbtn103")).isDisplayed());
41        anna.assertLoginSuccess();
42    }
43}
44
45
46
Full Screen
copy
1<!--SERENITY DEPENDENCIES-->
2        <dependency>
3            <groupId>net.serenity-bdd</groupId>
4            <artifactId>serenity-core</artifactId>
5            <version>1.2.5-rc.6</version>
6        </dependency>
7        <dependency>
8        <groupId>net.serenity-bdd</groupId>
9        <artifactId>serenity-junit</artifactId>
10        <version>1.2.5-rc.6</version>
11        </dependency>
12        <dependency>
13            <groupId>net.serenity-bdd</groupId>
14            <artifactId>serenity-cucumber</artifactId>
15            <version>1.1.29</version>
16            <scope>test</scope>
17        </dependency>
18        <dependency>
19            <groupId>org.slf4j</groupId>
20            <artifactId>slf4j-simple</artifactId>
21            <version>1.7.25</version>
22        </dependency>
23        <!--SERENITY DEPENDENCIES-->
24
Full Screen
copy
1<properties>
2    <cucumber.version>1.2.5</cucumber.version>
3    <serenity.version>1.2.5-rc.6</serenity.version>
4    <serenity.cucumber.version>1.1.29</serenity.cucumber.version>
5       <java.version>8</java.version>
6   </properties>
7
Full Screen
copy
1<plugin>
2                 <groupId>net.serenity-bdd.maven.plugins</groupId>
3                 <artifactId>serenity-maven-plugin</artifactId>
4                 <version>1.1.29</version>
5                 <executions>
6                     <execution>
7                         <id>serenity-reports</id>
8                         <phase>post-integration-test</phase>
9                         <goals>
10                             <goal>aggregate</goal>
11                         </goals>
12                     </execution>
13                 </executions>
14             </plugin>
15
Full Screen
copy
1[main] INFO net.serenitybdd.core.Serenity - 
2
3        __    _____ _____ ____ _____   ____   _    ____  ____  _____ ____  
4  _     \ \  |_   _| ____/ ___|_   _| |  _ \ / \  / ___|/ ___|| ____|  _ \ 
5 (_)_____| |   | | |  _| \___ \ | |   | |_) / _ \ \___ \\___ \|  _| | | | |
6  _|_____| |   | | | |___ ___) || |   |  __/ ___ \ ___) |___) | |___| |_| |
7 (_)     | |   |_| |_____|____/ |_|   |_| /_/   \_\____/|____/|_____|____/ 
8        /_/          
9
Full Screen
copy
1<!--SERENITY DEPENDENCIES-->
2        <dependency>
3            <groupId>net.serenity-bdd</groupId>
4            <artifactId>serenity-core</artifactId>
5            <version>1.2.5-rc.6</version>
6        </dependency>
7        <dependency>
8        <groupId>net.serenity-bdd</groupId>
9        <artifactId>serenity-junit</artifactId>
10        <version>1.2.5-rc.6</version>
11        </dependency>
12        <dependency>
13            <groupId>net.serenity-bdd</groupId>
14            <artifactId>serenity-cucumber</artifactId>
15            <version>1.1.29</version>
16            <scope>test</scope>
17        </dependency>
18        <dependency>
19            <groupId>org.slf4j</groupId>
20            <artifactId>slf4j-simple</artifactId>
21            <version>1.7.25</version>
22        </dependency>
23        <!--SERENITY DEPENDENCIES-->
24
Full Screen
copy
1<properties>
2    <cucumber.version>1.2.5</cucumber.version>
3    <serenity.version>1.2.5-rc.6</serenity.version>
4    <serenity.cucumber.version>1.1.29</serenity.cucumber.version>
5       <java.version>8</java.version>
6   </properties>
7
Full Screen
copy
1<plugin>
2                 <groupId>net.serenity-bdd.maven.plugins</groupId>
3                 <artifactId>serenity-maven-plugin</artifactId>
4                 <version>1.1.29</version>
5                 <executions>
6                     <execution>
7                         <id>serenity-reports</id>
8                         <phase>post-integration-test</phase>
9                         <goals>
10                             <goal>aggregate</goal>
11                         </goals>
12                     </execution>
13                 </executions>
14             </plugin>
15
Full Screen
copy
1[main] INFO net.serenitybdd.core.Serenity - 
2
3        __    _____ _____ ____ _____   ____   _    ____  ____  _____ ____  
4  _     \ \  |_   _| ____/ ___|_   _| |  _ \ / \  / ___|/ ___|| ____|  _ \ 
5 (_)_____| |   | | |  _| \___ \ | |   | |_) / _ \ \___ \\___ \|  _| | | | |
6  _|_____| |   | | | |___ ___) || |   |  __/ ___ \ ___) |___) | |___| |_| |
7 (_)     | |   |_| |_____|____/ |_|   |_| /_/   \_\____/|____/|_____|____/ 
8        /_/          
9
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)