dutchiexl / BehatHtmlFormatterPlugin

This is a behat plugin to generate HTML reports
MIT License
112 stars 117 forks source link

Get Scenario Title #48

Open jhenya opened 9 years ago

jhenya commented 9 years ago

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 commented 9 years ago

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

jroy-998 commented 9 years ago

Hi jhenya,

What exactly is the issue you're having?

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

jhenya commented 9 years ago

Hi, yes i try to get it in afterStep($scope) in my feature context $scenarioName = $this->currentScenario->getTitle(); $fileName = strreplace('|', '', 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

jroy-998 commented 9 years ago

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

I'll have a quick look.

jhenya commented 9 years ago
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

jroy-998 commented 9 years ago

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?

jhenya commented 9 years ago

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

jroy-998 commented 9 years ago

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.

jhenya commented 9 years ago

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 commented 9 years ago

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

jroy-998 commented 9 years ago

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?

jhenya commented 9 years ago

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 |

jroy-998 commented 9 years ago

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?

jhenya commented 9 years ago

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

jroy-998 commented 9 years ago

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

jhenya commented 9 years ago

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

jroy-998 commented 9 years ago

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?

jhenya commented 9 years ago

i watch Behat2Renderer in debug

jroy-998 commented 9 years ago

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?

jhenya commented 9 years ago

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

jroy-998 commented 9 years ago

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.

jhenya commented 9 years ago

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

jroy-998 commented 9 years ago

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

jhenya commented 9 years ago

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

jroy-998 commented 9 years ago

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

jhenya commented 9 years ago

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

jroy-998 commented 9 years ago

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

dmytro-grablov commented 8 years ago

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