Search code examples
cucumbercucumber-junitcucumber-java

How to use hooks in cucumber?


I am new in cucumber framework. I'm automating login scenarios using cucumber with java. Following is my feature file -

enter image description here

And following is step definition file -

enter image description here

I wanted to execute setUp() methods once before all scenarios (methods implemented for the same) and tearDown() method after all scenarios. I've used cucumber @Before and @After hooks (may be not in correct way).

But I'm seeing same result as before not using these. It open 4 instances of browser for all four scenarios one by one and then do execute them. Is there any way to execute all these scenarios only with one browser instance (call setUp() method once for all scenarios)?


Solution

  • @Before
    public void setup() {
        if (driver == null) {
           ...//What you have
        }
        driver.manage().deleteAllCookies();
    }
    

    To start don't close the browser in the @After hook.