SocialMinkContext.php in Open Social 8.6
File
tests/behat/features/bootstrap/SocialMinkContext.php
View source
<?php
namespace Drupal\social\Behat;
use Drupal\DrupalExtension\Context\MinkContext;
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Drupal\DrupalExtension\Context\DrupalContext;
use Behat\MinkExtension\Context\RawMinkContext;
use PHPUnit_Framework_Assert as PHPUnit;
use Drupal\DrupalExtension\Hook\Scope\EntityScope;
use Behat\Mink\Driver\Selenium2Driver;
use Behat\Behat\Hook\Scope\AfterStepScope;
class SocialMinkContext extends MinkContext {
public function assertRegionHeading($heading, $region) {
$regionObj = $this
->getRegion($region);
foreach (array(
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
) as $tag) {
$elements = $regionObj
->findAll('css', $tag);
if (!empty($elements)) {
foreach ($elements as $element) {
if (trim(strtolower($element
->getText())) === strtolower($heading)) {
return;
}
}
}
}
throw new \Exception(sprintf('The heading "%s" was not found in the "%s" region on the page %s', $heading, $region, $this
->getSession()
->getCurrentUrl()));
}
public function assertCheckBox($checkbox) {
$this
->getSession()
->executeScript("\n var inputs = document.getElementsByTagName('input');\n for (var i = 0; i < inputs.length; i++) {\n inputs[i].style.opacity = 1;\n inputs[i].style.left = 0;\n inputs[i].style.position = 'relative';\n }\n ");
parent::assertCheckBox($checkbox);
}
public function iMakeAScreenshot() {
$this
->iMakeAScreenshotWithFileName('screenshot');
}
public function iMakeAScreenshotWithFileName($filename) {
$screenshot = $this
->getSession()
->getDriver()
->getScreenshot();
$dir = '/var/www/travis_artifacts';
if (is_writeable($dir)) {
$file_and_path = $dir . '/' . $filename . '.jpg';
file_put_contents($file_and_path, $screenshot);
}
}
public function takeScreenShotAfterFailedStep(AfterStepScope $scope) {
if (99 === $scope
->getTestResult()
->getResultCode()) {
$driver = $this
->getSession()
->getDriver();
if (!$driver instanceof Selenium2Driver) {
return;
}
$feature = $scope
->getFeature();
$title = $feature
->getTitle();
$filename = date("Ymd-H_i_s");
if (!empty($title)) {
$filename .= '-' . str_replace(' ', '-', strtolower($title));
}
$filename .= '-error';
$this
->iMakeAScreenshotWithFileName($filename);
}
}
public function attachFileToHiddenField($field, $path) {
$field = $this
->fixStepArgument($field);
$javascript = "jQuery('#" . $field . "').parent().removeClass('hidden')";
$this
->getSession()
->executeScript($javascript);
$this
->attachFileToField($field, $path);
}
public function iShouldSeeCheckedTheBox($checkbox) {
$checkbox = $this
->fixStepArgument($checkbox);
if (!$this
->getSession()
->getPage()
->hasCheckedField($checkbox)) {
$field = $this
->getSession()
->getPage()
->findField($checkbox);
if (null === $field) {
throw new \Exception(sprintf('The checkbox "%s" with id|name|label|value was not found', $checkbox));
}
else {
throw new \Exception(sprintf('The checkbox "%s" is not checked', $checkbox));
}
}
}
public function iShouldSeeUncheckedTheBox($checkbox) {
$checkbox = $this
->fixStepArgument($checkbox);
if (!$this
->getSession()
->getPage()
->hasUncheckedField($checkbox)) {
$field = $this
->getSession()
->getPage()
->findField($checkbox);
if (null === $field) {
throw new \Exception(sprintf('The checkbox "%s" with id|name|label|value was not found', $checkbox));
}
else {
throw new \Exception(sprintf('The checkbox "%s" is checked', $checkbox));
}
}
}
public function iWaitForAjaxToFinish() {
$condition = <<<JS
(function() {
function isAjaxing(instance) {
return instance && instance.ajaxing === true;
}
var d7_not_ajaxing = true;
if (typeof Drupal !== 'undefined' && typeof Drupal.ajax !== 'undefined' && typeof Drupal.ajax.instances === 'undefined') {
for(var i in Drupal.ajax) { if (isAjaxing(Drupal.ajax[i])) { d7_not_ajaxing = false; } }
}
var d8_not_ajaxing = (typeof Drupal === 'undefined' || typeof Drupal.ajax === 'undefined' || typeof Drupal.ajax.instances === 'undefined' || !Drupal.ajax.instances.some(isAjaxing))
return (
// Assert no AJAX request is running (via jQuery or Drupal) and no
// animation is running.
(typeof jQuery === 'undefined' || (jQuery.active === 0 && jQuery(':animated').length === 0)) &&
d7_not_ajaxing && d8_not_ajaxing
);
}());
JS;
$result = $this
->getSession()
->wait(20000, $condition);
if (!$result) {
throw new \RuntimeException('Unable to complete AJAX request.');
}
}
public function iSetAlias($value) {
if (\Drupal::service('module_handler')
->moduleExists('social_path_manager')) {
$option = $this
->fixStepArgument('Generate automatic URL alias');
$this
->getSession()
->getPage()
->uncheckField($option);
}
$field = $this
->fixStepArgument('URL alias');
$value = $this
->fixStepArgument($value);
$this
->getSession()
->getPage()
->fillField($field, $value);
}
}