BehatHtmlFormatterPlugin icon indicating copy to clipboard operation
BehatHtmlFormatterPlugin copied to clipboard

Get Scenario Title

Open jhenya opened this issue 9 years ago • 28 comments

Hi! I have question about getting scenario name in report

Step by step explenation my case

  1. I set scenario title in my FeatureContext

    public function setUpTestEnvironment($scope) { $this->currentScenarioTitle = $scope->getScenario()->getTitle; }

  2. When i getTitle durint test execution i get something like this: | some| test| parameter| parameter1|

  3. This string is from my Scenario Example table Scenario Outline: Open page anf validate something Given I am on page When New page is displayed Then I should see Examples: | header1| header2| header3| header4| | some| test| parameter| parameter1|

4.After test execution when i try get scenario name using getName in \vendor\emuse\behat-html-formatter\src\Classes\Scenario.php i get only Scenario Title, in my case it is "Open page anf validate something"
but i need title from Example table like this "| some| test| parameter| parameter1|"

Could you please explain how to get it?

jhenya avatar Oct 29 '15 14:10 jhenya

I think this problem is besause of onBeforeScenarioTested wasn't executed, but i can't understand why it wasn't executed

jhenya avatar Oct 29 '15 20:10 jhenya

Hi jhenya,

What exactly is the issue you're having?

Are you trying to get the scenario name for a screenshot?

jroy-998 avatar Oct 30 '15 10:10 jroy-998

Hi, yes i try to get it in afterStep($scope) in my feature context $scenarioName = $this->currentScenario->getTitle(); $fileName = str_replace('|', '_', str_replace(' ', '', $scenarioName)) . '.png';

And it works correct and get current scenario name (generate name from example table)

But in behat-html-formatter\src\Renderer\Behat2Renderer.php in line 346 $scenario->getScreenshotName() i get null

looks like onBeforeScenarioTested (behat-html-formatter\src\Formatter\BehatHTMLFormatter.php line 487) where screenshot name should be set wasn't executed

jhenya avatar Oct 30 '15 13:10 jhenya

HI, can you put up the render after step function you have in Behat2Renderer?

I'll have a quick look.

jroy-998 avatar Oct 30 '15 14:10 jroy-998

public function renderAfterStep($obj)
{
    $feature = $obj->getCurrentFeature();
    $scenario = $obj->getCurrentScenario();

    $steps = $scenario->getSteps();
    $step = end($steps); //needed because of strict standards

    //path displayed only if available (it's not available in undefined steps)
    $strPath = '';
    if($step->getDefinition() !== null) {
        $strPath = $step->getDefinition()->getPath();
    }

    $stepResultClass = '';
    if($step->isPassed()) {
        $stepResultClass = 'passed';
    }
    if($step->isFailed()) {
        $stepResultClass = 'failed';
    }
    if($step->isSkipped()) {
        $stepResultClass = 'skipped';
    }
    if($step->isPending()) {
        $stepResultClass = 'pending';
    }

    $print = '
                <li class="'.$stepResultClass.'">
                    <div class="step">
                        <span class="keyword">'.$step->getKeyWord().' </span>
                        <span class="text">'.$step->getText().' </span>
                        <span class="path">'.$strPath.'</span>
                    </div>';
    $exception = $step->getException();
    if(!empty($exception)) {
        $relativeScreenshotPath = 'assets/screenshots/' . $feature->getScreenshotFolder() . '/' . $scenario->getScreenshotName();
        $fullScreenshotPath = $obj->getBasePath() . '/build/html/behat/' . $relativeScreenshotPath;
        $print .= '
                    <pre class="backtrace">'.$step->getException().'</pre>';
        if(file_exists($fullScreenshotPath))
        {
            $print .= '<a href="' . $relativeScreenshotPath . '">Screenshot</a>';
        }
    }
    $print .= '
                </li>';

    return $print;
}

Here i just change folder results/html feature->getScreenshotFolder() works correct

jhenya avatar Oct 30 '15 14:10 jhenya

Hmm, I'm not sure what's going off here. What version of Behat are you using? I'm currently on dev-master and it's working for me. Maybe they've changed something in one of the versions?

jroy-998 avatar Oct 30 '15 14:10 jroy-998

now v3.0.15, i think problem is that onBeforeScenarioTested wasn't executed (behat-html-formatter\src\Formatter\BehatHTMLFormatter.php line 487) where screenshot name should be set

jhenya avatar Oct 30 '15 14:10 jhenya

Yes that does seem to be the issue. I can't see why that wouldn't be executed though? Can you paste the onBeforeScenarioTested function you have so I can take a look. APologies about all the code requests, just trying to figure out what's going on here? I'll test on 3.0.15 too.

jroy-998 avatar Oct 30 '15 14:10 jroy-998

no problem i just tried on dev-master and get same behavior /** * @param BeforeScenarioTested $event */ public function onBeforeScenarioTested(BeforeScenarioTested $event) { $scenario = new Scenario(); $scenario->setName($event->getScenario()->getTitle()); $scenario->setTags($event->getScenario()->getTags()); $scenario->setLine($event->getScenario()->getLine()); $scenario->setScreenshotName($event->getScenario()->getTitle()); $this->currentScenario = $scenario;

    $print = $this->renderer->renderBeforeScenario($this);
    $this->printer->writeln($print);
}

jhenya avatar Oct 30 '15 14:10 jhenya

we need in setScreenshotName set name of scenario (name of each example row in the scenario outline) not Scenario outline name

jhenya avatar Oct 30 '15 14:10 jhenya

Still working for me? As far as I'm aware, getTitle() will get the text that comes after the Scenario: declaration unless it's a scenario outline in which case it will get the text in the example.

How are you creating your scenarios? If I could get an example of one of your features I could give it a test?

jroy-998 avatar Oct 30 '15 14:10 jroy-998

Scenario Outline: Open page and validate something Given I am on page When New page is displayed Then I should see Examples: | header1| header2| header3 | header4 | | some | test | parameter | parameter1 | | some1 | test1 | parameter1| parameter2 |

for each row in example table scenario will be executed Name of each scenario here are: | some | test | parameter | parameter1 | | some | test | parameter | parameter1 |

jhenya avatar Oct 30 '15 14:10 jhenya

So, what you're saying is that the above is working and creating you a screenshot with the correct name, but you are getting null when Behat2Renderer attempts to get the scenario name?

If that is the case, I'm not sure why it wouldn't be setting the screenshot name. If you could paste me an example feature file you're using, I can run it on mine to see if it works?

jroy-998 avatar Oct 30 '15 14:10 jroy-998

you need .feature or context file? In your feature from Examples table you just set parameter to context methods using angle brackets

jhenya avatar Oct 30 '15 14:10 jhenya

Just a feature file, but I've found yours above, tried it and it worked fine?

jroy-998 avatar Oct 30 '15 15:10 jroy-998

did you get scenario name like this? | some | test | parameter | parameter1 |

jhenya avatar Oct 30 '15 15:10 jhenya

Just tried that too and it worked for me?

How are you seeing that getScenario() is calling a null value, are you printing it somewhere?

Is it null in FeatureContext() or Behat2Renderer?

jroy-998 avatar Oct 30 '15 15:10 jroy-998

i watch Behat2Renderer in debug

jhenya avatar Oct 30 '15 15:10 jhenya

So it is in Behat2Renderer then? Not really sure what I can offer here. This is consistently working for me and I'm never getting a null value. If I can't recreate, I'm not sure if there's anything I can do to fix it?

jroy-998 avatar Oct 30 '15 15:10 jroy-998

did you get scenario name like this? | some | test | parameter | parameter1 |

jhenya avatar Oct 30 '15 15:10 jhenya

It just uses the Behat framework getScenario()->getTitle() function, so whatever that returns, which is either the scenario description, or in the case of scenario outline, the example data.

jroy-998 avatar Oct 30 '15 15:10 jroy-998

Hmm, then try to find something wrong in my test and let you know later

jhenya avatar Oct 30 '15 15:10 jhenya

OK, well if you can find me some way to recreate it, let me know and I'd be happy to take a look.

jroy-998 avatar Oct 30 '15 15:10 jroy-998

in report we have only Scenario outline name and Scenario Example Name not available, is it possible that Scenario Example Name is not available during creating report ? example

but when i add in BehatHTMLFormatter.php (line 195 getSubscribedEvents) this code tester.example_tested.before' => 'onBeforeScenarioTested', then onBeforeScenarioTested was executed before Scenario Example and I can get Scenario Example Name in report but only last Scenario Example

May be we need to add Example class here behat-html-formatter\src\Classes and implement listen event tester.example_tested.before

jroy-998, could you please try to investigate it with scenario with 2 or more Scenario Examples

jhenya avatar Nov 02 '15 09:11 jhenya

Hi,

I can still get scenario example name. I did notice an issue to do with long example names, but that is separate to this I think.

I am still getting the scenario example name, so am not sure what is going wrong here? I notice your report is formatted differently to mine (see screenshot).

Are you using a different/custom version?

screenshot from 2015-10-28 12 21 08

jroy-998 avatar Nov 04 '15 09:11 jroy-998

Hi I try using same format (renderer: Behat2) and i get Scenario Example name but only after adding in BehatHTMLFormatter.php (line 195 getSubscribedEvents) this code tester.example_tested.before' => 'onBeforeScenarioTested',

example2

this solution Ok for me but each next Scenario Outline is nested in previous

jhenya avatar Nov 04 '15 10:11 jhenya

I may have to go back to the drawing board a little bit here.

If an example contains a lot of data, the image file will not be created (I suspect that this may have something to do with file name length restrictions).

I may have to think of some other way of doing it.

I still cannot recreate the issue that you are having. But this may be superseded by doing it a different way.

As for the nesting issue. This is a seperate issue that I raised here:

https://github.com/dutchiexl/BehatHtmlFormatterPlugin/issues/47

jroy-998 avatar Nov 05 '15 10:11 jroy-998

Hi guys, are there any conclusions about the initial issue? I'm writing here from the future (2016) and the case with the empty screenshot name in Behat2Renderer is still reproducing for me. Just like described above, and only for the cases with the scenario outline

dmytro-grablov avatar Nov 04 '16 17:11 dmytro-grablov