Re: [Cucumber] [Cucumber-JVM] Getting annotation on feature level instead of scenario level

347 views
Skip to first unread message

aslak hellesoy

unread,
Apr 15, 2013, 8:11:41 AM4/15/13
to Cucumber Users



On Mon, Apr 15, 2013 at 12:22 PM, Gergely Brautigam <skarl...@gmail.com> wrote:
Hi guys.

I want to find out if it possible to get the annotations on a Feature instead of a Scenario. 

We call them tags, not annotations (just want to avoid confusion)
 

I know I can access some part with an @After and the Scenario. Which contains the Annotation on a Scenario which I found after some digging. 

However I can't seem to be able to find the name of the feature and annotations on it. 

For example:

@After
public static runAfterFeatures(Scenario sc) {
    logger.log("These are the annotations on a feature: " + sc.annotationsOnAFeature);
}

Where as a feature looks like this: 

@Oneannotation @web @storynumber @quick
Feature: Do something interesting

And I would like to be able to get these annotations from it. 

Why? What do you need it for?
 

Do I have to implement my own filter for that?

What do you mean by filter? What API do you have in mind?

Aslak
 
If so...where do I register the implementor with cucumber? 

thanks very much!

Greg

--
-- Rules --
 
1) Please prefix the subject with [Ruby], [JVM] or [JS].
2) Please use interleaved answers http://en.wikipedia.org/wiki/Posting_style#Interleaved_style
3) If you have a question, don't reply to an existing message. Start a new topic instead.
 
You received this message because you are subscribed to the Google Groups Cukes group. To post to this group, send email to cu...@googlegroups.com. To unsubscribe from this group, send email to cukes+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/cukes?hl=en
---
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/groups/opt_out.
 
 

Gergely Brautigam

unread,
Apr 15, 2013, 8:27:50 AM4/15/13
to cu...@googlegroups.com
Hi Aslak. 

Because based on the Tag I'd like to perform a certain action for example put it into a custom report.

I'd like to generate a report based on tags corresponding to JIRA issues / stories to have a view of how many tests there are for a ticket.

Sorry I might have tried to say Listener not filter. Like a listener for jUnit test execution for example.

G.

Gergely Brautigam

unread,
Apr 15, 2013, 8:54:25 AM4/15/13
to cu...@googlegroups.com
And before you might think that we can use @After("Tagname") that's not an option unfortunately because Tagname is unknown and differs from story to story obviously. 

It's the title of the ticket. :)

G.

Gergely Brautigam

unread,
Apr 15, 2013, 3:19:06 PM4/15/13
to cu...@googlegroups.com
Well if there is no other way... hacking it shall be... 

There you go, how is that for a solution? 

            Field f = scenario.getClass().getDeclaredField("reporter");
            f.setAccessible(true);
            JUnitReporter reporter = (JUnitReporter) f.get(scenario);

            Field executionRunnerField = reporter.getClass().getDeclaredField("executionUnitRunner");
            executionRunnerField.setAccessible(true);
            ExecutionUnitRunner executionUnitRunner = (ExecutionUnitRunner) executionRunnerField.get(reporter);

            Field cucumberScenarioField = executionUnitRunner.getClass().getDeclaredField("cucumberScenario");
            cucumberScenarioField.setAccessible(true);
            CucumberScenario cucumberScenario = (CucumberScenario) cucumberScenarioField.get(executionUnitRunner);

            Field cucumberBackgroundField = cucumberScenario.getClass().getDeclaredField("cucumberBackground");
            cucumberBackgroundField.setAccessible(true);
            CucumberBackground cucumberBackground = (CucumberBackground) cucumberBackgroundField.get(cucumberScenario);

            Field cucumberFeatureField = cucumberBackground.getClass().getSuperclass().getDeclaredField("cucumberFeature");
            cucumberFeatureField.setAccessible(true);
            CucumberFeature cucumberFeature = (CucumberFeature) cucumberFeatureField.get(cucumberBackground);

This will get you EVERYTHING from the executed feature. Name, tags, Scenarios, Whatever you want. 

Put this into an AFTER clause in any class you want and there you go. You will have everything you ever wanted from your test case. Every bit of information is in that cucumberFeature. 

Enjoy your days in hell.

G.

aslak hellesoy

unread,
Apr 15, 2013, 3:26:57 PM4/15/13
to Cucumber Users
On Mon, Apr 15, 2013 at 8:19 PM, Gergely Brautigam <skarl...@gmail.com> wrote:
Well if there is no other way... hacking it shall be... 


Be patient. Allow a few days to wait for a response. After that, send a reminder. We're all busy here.

Gergely Brautigam

unread,
Apr 16, 2013, 3:40:49 AM4/16/13
to cu...@googlegroups.com
Ohh yes of course. No problem. I just didn't have the time to wait unfortunately. :( BUT!! That doesn't mean I'm not awaiting some awesome better solution!! :) 

So definitely I'll wait. I just needed a temporary solution for as long as I wait. 

Thanks very much for all the help you can give me!

Gergely.

aslak hellesoy

unread,
Apr 16, 2013, 1:01:25 PM4/16/13
to Cucumber Users
Please don't top post. It's too hard to have an email discussion that way. See Rule 2.


On Mon, Apr 15, 2013 at 1:27 PM, Gergely Brautigam <skarl...@gmail.com> wrote:
Hi Aslak. 

Because based on the Tag I'd like to perform a certain action for example put it into a custom report.


So you're using hooks to write a report? That's what formatters/listeners are for.

Hooks are designed to set up tear down state - not perform reporting.

Aslak

Gergely Brautigam

unread,
Apr 17, 2013, 3:22:19 AM4/17/13
to cu...@googlegroups.com
Top post? I don't know what that is, I'll look into it.

Well see my first question then, which said how do I implement another solution like a listener.

I even used the exact same word.

"Sorry I might have tried to say Listener not filter. Like a listener for jUnit test execution for example.".

Matt Wynne

unread,
Apr 17, 2013, 3:59:35 AM4/17/13
to cu...@googlegroups.com
On 17 Apr 2013, at 08:22, Gergely Brautigam <skarl...@gmail.com> wrote:

Top post? I don't know what that is, I'll look into it.

Here are the rules for this forum, pasted from the bottom of this email:

-- Rules --
 
1) Please prefix the subject with [Ruby], [JVM] or [JS].
2) Please use interleaved answers http://en.wikipedia.org/wiki/Posting_style#Interleaved_style
3) If you have a question, don't reply to an existing message. Start a new topic instead.

Rule 2 is about top-posting. Please read the linked article.


Gergely Brautigam

unread,
Apr 18, 2013, 3:25:17 AM4/18/13
to cu...@googlegroups.com
Excellent. Thanks. However this still does not solve my problem. :) 

As far as I could find I could only come up with listeners for ruby but not the java version one. :( 

So if there is a resource I maybe missed or something could you please point me into the right direction? :)

Thanks!
G.

Matt Wynne

unread,
Apr 18, 2013, 3:27:42 AM4/18/13
to cu...@googlegroups.com
Fixing Rule 2. Please stop breaking this rule, it's there for a reason.

On 18 Apr 2013, at 08:25, Gergely Brautigam <skarl...@gmail.com> wrote:

On Wednesday, 17 April 2013 08:59:35 UTC+1, Matt Wynne wrote:

On 17 Apr 2013, at 08:22, Gergely Brautigam <skarl...@gmail.com> wrote:

Top post? I don't know what that is, I'll look into it.

Here are the rules for this forum, pasted from the bottom of this email:

-- Rules --
 
1) Please prefix the subject with [Ruby], [JVM] or [JS].
2) Please use interleaved answers http://en.wikipedia.org/wiki/Posting_style#Interleaved_style
3) If you have a question, don't reply to an existing message. Start a new topic instead.

Rule 2 is about top-posting. Please read the linked article.

Excellent. Thanks. However this still does not solve my problem. :) 

As far as I could find I could only come up with listeners for ruby but not the java version one. :( 

So if there is a resource I maybe missed or something could you please point me into the right direction? :)

Thanks!

Gergely Brautigam

unread,
Apr 18, 2013, 5:45:01 AM4/18/13
to cu...@googlegroups.com


On Thursday, 18 April 2013 08:27:42 UTC+1, Matt Wynne wrote:
Fixing Rule 2. Please stop breaking this rule, it's there for a reason.


Like this?

Karan

unread,
Dec 27, 2013, 12:50:05 AM12/27/13
to cu...@googlegroups.com
Hi,

I am getting error on the statement : 
Field executionRunnerField = reporter.getClass().getDeclaredField("executionUnitRunner");

that executionUnitRunner is empty..

From where do we get it.?? or do i need to write something else at its place ??

Thanks

aslak hellesoy

unread,
Dec 27, 2013, 1:50:21 AM12/27/13
to Cucumber Users
On Fri, Dec 27, 2013 at 5:50 AM, Karan <karanwa...@gmail.com> wrote:
Hi,

I am getting error on the statement : 
Field executionRunnerField = reporter.getClass().getDeclaredField("executionUnitRunner");

that executionUnitRunner is empty..

From where do we get it.?? or do i need to write something else at its place ??


Why are you trying to access this field? If we understood what you're trying to achieve we could perhaps suggest an alternative.

Aslak
 
Reply all
Reply to author
Forward
0 new messages