Search code examples
selenium-webdrivercucumbercucumber-jvmserenity-bddcucumber-serenity

serenity cucumber browser unique session per feature


I'm looking at serenity (the following versions)

<serenity.version>1.1.26</serenity.version>
 <serenity.maven.version>1.1.26</serenity.maven.version>
 <serenity.cucumber.version>1.1.5</serenity.cucumber.version>

I have feature files F1, F2, F3.

I'm looking for support to run all the scenarios in F1 (only) to run in a single browser session.

The scenarios in F2 and F3 can run in a "browser per scenario" mode.

How to achieve this?


Solution

  • Cucumber hooks do the job for you.

    import cucumber.annotation.After;
    import cucumber.annotation.Before;
    
    public static WebDriver DRIVER;
    
    @Before
    public void setUp() {
      // start browser if it does not exist yet
    }
    
    @After
    public void tearDown() {
      // clean cookies
    }
    

    Note that I use the cucumber before, not the JUnit before.Do make sure you have a reference to the DRIVER in your tests. The hooks wil run before and after each scenario (or example if you use scenario outline). If you want a specific setup for certain annotated features, for example:

    @slowtest
    Feature: F1 feature
    

    Then you can use:

    import cucumber.annotation.After;
    import cucumber.annotation.Before;
    
    public static WebDriver DRIVER;
    
    @Before("@slowtest")
    public void setUp() {
      // start browser if it does not exist yet
    }
    
    @After("@slowtest")
    public void tearDown() {
      // close browser or clean cookies, or....
    }
    

    Conclusion you can use cucumber hooks in combination with annotations in features for custom setup and teardown.