serenity-bdd / serenity-cucumber4-starter

48 stars 73 forks source link

How do you setup @BeforeAll and @AfterAll in the cucumber steps? #10

Open BartVanRaemdonck opened 4 years ago

BartVanRaemdonck commented 4 years ago

I tried it like this, but it doesn't work. Also with the JUnit4 BeforeClass and Afterclass.



public class CucumberTestSuite {

    @BeforeClass
    public static void setup(){
        System.out.println("IN BEFORE ALL ");
    }

    @Before
    public static void setup2(){
        System.out.println("IN BEFORE ALL ");
    }

    @AfterAll
    public static void tearDown() throws Exception {
        System.out.println("IN AFTER ALL ");
        BrowserMobProxy p = getProxy();
        p.stop();
        Local bsLocal = getBrowserStackLocal();
        bsLocal.stop();

    }

    @Given("The user navigates to the goedgevoel website")
    public void theUserNavigatesToTheGoedgevoelWebsite() {
        goedGevoelHomePage.navigateToIt();
    }

    @When("advertising cookies are set")
    public void advertisingCookiesAreSet() {
        defaultPage.setCookies();
    }

    @AfterAll()
    public void tearDown() throws Exception {
        System.out.println("IN AFTER ALL ");
        defaultPage.tearDown();
    }
}
wakaleo commented 4 years ago

@BeforeAll and @AfterAll have no equivalent in Cucumber.

BartVanRaemdonck commented 4 years ago

Is there an alternative? I want to close my local Browserstack proxy after my tests.

BartVanRaemdonck commented 4 years ago

So I should quit Serenity BDD and Cucumber just because it doesn't support @AfterAll ?

wakaleo commented 4 years ago

@AfterAll annotations have a number of potential issues in multi-threaded contexts (how do you know when all the features have finished?). I would probabl use a shutdown hook (https://www.geeksforgeeks.org/jvm-shutdown-hook-java/)

BartVanRaemdonck commented 4 years ago

Ok, thanks @wakaleo . For now I worked with this code:

@Before
    public void setup() {
      Runtime.getRuntime().addShutdownHook(new Thread(){
          @Override
                  public void run(){
              try {
                  System.out.println("Artificial After ALL");
                  stopBrowserStackLocal();
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      });

Maybe in the future the following can be developed in Serenity? https://medium.com/@pauloloboneto/cucumber-java-annotation-afterall-e-beforeall-c2bfea0e2203

BartVanRaemdonck commented 4 years ago

I still have a problem because I want to use Browserstack. And Browserstack uses a session for every test. I have to use BrowserMobProxy and Browserstack local to run some network tests. U need to initialize browserstack local at driver level for every session, so I can't use Serenity? Because I initialize my driver, at the start of the fist test with my provided driver, after the first test the browserstack session is closed, so the best solution here is to close the browserstack local instance, close the browsermob proxy instance and startup new ones, but for that the happen I just start a new driver instance? And that isn't possible with Serenity?