Commit 03910465 authored by Moshe Weitzman's avatar Moshe Weitzman
Browse files

Add capturePageContent(); Avoid drupal_basename()

parent b2c31b44
Pipeline #161802610 passed with stage
in 5 minutes and 44 seconds
......@@ -45,8 +45,8 @@ vendor/bin/phpunit --group CustomTestGroup
## Debugging tests
- All HTML requests can be logged. To do so, add `BROWSERTEST_OUTPUT_DIRECTORY=/tmp` and `--printer '\\Drupal\\Tests\\Listeners\\HtmlOutputPrinter'` to the `phpunit` call. To disable deprecation notices, include `SYMFONY_DEPRECATIONS_HELPER=disabled`. Alternatively, you can specify these in your `phpunit.xml` ([example phpunit.xml](docs/phpunit.xml)).
- To check the current HTML of the page, use `file_put_contents('public://' . drupal_basename($this->getSession()->getCurrentUrl()) . '.html', $this->getCurrentPageContent());`.
- To take a screenshot of the current page under ExistingSiteSelenium2DriverTest or ExistingSiteWebDriverTest, use `file_put_contents('public://screenshot.jpg', $this->getSession()->getScreenshot());`.
- To write the current HTML of the page to a file in the browser_output directory, use `$this->capturePageContent()`.
- To take a screenshot of the current page under ExistingSiteSelenium2DriverTest or ExistingSiteWebDriverTest, use `\weitzman\DrupalTestTraits\ScreenShotTrait::captureScreenshot`.
## Available traits
......
......@@ -2,6 +2,7 @@
namespace weitzman\DrupalTestTraits;
use Drupal\Component\Utility\Html;
use Drupal\Core\DrupalKernel;
use Drupal\Core\Entity\EntityInterface;
use DrupalFinder\DrupalFinder;
......@@ -126,4 +127,42 @@ trait DrupalTrait
{
$this->cleanupEntities[] = $entity;
}
/**
* Get filename for writing browser output files for debugging.
*
* @param string $directory
*
* @return string
* An absolute file path, without an extension.
*/
protected function getOutputFileName($directory = '')
{
if (empty($directory)) {
$directory = $this->htmlOutputDirectory;
}
// Ensure directory exists.
if (!is_dir($directory)) {
mkdir($directory, 0777, true);
}
$current_url = Html::cleanCssIdentifier($this->getSession()->getCurrentUrl());
return $directory . DIRECTORY_SEPARATOR . uniqid() . '_' . $current_url;
}
/**
* Captures and saves page content.
*
* The filename will contain a unique ID, the URL where
* the screenshot was taken and the given base filename.
*
* @param string $base_filename (optional)
* The base filename to use, defaults to 'page'.
*/
protected function capturePageContent($base_filename = 'page')
{
$base_filename = Html::cleanCssIdentifier($base_filename);
$filename = $this->getOutputFileName() . '_' . $base_filename . '.html';
$contents = $this->getCurrentPageContent();
file_put_contents($filename, $contents);
}
}
......@@ -27,17 +27,9 @@ trait ScreenShotTrait
*/
protected function captureScreenshot($base_filename = 'screenshot')
{
$directory = getenv('DTT_SCREENSHOT_REPORT_DIRECTORY') ?: '/sites/simpletest/browser_output';
if ($directory) {
// Ensure directory exists.
if (!is_dir($directory)) {
mkdir($directory, 0777, true);
}
$current_url = Html::cleanCssIdentifier($this->getSession()->getCurrentUrl());
$base_filename = Html::cleanCssIdentifier($base_filename);
$filename = $directory . DIRECTORY_SEPARATOR . (uniqid() . '_' . $current_url . '_' . $base_filename . '.png');
$screenshot = $this->getDriverInstance()->getScreenshot();
file_put_contents($filename, $screenshot);
}
$base_filename = Html::cleanCssIdentifier($base_filename);
$filename = $this->getOutputFileName(getenv('DTT_SCREENSHOT_REPORT_DIRECTORY')) . '_' . $base_filename . '.png';
$screenshot = $this->getDriverInstance()->getScreenshot();
file_put_contents($filename, $screenshot);
}
}
......@@ -26,8 +26,8 @@ class ExampleSelenium2DriverTest extends ExistingSiteSelenium2DriverTestBase
// @codingStandardsIgnoreStart
// These lines are left here as examples of how to debug requests.
// file_put_contents('public://screenshot.jpg', $this->getSession()->getScreenshot());
// file_put_contents('public://' . drupal_basename($this->getSession()->getCurrentUrl()) . '.html', $this->getCurrentPageContent());
// \weitzman\DrupalTestTraits\ScreenShotTrait::captureScreenshot();
// $this->capturePageContent();
// @codingStandardsIgnoreStop
// Test autocomplete on article creation.
......
......@@ -30,8 +30,8 @@ class ExampleWebDriverTest extends ExistingSiteWebDriverTestBase
$web_assert->statusCodeEquals(200);
// These lines are left here as examples of how to debug requests.
// file_put_contents('public://screenshot.png', $session->getScreenshot());
// file_put_contents('public://' . drupal_basename($session->getCurrentUrl()) . '.html', $this->getCurrentPageContent());
// \weitzman\DrupalTestTraits\ScreenShotTrait::captureScreenshot();
$this->capturePageContent();
$page = $this->getCurrentPage();
$page->fillField('name', 'admin');
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment