0

I am using Behat with Mink in PHP. I'm writing a class to create Json logs for every test run including failed/passed steps and so on.

Now we are using Scenario Outlines a lot because we run the tests on multiple websites and put the URLs into the examples section.

However I cannot get the title of the Scenario Node to put it into Json. I am using

$event->getScenario()->getTitle();

but that returns not the Scenario title but the example that is currently running. So if I have this

Scenario Outline: Scenario 1
Given I am on "<domain>"
Then I should see "test"

Examples:
|domain|
|www.webpage.com|

what I ultimately want in my report is the name of the Scenario Outline, 'Scenario 1'. But what is really returned is '|www.webpage.com|'.

Is there a way to get to the name of a Scenario Outline? It works fine on 'normal' Scenarios.

yeaitsme
  • 91
  • 1
  • 9

2 Answers2

1

Try something like:

$event->getScenario()->getOutlineTitle();

This should do the trick.

lauda
  • 4,153
  • 2
  • 14
  • 28
  • sadly no, there is an OutlineNode class but it doesn't have a title or anything. – yeaitsme Apr 26 '17 at 12:22
  • Updated the answer. If this also is not working please check this PR to see if you have it in your code https://github.com/Behat/Gherkin/pull/118/commits/5f1038abdf8e190db717b98ce8a4a49adc1c882f – lauda Apr 26 '17 at 14:01
  • Nice! I've added that to our code and it's working now. Thanks – yeaitsme Apr 26 '17 at 14:15
0

@lauda's answer points in the right direction, and the comments below mentioning the commit is particularly useful. However, I feel that the answer itself is half of the solution.

In the behat hooks we typically end up with a ScenarioInterface object, while the outline title is only available in a child class of that instance.

Therefore, the right code should look like so:

use Behat\Gherkin\Node\ExampleNode;
use Behat\Gherkin\Node\ScenarioInterface:

$scenario = $event->getScenario();

// For outlines, this is the data line title. Otherwise, it's the scenario title.
$title = $scenario->getTitle();

// In case of scenario outlines, this is the original scenario title.
// Otherwise it will be an empty string.
$outlineTitle = $scenario instanceof ExampleNode ? $scenario->getOutlineTitle() : '';
Christian
  • 27,509
  • 17
  • 111
  • 155