Before/After hooks for multiple feature files run one after the other

1,605 views
Skip to first unread message

SPK

unread,
Jul 29, 2015, 2:40:18 AM7/29/15
to Cukes
Hi,

I am new to cucumber and have created a java code using cucumber.

I created two different feature files. For both the features files, I have a corresponding step definition files, each having a Before and After hook.

When I run the code, I observed that before hook for one step file is called and then immediately before hook for the other step file is called, see output below

@search
Feature: Search for a part
BeforeResult
BeforeHome
AfterResult
AfterHome

  @search
  Scenario Outline: ...
  Given ...
  When ...
  Then ...


Any explanation for the above behavior as I expected the output to be
@search
Feature: Search for a part
BeforeHome
AfterHome

  @search
  Scenario Outline: ...
  Given ...
  When ...
  Then ...

Regards,
SPK

Paolo Ambrosio

unread,
Jul 29, 2015, 3:13:12 AM7/29/15
to cu...@googlegroups.com


On 29 Jul 2015 07:40, "SPK" <swap...@gmail.com> wrote:
>
> Hi,
>
> I am new to cucumber and have created a java code using cucumber.
>
> I created two different feature files. For both the features files, I have a corresponding step definition files, each having a Before and After hook.

Step definitions are global (by design) to help building a ubiquitous language. Hooks are also global.

> When I run the code, I observed that before hook for one step file is called and then immediately before hook for the other step file is called, see output below
>
> @search
> Feature: Search for a part
> BeforeResult
> BeforeHome
> AfterResult
> AfterHome

This seems correct. The only way to filter execution of hooks is with tags.

What you are trying to do looks like an anti-pattern to me... can you explain better what the hooks are meant to do?

>   @search
>   Scenario Outline: ...
>   Given ...
>   When ...
>   Then ...
>
>
> Any explanation for the above behavior as I expected the output to be
> @search
> Feature: Search for a part
> BeforeHome
> AfterHome
>
>   @search
>   Scenario Outline: ...
>   Given ...
>   When ...
>   Then ...
>
> Regards,
> SPK
>

> --
> Posting rules: http://cukes.info/posting-rules.html
> ---
> You received this message because you are subscribed to the Google Groups "Cukes" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to cukes+un...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

SPK

unread,
Jul 29, 2015, 5:31:29 AM7/29/15
to Cukes, pa...@ambrosio.name
Hi Paolo,

Thanks for the response


On Wednesday, July 29, 2015 at 12:43:12 PM UTC+5:30, Paolo Ambrosio wrote:


On 29 Jul 2015 07:40, "SPK" <swap...@gmail.com> wrote:
>
> Hi,
>
> I am new to cucumber and have created a java code using cucumber.
>
> I created two different feature files. For both the features files, I have a corresponding step definition files, each having a Before and After hook.

Step definitions are global (by design) to help building a ubiquitous language. Hooks are also global.

> When I run the code, I observed that before hook for one step file is called and then immediately before hook for the other step file is called, see output below
>
> @search
> Feature: Search for a part
> BeforeResult
> BeforeHome
> AfterResult
> AfterHome

This seems correct. The only way to filter execution of hooks is with tags.

What you are trying to do looks like an anti-pattern to me... can you explain better what the hooks are meant to do?

 
   I have now associated the hooks for both the step files with the corresponding feature tags and the code works fine. What I am trying to do is creating a FireFoxDriver (Selenium 2) instance in before hook and setting it to null in the after hook.

I have also observed that w/o associating hooks with the tags, two firefox instances open ie one blank and the one which runs the test. Maybe due to the global nature of hooks.   

Paolo Ambrosio

unread,
Aug 15, 2015, 6:06:02 AM8/15/15
to Swapnaja ., cu...@googlegroups.com
Hi,

I'm bringing this back into the list to be useful to everyone that
knows how to use Google ;-)

On Thu, Aug 13, 2015 at 8:40 AM, Swapnaja . <swap...@gmail.com> wrote:
> Hi Paolo,
>
> Thanks and sorry for the late reply but I was caught up in some other work.
>
> I modified my code to use the SharedDriver and indeed there is ever only one
> instance of FF open which run all the tests.
> However, I noticed that when all the tests are done, the FF does not close.

That usually happens in Selenium when the browser is too new. On the
latest version of Firefox I had to upgrade Selenium from 2.45.0 to
2.47.0 in the root pom.xml for the browser to close:

<selenium.version>2.47.0</selenium.version>

> https://github.com/cucumber/cucumber-jvm/blob/master/examples/java-webbit-websockets-selenium/src/test/java/cucumber/examples/java/websockets/SharedDriver.java
>
> I tried to understand the code above and could not understand under what
> circumstances would the super.close() in the close() method be called?
> Also, when would the run() method of the anonymous thread be called?

The static initialization block adds a shutdown hook on the JVM to
close the browser
(http://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#addShutdownHook-java.lang.Thread-):

static {
Runtime.getRuntime().addShutdownHook(CLOSE_THREAD);
}

When the JVM is shutting down (for example after all tests are run),
CLOSE_THREAD will be executed closing the Firefox browser window.

Please note that in Java static variables are initialised and static
blocks are executed when the class is loaded. If the code does not
reference it, it won't happen (e.g. for tests that do not rely on a
Web browser).

If you are using the Spring Framework alredy, the same effect can be
achieved by something like (using XML bean declaration):

<bean class="org.openqa.selenium.firefox.FirefoxDriver"
destroy-method="close" lazy-init="true"/>


Paolo

> Looking forward to your reply
>
> Thanks
>
> On Thu, Jul 30, 2015 at 1:21 AM, Paolo Ambrosio <pa...@ambrosio.name> wrote:
>> This example uses Selenium to test a web application and you can see
>> how to use hooks to open a Firefox instance:
>>
>>
>> https://github.com/cucumber/cucumber-jvm/tree/v1.2.4/examples/java-webbit-websockets-selenium
>>
>> As you can see, there is only one hook defined in
>> src/test/java/cucumber/examples/java/websockets/SharedDriver.java

SPK

unread,
Aug 18, 2015, 11:22:11 AM8/18/15
to Cukes, swap...@gmail.com
Hi Paolo,

I upgraded Selenium from 2.46.0 to 2.47.1. Now, when I run the program, the Firefox browser (FF 39.0) gets closed after all the tests are done.

Thanks for making it clear

Have a good day

Regards,
SPK
Reply all
Reply to author
Forward
0 new messages