How to use addClass method of cucumber.runtime.SerenityObjectFactory class

Best Serenity Cucumber code snippet using cucumber.runtime.SerenityObjectFactory.addClass

Run Serenity Cucumber automation tests on LambdaTest cloud grid

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

copy
1package cucumber.runtime;
2
3import cucumber.api.java.ObjectFactory;
4import net.serenitybdd.core.Serenity;
5import net.thucydides.core.pages.Pages;
6import net.thucydides.core.webdriver.ThucydidesWebDriverSupport;
7
8import java.lang.reflect.Constructor;
9import java.util.*;
10
11/**
12 * Serenity factory for cucumber tests.
13 *
14 * @author Liviu Carausu (liviu.carausu@gmail.com).
15 */
16
17public class SerenityObjectFactory implements ObjectFactory {
18
19    private final Set<Class<?>> classes = Collections.synchronizedSet(new HashSet<Class<?>>());
20
21    private final Map<Class<?>, Object> instances = Collections.synchronizedMap(new HashMap<Class<?>, Object>());
22
23    public void start() {
24    }
25
26    public void stop() {
27        instances.clear();
28        Serenity.done(false);
29    }
30
31
32    @Override
33    public boolean addClass(Class<?> glueClass) {
34        classes.add(glueClass);
35        return true;
36    }
37
38    public <T> T getInstance(Class<T> type) {
39        T instance = type.cast(instances.get(type));
40        if (instance == null) {
41            instance = cacheNewInstance(type);
42        }
43        return instance;
44    }
45
46    /**
47     * Tries to instantiate the type using an empty constructor, if it does not work, tries to instantiate
48     * using a constructor with a Pages parameter.
49     */
50    private <T> T cacheNewInstance(Class<T> type) {
51        T instance = newInstance(type);
52        instances.put(type, instance);
53        return instance;
54    }
55
56    private <T> T newInstance(Class<T> type) {
57        T instance;
58        try {
59            if (hasConstructorWithPagesParameter(type)) {
60                instance = createNewPageEnabledStepCandidate(type);
61            } else {
62                Constructor<T> constructor = type.getConstructor();
63                instance = constructor.newInstance();
64            }
65        } catch (ReflectiveOperationException e) {
66            throw new CucumberException(String.format("Failed to instantiate %s - this class doesn't have an empty or a page enabled constructor\"", type), e);
67        }
68        Serenity.initializeWithNoStepListener(instance).throwExceptionsImmediately();
69        return instance;
70    }
71
72    private <T> T createNewPageEnabledStepCandidate(final Class<T> type) {
73        T newInstance;
74        try {
75            ThucydidesWebDriverSupport.initialize();
76            Pages pageFactory = ThucydidesWebDriverSupport.getPages();
77            Class[] constructorArgs = new Class[1];
78            constructorArgs[0] = Pages.class;
79            Constructor<T> constructor = type.getConstructor(constructorArgs);
80            newInstance = constructor.newInstance(pageFactory);
81            Serenity.initialize(newInstance);
82        } catch (Exception e) {
83            throw new CucumberException(String.format("%s doesn't have an empty or a page enabled constructor.", type), e);
84        }
85        return newInstance;
86    }
87
88    private boolean hasConstructorWithPagesParameter(Class<?> type) {
89        Class[] constructorArgs = new Class[1];
90        constructorArgs[0] = Pages.class;
91        try {
92            type.getConstructor(constructorArgs);
93        } catch (NoSuchMethodException e) {
94            return false;
95        }
96        return true;
97    }
98
99
100}
101
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

Most used method in SerenityObjectFactory

Trigger addClass code on LambdaTest Cloud Grid

Execute automation tests with addClass on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
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)