[JVM] Accessing metadata like test name

405 views
Skip to first unread message

Brian Jackson

unread,
Dec 23, 2013, 2:43:32 PM12/23/13
to cu...@googlegroups.com
I'm trying to use cucumber-jvm with SauceLabs. I'm initializing the RemoteWebDriver but I can't find a way to lookup the name of the test that is currently executing, ideally a descriptive name that captures the feature, scenario, and any example data that is being used. Is it possible to get that info, for example in the @Before method, in cucumber-jvm at this time? 

Aslak Hellesøy

unread,
Dec 23, 2013, 3:31:26 PM12/23/13
to cu...@googlegroups.com

On Monday, 23 December 2013 at 19:43, Brian Jackson wrote:

I'm trying to use cucumber-jvm with SauceLabs. I'm initializing the RemoteWebDriver but I can't find a way to lookup the name of the test that is currently executing, ideally a descriptive name that captures the feature, scenario, and any example data that is being used. Is it possible to get that info, for example in the @Before method, in cucumber-jvm at this time? 
You can declare a cucumber.api.Scenario parameter in the before hook, and Cucumber will pass you one. 

Why do you need to get the metadata?

Aslak

--
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/groups/opt_out.

Brian Jackson

unread,
Dec 29, 2013, 10:46:47 PM12/29/13
to cu...@googlegroups.com

On Monday, December 23, 2013 3:31:26 PM UTC-5, Aslak Hellesøy wrote:

On Monday, 23 December 2013 at 19:43, Brian Jackson wrote:

I'm trying to use cucumber-jvm with SauceLabs. I'm initializing the RemoteWebDriver but I can't find a way to lookup the name of the test that is currently executing, ideally a descriptive name that captures the feature, scenario, and any example data that is being used. Is it possible to get that info, for example in the @Before method, in cucumber-jvm at this time? 
You can declare a cucumber.api.Scenario parameter in the before hook, and Cucumber will pass you one. 

Why do you need to get the metadata?

I need the scenario name to set the "name" Capability on the RemoteWebDriver so that my SauceLabs tests have a nice name. Below is an excerpt of the hack I have so far, but its not great for several reasons. For example in a Scenario Outline it only includes the Example text and not the name of the Scenario Outline or Feature. I'd like that all concatenated together so on SauceLabs I can see the Feature/Scenario Outline/Example that was run for a particular execution.
 
    @Before
    public void initSelenium(Scenario scenario) throws Exception {

        // TODO: Find the scenario name
        String scenarioName = ((ExecutionUnitRunner)getPrivateField(getPrivateField(scenario, "reporter"), "executionUnitRunner")).getName();

        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setCapability("version", "5");
        capabilities.setCapability("platform", Platform.XP);
        capabilities.setCapability("name", scenarioName);
        this.driver = new RemoteWebDriver(
                    new URL("http://"+authentication.getUsername()+":"+authentication.getAccessKey()+"@ondemand.saucelabs.com:80/wd/hub"),
                    capabilities);
    }

    private static Object getPrivateField(Object subject, String fieldName) throws NoSuchFieldException, IllegalAccessException {
        Field f = subject.getClass().getDeclaredField(fieldName);
        f.setAccessible(true);
        return f.get(subject);
    }



Brian Jackson

unread,
Jan 12, 2014, 9:52:48 PM1/12/14
to cu...@googlegroups.com
On Monday, December 23, 2013 3:31:26 PM UTC-5, Aslak Hellesøy wrote:

On Monday, 23 December 2013 at 19:43, Brian Jackson wrote:

I'm trying to use cucumber-jvm with SauceLabs. I'm initializing the RemoteWebDriver but I can't find a way to lookup the name of the test that is currently executing, ideally a descriptive name that captures the feature, scenario, and any example data that is being used. Is it possible to get that info, for example in the @Before method, in cucumber-jvm at this time? 
You can declare a cucumber.api.Scenario parameter in the before hook, and Cucumber will pass you one. 

Why do you need to get the metadata?

Aslak

I need the scenario name to set the "name" Capability on the RemoteWebDriver so that my SauceLabs tests have a nice name. Below is an excerpt of the hack I have so far, but its not great for several reasons. For example in a Scenario Outline it only includes the Example text and not the name of the Scenario Outline or Feature. I'd like that all concatenated together so on SauceLabs I can see the Feature/Scenario Outline/Example that was run for a particular execution.

Björn Rasmusson

unread,
Jan 13, 2014, 4:30:05 AM1/13/14
to cu...@googlegroups.com
Brian Jackson wrote:
On Monday, December 23, 2013 3:31:26 PM UTC-5, Aslak Hellesøy wrote:

On Monday, 23 December 2013 at 19:43, Brian Jackson wrote:

I'm trying to use cucumber-jvm with SauceLabs. I'm initializing the RemoteWebDriver but I can't find a way to lookup the name of the test that is currently executing, ideally a descriptive name that captures the feature, scenario, and any example data that is being used. Is it possible to get that info, for example in the @Before method, in cucumber-jvm at this time? 
You can declare a cucumber.api.Scenario parameter in the before hook, and Cucumber will pass you one. 

Why do you need to get the metadata?

Aslak

I need the scenario name to set the "name" Capability on the RemoteWebDriver so that my SauceLabs tests have a nice name. Below is an excerpt of the hack I have so far, but its not great for several reasons. For example in a Scenario Outline it only includes the Example text and not the name of the Scenario Outline or Feature. I'd like that all concatenated together so on SauceLabs I can see the Feature/Scenario Outline/Example that was run for a particular execution.

What you would like to get your hands on is the gherkin.formatter.model.Scenario of the current executing scenario. Its getId() method returns the "<feature>;<scenario outline>;<example group>;<line>" string for examples scenarios (available from gherkin-2.12.1/cucumber-jvm-1.1.5). The gherkin.formatter.model.Scenario instance is sent to the formatters in the calls to the startOfScenarioLifecycle() and scenario() methods. But the gherkin.formatter.model.Scenario instance is not easily accessible from the cucumber.api.Scenario instance sent to the before hook (that means, when you hacked your way to the ExecutionUnitRunner, calling getGherkinModel() on its private field cucumberScenario would return the the gherkin.formatter.model.Scenario).

Best Regards
Björn
 

rkap...@gmail.com

unread,
Jan 24, 2014, 5:26:53 PM1/24/14
to cu...@googlegroups.com

Is there a possibility to expose the scenario metadata in the Before and After hooks?
  Here is our use case.
   Our scenarios are bit complex and not possible to hard code setup/test data in step definitions.
   So basically we want to read the test data based on the scenario name, feature, tag e.t.c
   (We understand Gherkin does not enforce unique feature/scenario names, but we can follow coding standards to keep it unique )
   We really need this information.
   
   In addition to the above we want to run our tests from cli.

   Unfortunately the above hack is not working when running from cli.
   The "reporter" field as null in Before, After hooks.
    We kind of stuck.

Thanks
-Ramesh Kapa

Björn Rasmusson

unread,
Feb 23, 2014, 1:34:58 PM2/23/14
to cu...@googlegroups.com
rkap...@gmail.com wrote:

Is there a possibility to expose the scenario metadata in the Before and After hooks?
  Here is our use case.
   Our scenarios are bit complex and not possible to hard code setup/test data in step definitions.
   So basically we want to read the test data based on the scenario name, feature, tag e.t.c
   (We understand Gherkin does not enforce unique feature/scenario names, but we can follow coding standards to keep it unique )
   We really need this information.
   
   In addition to the above we want to run our tests from cli.

   Unfortunately the above hack is not working when running from cli.
   The "reporter" field as null in Before, After hooks.
    We kind of stuck.

Thanks
-Ramesh Kapa
   
On Monday, January 13, 2014 4:30:05 AM UTC-5, Björn Rasmusson wrote:
Brian Jackson wrote:
On Monday, December 23, 2013 3:31:26 PM UTC-5, Aslak Hellesøy wrote:

On Monday, 23 December 2013 at 19:43, Brian Jackson wrote:

I'm trying to use cucumber-jvm with SauceLabs. I'm initializing the RemoteWebDriver but I can't find a way to lookup the name of the test that is currently executing, ideally a descriptive name that captures the feature, scenario, and any example data that is being used. Is it possible to get that info, for example in the @Before method, in cucumber-jvm at this time? 
You can declare a cucumber.api.Scenario parameter in the before hook, and Cucumber will pass you one. 

Why do you need to get the metadata?

Aslak

I need the scenario name to set the "name" Capability on the RemoteWebDriver so that my SauceLabs tests have a nice name. Below is an excerpt of the hack I have so far, but its not great for several reasons. For example in a Scenario Outline it only includes the Example text and not the name of the Scenario Outline or Feature. I'd like that all concatenated together so on SauceLabs I can see the Feature/Scenario Outline/Example that was run for a particular execution.

FYI, the Scenario name is now available through the cucumber.api.Scenario parameter in the before hook. For the Feature/Scenario Outline/Example concatenation (the Scenario id), there is a pending pull request to also make it available through the same parameter.

Best regards
Björn

 
Reply all
Reply to author
Forward
0 new messages