Passing scenario context between multiple step definition classes












0















So, I started developing a framework using Cucumber/TestNG/Java/selenium
I have a Context class that saves the scenariocontext with the help of enums in the form of key value pair
Referenced from here



My issue is that:
For a particular scenario in a feature, Step definitions are defined in multiple classes:




Sample feature




Feature: A feature
Scenario: Scenario
Given Statement 1
Then Statement 2



Class1




Class firstDef{
TestRunner test;
public firstDef(TestRunner test){
this.test = test
}
Brain context = new Brain();
@Given("Statement1")
void method1(){
}
}



Class 2




Class secondDef{
TestRunner test;
public secondDef(TestRunner test){
this.test = test
}
Brain context = new Brain();
@Given("Statement2")
void method1(){
}
}



TestRunner class




Class TestRunner{
//some code
@Test
public method1(){
//some code
}
}


So,



The brain class object for every step-definition will be different, this doesn't help as I want the context to be same throughout the scenario



Even if I instantiate the Brain in Runner class, the instance will be new for every instance of the test class



To overcome this, one possible solution that I have thought of is Serialization and de-serialization



In the @BeforeClass method, I will have:



File f = new File(path);
if(!f.exists()){
Brain context = new Brain();
FileOutputStream fos = new FileOutputStream(name);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(context);
}


Then I can deserialize wherever I want the context and serialize again after making changes to same reference variable



Is the above method correct or is there a better way to overcome the same problem










share|improve this question























  • Use DI with constructor injection in the step definition classes by using the cucumber picocontainer. For a scenario the DI container will inject the same instance of the object. Also why are u passing the testrunner to the step definition class?

    – Grasshopper
    Nov 22 '18 at 12:28











  • @Grasshopper , Thanks. I didnt know about the pico-container. Scenarios are working fine now

    – Akash Singh
    Nov 22 '18 at 21:25











  • About the testrunner, My WebDriverManager is not completed so I am fetching the driver instance straight from the TestRunner class by a method. Not a good way but I look forward to rectifying that soon

    – Akash Singh
    Nov 22 '18 at 21:26











  • Google ThreadLocal to store the driver instance specific to a thread. Ideal for parallel running too.

    – Grasshopper
    Nov 23 '18 at 2:12
















0















So, I started developing a framework using Cucumber/TestNG/Java/selenium
I have a Context class that saves the scenariocontext with the help of enums in the form of key value pair
Referenced from here



My issue is that:
For a particular scenario in a feature, Step definitions are defined in multiple classes:




Sample feature




Feature: A feature
Scenario: Scenario
Given Statement 1
Then Statement 2



Class1




Class firstDef{
TestRunner test;
public firstDef(TestRunner test){
this.test = test
}
Brain context = new Brain();
@Given("Statement1")
void method1(){
}
}



Class 2




Class secondDef{
TestRunner test;
public secondDef(TestRunner test){
this.test = test
}
Brain context = new Brain();
@Given("Statement2")
void method1(){
}
}



TestRunner class




Class TestRunner{
//some code
@Test
public method1(){
//some code
}
}


So,



The brain class object for every step-definition will be different, this doesn't help as I want the context to be same throughout the scenario



Even if I instantiate the Brain in Runner class, the instance will be new for every instance of the test class



To overcome this, one possible solution that I have thought of is Serialization and de-serialization



In the @BeforeClass method, I will have:



File f = new File(path);
if(!f.exists()){
Brain context = new Brain();
FileOutputStream fos = new FileOutputStream(name);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(context);
}


Then I can deserialize wherever I want the context and serialize again after making changes to same reference variable



Is the above method correct or is there a better way to overcome the same problem










share|improve this question























  • Use DI with constructor injection in the step definition classes by using the cucumber picocontainer. For a scenario the DI container will inject the same instance of the object. Also why are u passing the testrunner to the step definition class?

    – Grasshopper
    Nov 22 '18 at 12:28











  • @Grasshopper , Thanks. I didnt know about the pico-container. Scenarios are working fine now

    – Akash Singh
    Nov 22 '18 at 21:25











  • About the testrunner, My WebDriverManager is not completed so I am fetching the driver instance straight from the TestRunner class by a method. Not a good way but I look forward to rectifying that soon

    – Akash Singh
    Nov 22 '18 at 21:26











  • Google ThreadLocal to store the driver instance specific to a thread. Ideal for parallel running too.

    – Grasshopper
    Nov 23 '18 at 2:12














0












0








0


1






So, I started developing a framework using Cucumber/TestNG/Java/selenium
I have a Context class that saves the scenariocontext with the help of enums in the form of key value pair
Referenced from here



My issue is that:
For a particular scenario in a feature, Step definitions are defined in multiple classes:




Sample feature




Feature: A feature
Scenario: Scenario
Given Statement 1
Then Statement 2



Class1




Class firstDef{
TestRunner test;
public firstDef(TestRunner test){
this.test = test
}
Brain context = new Brain();
@Given("Statement1")
void method1(){
}
}



Class 2




Class secondDef{
TestRunner test;
public secondDef(TestRunner test){
this.test = test
}
Brain context = new Brain();
@Given("Statement2")
void method1(){
}
}



TestRunner class




Class TestRunner{
//some code
@Test
public method1(){
//some code
}
}


So,



The brain class object for every step-definition will be different, this doesn't help as I want the context to be same throughout the scenario



Even if I instantiate the Brain in Runner class, the instance will be new for every instance of the test class



To overcome this, one possible solution that I have thought of is Serialization and de-serialization



In the @BeforeClass method, I will have:



File f = new File(path);
if(!f.exists()){
Brain context = new Brain();
FileOutputStream fos = new FileOutputStream(name);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(context);
}


Then I can deserialize wherever I want the context and serialize again after making changes to same reference variable



Is the above method correct or is there a better way to overcome the same problem










share|improve this question














So, I started developing a framework using Cucumber/TestNG/Java/selenium
I have a Context class that saves the scenariocontext with the help of enums in the form of key value pair
Referenced from here



My issue is that:
For a particular scenario in a feature, Step definitions are defined in multiple classes:




Sample feature




Feature: A feature
Scenario: Scenario
Given Statement 1
Then Statement 2



Class1




Class firstDef{
TestRunner test;
public firstDef(TestRunner test){
this.test = test
}
Brain context = new Brain();
@Given("Statement1")
void method1(){
}
}



Class 2




Class secondDef{
TestRunner test;
public secondDef(TestRunner test){
this.test = test
}
Brain context = new Brain();
@Given("Statement2")
void method1(){
}
}



TestRunner class




Class TestRunner{
//some code
@Test
public method1(){
//some code
}
}


So,



The brain class object for every step-definition will be different, this doesn't help as I want the context to be same throughout the scenario



Even if I instantiate the Brain in Runner class, the instance will be new for every instance of the test class



To overcome this, one possible solution that I have thought of is Serialization and de-serialization



In the @BeforeClass method, I will have:



File f = new File(path);
if(!f.exists()){
Brain context = new Brain();
FileOutputStream fos = new FileOutputStream(name);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(context);
}


Then I can deserialize wherever I want the context and serialize again after making changes to same reference variable



Is the above method correct or is there a better way to overcome the same problem







java automation cucumber testng






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 9:22









Akash SinghAkash Singh

11




11













  • Use DI with constructor injection in the step definition classes by using the cucumber picocontainer. For a scenario the DI container will inject the same instance of the object. Also why are u passing the testrunner to the step definition class?

    – Grasshopper
    Nov 22 '18 at 12:28











  • @Grasshopper , Thanks. I didnt know about the pico-container. Scenarios are working fine now

    – Akash Singh
    Nov 22 '18 at 21:25











  • About the testrunner, My WebDriverManager is not completed so I am fetching the driver instance straight from the TestRunner class by a method. Not a good way but I look forward to rectifying that soon

    – Akash Singh
    Nov 22 '18 at 21:26











  • Google ThreadLocal to store the driver instance specific to a thread. Ideal for parallel running too.

    – Grasshopper
    Nov 23 '18 at 2:12



















  • Use DI with constructor injection in the step definition classes by using the cucumber picocontainer. For a scenario the DI container will inject the same instance of the object. Also why are u passing the testrunner to the step definition class?

    – Grasshopper
    Nov 22 '18 at 12:28











  • @Grasshopper , Thanks. I didnt know about the pico-container. Scenarios are working fine now

    – Akash Singh
    Nov 22 '18 at 21:25











  • About the testrunner, My WebDriverManager is not completed so I am fetching the driver instance straight from the TestRunner class by a method. Not a good way but I look forward to rectifying that soon

    – Akash Singh
    Nov 22 '18 at 21:26











  • Google ThreadLocal to store the driver instance specific to a thread. Ideal for parallel running too.

    – Grasshopper
    Nov 23 '18 at 2:12

















Use DI with constructor injection in the step definition classes by using the cucumber picocontainer. For a scenario the DI container will inject the same instance of the object. Also why are u passing the testrunner to the step definition class?

– Grasshopper
Nov 22 '18 at 12:28





Use DI with constructor injection in the step definition classes by using the cucumber picocontainer. For a scenario the DI container will inject the same instance of the object. Also why are u passing the testrunner to the step definition class?

– Grasshopper
Nov 22 '18 at 12:28













@Grasshopper , Thanks. I didnt know about the pico-container. Scenarios are working fine now

– Akash Singh
Nov 22 '18 at 21:25





@Grasshopper , Thanks. I didnt know about the pico-container. Scenarios are working fine now

– Akash Singh
Nov 22 '18 at 21:25













About the testrunner, My WebDriverManager is not completed so I am fetching the driver instance straight from the TestRunner class by a method. Not a good way but I look forward to rectifying that soon

– Akash Singh
Nov 22 '18 at 21:26





About the testrunner, My WebDriverManager is not completed so I am fetching the driver instance straight from the TestRunner class by a method. Not a good way but I look forward to rectifying that soon

– Akash Singh
Nov 22 '18 at 21:26













Google ThreadLocal to store the driver instance specific to a thread. Ideal for parallel running too.

– Grasshopper
Nov 23 '18 at 2:12





Google ThreadLocal to store the driver instance specific to a thread. Ideal for parallel running too.

– Grasshopper
Nov 23 '18 at 2:12












1 Answer
1






active

oldest

votes


















0














Constructor injection with Cucumber PicoContainer works like a charm for the above problem



Just add the dependency:



<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-picocontainer -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>


to your pom.xml(For Maven projects) and pass the HashMap class's reference variable through multiple step definition classes and it will remain the same over a scenario.



Please have a go through this article for detailed explaination






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427554%2fpassing-scenario-context-between-multiple-step-definition-classes%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Constructor injection with Cucumber PicoContainer works like a charm for the above problem



    Just add the dependency:



    <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-picocontainer -->
    <dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-picocontainer</artifactId>
    <version>1.2.5</version>
    <scope>test</scope>
    </dependency>


    to your pom.xml(For Maven projects) and pass the HashMap class's reference variable through multiple step definition classes and it will remain the same over a scenario.



    Please have a go through this article for detailed explaination






    share|improve this answer




























      0














      Constructor injection with Cucumber PicoContainer works like a charm for the above problem



      Just add the dependency:



      <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-picocontainer -->
      <dependency>
      <groupId>info.cukes</groupId>
      <artifactId>cucumber-picocontainer</artifactId>
      <version>1.2.5</version>
      <scope>test</scope>
      </dependency>


      to your pom.xml(For Maven projects) and pass the HashMap class's reference variable through multiple step definition classes and it will remain the same over a scenario.



      Please have a go through this article for detailed explaination






      share|improve this answer


























        0












        0








        0







        Constructor injection with Cucumber PicoContainer works like a charm for the above problem



        Just add the dependency:



        <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-picocontainer -->
        <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-picocontainer</artifactId>
        <version>1.2.5</version>
        <scope>test</scope>
        </dependency>


        to your pom.xml(For Maven projects) and pass the HashMap class's reference variable through multiple step definition classes and it will remain the same over a scenario.



        Please have a go through this article for detailed explaination






        share|improve this answer













        Constructor injection with Cucumber PicoContainer works like a charm for the above problem



        Just add the dependency:



        <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-picocontainer -->
        <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-picocontainer</artifactId>
        <version>1.2.5</version>
        <scope>test</scope>
        </dependency>


        to your pom.xml(For Maven projects) and pass the HashMap class's reference variable through multiple step definition classes and it will remain the same over a scenario.



        Please have a go through this article for detailed explaination







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 28 '18 at 9:52









        Akash SinghAkash Singh

        11




        11






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427554%2fpassing-scenario-context-between-multiple-step-definition-classes%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python