alexandresalome / behat-launcher

Launch Behat tests from your browser
MIT License
63 stars 7 forks source link

Fixes for Behat 3.0 and some additional features #15

Open danyc opened 9 years ago

danyc commented 9 years ago

Hello,

Great work, I really appreciate it! I've been using the tool for the past 2 months and I'm very satisfied. I've added a couple of extra features / small fixes and I want to make them public, maybe there are others that might find them useful.

Changes:

Feature files can now be filtered based on content

My story: I've created a @smoke tag and used it only for some scenarios. There were files which contained no such tag, therefore on execution an error would have been thrown (that no scenario was found). Now the list of .feature files displays only the files containing @smoke tag. This is just an example, you can use any text as filter.

Example of usage:

$app->createProject('INT: [Smoke - Chrome]', '/var/www/bros')
    ->setFileFilter('@smoke');

Added new project properties (optional):

Example of usage:

$app->createProject('INT: [Smoke - Chrome]', '/var/www/bros')
    ->setBehatYmlFile('behat_Chrome.yml')
    // behat --profile int
    ->setProfile('int')
    ->setTags('@smoke')
    // filter .feature files that contain @smoke tag
    // (otherwise behat raises error that no scenario was found)
    ->setFileFilter('@smoke');

Insert screenshots into output window

My story: after each step, if an error occurs I capture a screenshot of the browser and I print a message with the path:

    /**
     * Capture screenshot after a step has failed (valid only for webdriver)
     *
     * @AfterStep
     * @param AfterStepScope $scope
     */
    public function afterStep(AfterStepScope $scope)
    {
        if (99 === $scope->getTestResult()->getResultCode())
        {
            $this->takeScreenshot();
        }
    }
   /**
     * Capture screenshot of the browser opened by Selenium
     */
    private function takeScreenshot()
    {
        $driver = $this->getSession()->getDriver();
        if ($driver instanceof Selenium2Driver)
        {   
                $fileName = date('Y-m-d_H-i-s') . '_' . uniqid() . '.png';
                $filePath = getcwd() . DIRECTORY_SEPARATOR . 'screenshots';

                // create folder if not already existing
                if (!file_exists($filePath))
                {
                    mkdir($filePath, 0777, true);
                }

                $this->saveScreenshot($fileName, $filePath);
                print('Screenshot at: ' . $filePath . DIRECTORY_SEPARATOR . $fileName);
        }
    }

Now the text displayed in the output window is parsed and if such messages are found, they are replaced with the actual images so they can be seen directly (tested on Windows and Mac OS X)

    public function showAction($id)
    {
        $path = $this->getRunStorage()->getOutputFilePath($id);
        $content = file_get_contents($path);

        if (false !== strpos($content, '<html')) {
            $template = 'outputFile_html.html.twig';
        } else {
            $converter = new AnsiToHtmlConverter();
            $content = $converter->convert($content);
            $template = 'outputFile_text.html.twig';

            // extract screenshots messages
            preg_match_all('#Screenshot at: (.*)#', $content, $screenshots, PREG_OFFSET_CAPTURE);
            foreach ($screenshots[0] as $screenshot)
            {
                // extract screenshot path
                $screenshotPath = str_replace("Screenshot at: ", "", $screenshot[0]);
                $image = $this->generateImage(trim($screenshotPath));

                // add <img> tag
                $content = str_replace("Screenshot at: " . $screenshotPath, '<a href="file:///' . $screenshotPath . '">' . $screenshotPath . '</a>'
                    . '<p><img src="data:image/png;base64,' . $image . ' "/>', $content);

                $image = null;
                $screenshotPath = null;
            }
        }

        return $this->render($template, array(
            'content' => $content
        ));
    }

output_screenshot

Fixes:

  1. Replaced behat command line parameter '--ansi' with '--colors' (for Behat 3.0)
  2. There was a change in Yaml::parse() method signature