dutchiexl / BehatHtmlFormatterPlugin

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

BehatHtmlFormatterPlugin

Behat 3 extension for generating HTML reports from your test results.

Latest Stable Version Total Downloads Latest Unstable Version License

Twig report

Twig Screenshot

Behat 2 report

Behat2 Screenshot

How?

Installation

Prerequisites

This extension requires:

Through composer

The easiest way to keep your suite updated is to use Composer:

Install with composer:

$ composer require --dev emuse/behat-html-formatter

Install using composer.json

Add BehatHtmlFormatterPlugin to the list of dependencies inside your composer.json.

{
    "require": {
        "behat/behat": "3.*@stable",
        "emuse/behat-html-formatter": "0.1.*",
    },
    "minimum-stability": "dev",
    "config": {
        "bin-dir": "bin/"
    }
}

Then simply install it with composer:

$ composer install --dev --prefer-dist

You can read more about Composer on its official webpage.

Basic usage

Activate the extension by specifying its class in your behat.yml:

# behat.yml
default:
  suites:
    default:
       contexts:
          - emuse\BehatHTMLFormatter\Context\ScreenshotContext:
               screenshotDir: build/html/behat/assets/screenshots
    ... # All your awesome suites come here
  formatters:
    html:
      output_path: %paths.base%/build/html/behat

  extensions:
    emuse\BehatHTMLFormatter\BehatHTMLFormatterExtension:
      name: html
      renderer: Twig,Behat2
      file_name: index
      print_args: true
      print_outp: true
      loop_break: true

Command line options

Add the following to your behat command to print a report:

behat --format html --out MYDIRECTORY

Setting the format to html will output the various reports that you configure below (Behat2, Twig, Minimal, etc.)

You also need to specify the output directory for the reports as MYDIRECTORY.

Configuration

Formatter configuration

Extension configuration

Screenshot

The facility exists to embed a screenshot into test failures.

Currently png is the only supported image format.

In order to embed a screenshot, you will need to take a screenshot using your favourite webdriver and store it in the following filepath format:

results/html/assets/screenshots/{{feature_name}}/{{scenario_name}}.png

The feature_name and scenario_name variables will need to be the relevant item names without spaces.

Below is an example of FeatureContext methods which will produce an image file in the above format:


        /**
         * @BeforeScenario
         *
         * @param BeforeScenarioScope $scope
         *
         */
        public function setUpTestEnvironment($scope)
        {
            $this->currentScenario = $scope->getScenario();
        }

        /**
         * @AfterStep
         *
         * @param AfterStepScope $scope
         */
        public function afterStep($scope)
        {
            //if test has failed, and is not an api test, get screenshot
            if(!$scope->getTestResult()->isPassed())
            {
                //create filename string

               $featureFolder = preg_replace('/\W/', '', $scope->getFeature()->getTitle());

                              $scenarioName = $this->currentScenario->getTitle();
                              $fileName = preg_replace('/\W/', '', $scenarioName) . '.png';

                //create screenshots directory if it doesn't exist
                if (!file_exists('results/html/assets/screenshots/' . $featureFolder)) {
                    mkdir('results/html/assets/screenshots/' . $featureFolder);
                }

                //take screenshot and save as the previously defined filename
                $this->driver->takeScreenshot('results/html/assets/screenshots/' . $featureFolder . '/' . $fileName);
                // For Selenium2 Driver you can use:
                // file_put_contents('results/html/assets/screenshots/' . $featureFolder . '/' . $fileName, $this->getSession()->getDriver()->getScreenshot());
            }
        }

Note that the currentScenario variable will need to be at class level and generated in the @BeforeScenario method as Behat does not currently support obtaining the current Scenario in the @AfterStep method, where the screenshot is generated

Issue Submission

When you need additional support or you discover something strange, feel free to Create a new issue.

License and Authors

Authors: https://github.com/dutchiexl/BehatHtmlFormatterPlugin/contributors