[Cucumber JVM] need feature and scenario name at run time

6,348 views
Skip to first unread message

Vladimir Sapozhnik

unread,
Feb 15, 2013, 5:22:31 PM2/15/13
to cu...@googlegroups.com
How can I get Feature name and Scenario name during run time, in After hook
When I used cuke4duke I was able to do it this way:
@After("@some_tag")
public void functionName(RubyObject rbObj) {
String fetureName = cukeJRubyUtils.getFeatureName(rbObj);
String scenarioName = cukeJRubyUtils.getScenarioName(rbObj);
}


aslak hellesoy

unread,
Feb 16, 2013, 3:12:59 AM2/16/13
to Cucumber Users
 This has been discussed quite often. Have you searched the discussions archives? Here is one thread:


Aslak

--
-- 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.
 
 

Vladimir Sapozhnik

unread,
Feb 16, 2013, 4:38:35 PM2/16/13
to cu...@googlegroups.com

I saw that you can add scenario object to After hook, but so far I didn't see it to have at least scenario name. I was trying to search for that, so far didn't find any good example.

Vladimir Sapozhnik

unread,
Feb 18, 2013, 3:54:35 PM2/18/13
to cu...@googlegroups.com
The only way I saw so far that presumably does what I need is described here:
The problem is that it uses aspectJ to do so, and I need one in Java. I never worked with aspectJ before, so not sure how to interpret it to Java, if there is a way.
None of the other search results gave me any clue on how to get this.
So far even using debugger I wasn't able to spot which class holds that info.

If you can point me to how it's done, please do.

Thank you

Vladimir

Abhay Bharti

unread,
Dec 9, 2013, 9:06:29 AM12/9/13
to cu...@googlegroups.com
Any pointer on reading Feature, Scenario and Step name using Java.

On Friday, 1 March 2013 05:12:13 UTC+5:30, Cynthia wrote:
Hi,

I am also trying to figure out how to call the Scenario name.  Before I was using ScenarioResult but I just upgraded and I saw that that class is no longer in the framework.  

I've been trying to call the Cucumber.Runtime classes, but so far no luck :(.  I've been trying to transverse through the ScenarioImpl and other classes; and I haven't been able to dig up anything.  Any pointers would be helpful

aslak hellesoy

unread,
Dec 9, 2013, 9:23:26 AM12/9/13
to Cucumber Users
On Mon, Dec 9, 2013 at 2:06 PM, Abhay Bharti <abhay...@gmail.com> wrote:
Any pointer on reading Feature, Scenario and Step name using Java.


What do you need it for?

Aslak
 

Abhay Bharti

unread,
Dec 9, 2013, 9:12:51 PM12/9/13
to cu...@googlegroups.com
I am creating a framework using Cucumber + Java. I have created customized HTML report & need Feature, Scenario & Step name to print in report.

Regards,
Abhay Bharti
Phone - +91 9999915562
Blog - www.lifegoeasy.blogspot.in


You received this message because you are subscribed to a topic in the Google Groups "Cukes" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cukes/iMsZ_VyaTl0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cukes+un...@googlegroups.com.

aslak hellesoy

unread,
Dec 10, 2013, 4:22:15 AM12/10/13
to Cucumber Users
Abhay,

We're trying to get everyone on the list to reply inline to make conversations easier to follow. Can you do that too please? See the three list rules in the email footer.


On Tue, Dec 10, 2013 at 2:12 AM, Abhay Bharti <abhay...@gmail.com> wrote:
I am creating a framework using Cucumber + Java. I have created customized HTML report & need Feature, Scenario & Step name to print in report.


Custom reports should be implemented by implementing the Reporter and Formatter interfaces. If you do, the feature and scenario names will be passed to you.


Just call feature.getName(), scenario.getName() etc.

Aslak

Abhay Bharti

unread,
Dec 23, 2013, 8:59:34 AM12/23/13
to cu...@googlegroups.com
 Hey Aslak,
  getName() method is not returning scenario & feature name.   

Add following import in my code -
  import gherkin.formatter.model.Feature;
  import gherkin.formatter.model.Scenario;

My code is as follows -
        Feature feat = null;
        Scenario sc = null;
        System.out.println("Feature Name"+sc.getName());
        System.out.println("Feature Name"+feat.getName());

When I run my code, it throws error - .Error java.lang.NullPointerException

Can you please suggest what's wrong with my code?

Note -
1. I have written this code inside Feature file step definition.java file
2. if i comment these lines, my script is working fine.

aslak hellesoy

unread,
Dec 23, 2013, 9:13:40 AM12/23/13
to Cucumber Users
On Mon, Dec 23, 2013 at 1:59 PM, Abhay Bharti <abhay...@gmail.com> wrote:
 Hey Aslak,
  getName() method is not returning scenario & feature name.   

Add following import in my code -
  import gherkin.formatter.model.Feature;
  import gherkin.formatter.model.Scenario;

My code is as follows -
        Feature feat = null;
        Scenario sc = null;
        System.out.println("Feature Name"+sc.getName());
        System.out.println("Feature Name"+feat.getName());

When I run my code, it throws error - .Error java.lang.NullPointerException

Can you please suggest what's wrong with my code?


If you're unable to answer that question yourself you probably lack a basic understanding of Java. It will be hard for you to achieve what you're trying to do until you gain a better understanding of Java.

In Java (and pretty much any OO language), declaring a variable without initialising it and then call a method on it will cause a NullPointerException.

You're not even supposed to instantiate Feature and Scenario yourself - they will be passed to you when you implement the Reporter and Formatter interfaces I pointed you to. I suggest you take a look at the source code of some of the built-in formatters.

Good luck.

Dan Reale

unread,
Oct 1, 2015, 4:41:24 PM10/1/15
to Cukes
@After
public void AfterTest(Scenario scenario) throws Exception
{

try 
{
if (scenario.isFailed())
   {
String testcasename = scenario.getName();
System.out.println("Name:" + scenario.getName());

Test Professional

unread,
Dec 16, 2016, 8:05:15 AM12/16/16
to Cukes
When I try using Before hooks with both Scenario and Feature, it throws an error 'Hooks must declare 0 or 1 arguments' How can I retrieve the Feature Name in this instance, I am able to retrieve Scenario Name. Do suggest how to get Feature Name for logging purpose.
Reply all
Reply to author
Forward
0 new messages