NarendraR
NarendraR

Reputation: 7708

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)?

Upvotes: 0

Views: 3843

Answers (2)

Grasshopper
Grasshopper

Reputation: 9058

What you need is a webdriver which is shared between scenarios and does not close until all the scenarios are run. You can do it by storing the webdriver instance in a static variable and adding a shutdown hook to the instance.

Have a look at this which gives you such a driver class -- SharedWebDriver

If you use it with a DI container such as PicoContainer you can inject the driver in the constructor. Else you can create an instance.

Also you should have a look at ScenarioOutline to condense the multiple login scenarios.

Upvotes: 0

MikeJRamsey56
MikeJRamsey56

Reputation: 2819

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

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

Upvotes: 1

Related Questions