View source
<?php
use Behat\Behat\Context\ClosuredContextInterface, Behat\Behat\Context\TranslatedContextInterface, Behat\Behat\Context\BehatContext, Behat\Behat\Event\ScenarioEvent, Behat\Behat\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode, Behat\Gherkin\Node\TableNode;
use Drupal\DrupalExtension\Context\DrupalContext;
use Drupal\Component\Utility\Random;
use Behat\Behat\Event\FeatureEvent;
class FeatureContext extends DrupalContext {
public $subcontexts = array();
public function __construct() {
}
public function fixStepArgument($argument) {
static $random = array();
for ($start = 0; ($start = strpos($argument, '[', $start)) !== FALSE;) {
$end = strpos($argument, ']', $start);
if ($end === FALSE) {
break;
}
$random_generator = new Random();
$name = substr($argument, $start + 1, $end - $start - 1);
if ($name == 'random') {
$this->vars[$name] = $random_generator
->name(8);
$random[] = $this->vars[$name];
}
elseif (substr($name, 0, 7) == 'random:') {
$num = substr($name, 7);
if (is_numeric($num) && $num <= count($random)) {
$this->vars[$name] = $random[count($random) - $num];
}
}
if (isset($this->vars[$name])) {
$argument = substr_replace($argument, $this->vars[$name], $start, $end - $start + 1);
$start += strlen($this->vars[$name]);
}
else {
$start = $end + 1;
}
}
return $argument;
}
public function transformLocalConf($argument) {
preg_match('/\\[(.*)\\]/', $argument, $matches);
$token = $matches[1];
if (isset($this->localConf[$token])) {
return str_replace('[' . $token . ']', $this->localConf[$token], $argument);
}
}
public function switchToIFrame($name) {
if ($name) {
$this
->getMainContext()
->getSession()
->switchToIFrame($name);
}
else {
$this
->getMainContect()
->getSession()
->switchToIFrame();
}
}
public function iEnterTheIframe($arg1 = 'mediaBrowser') {
$this
->getSession()
->switchToIFrame($arg1);
}
public function iLeaveTheIframe() {
$this
->getSession()
->switchToIFrame();
$this
->getSession()
->wait(3000, 'false');
}
public function iChooseAnImage() {
$this
->getSession()
->getPage()
->find("css", "#media-browser-library-list li img")
->click();
$this
->getSession()
->wait(3000, 'false');
}
public function iChooseAnImageFromWYSIWYGMedia() {
$this
->getSession()
->getDriver()
->find("xpath", "//ul[@id='media-browser-library-list']/li/img")
->click();
}
public function iClickTheFakeButton($text) {
$driver = $this
->getSession()
->getDriver();
$buttons = $driver
->find("//a[text()='{$text}']");
$buttons[0]
->click();
}
public function iShouldSeeTheImage($file) {
$escapedValue = $this
->getSession()
->getSelectorsHandler()
->xpathLiteral($file);
$image = $this
->getSession()
->getPage()
->find('named', array(
'link',
$escapedValue,
));
if (!$image) {
throw new \Exception(sprintf('Image "%s" was not found on the page %s', $file, $this
->getSession()
->getCurrentUrl()));
}
}
public function iShouldSeeTheVideo($file) {
$video = $this
->getSession()
->getPage()
->find('xpath', '//iframe[title="{$file}]"');
if (!$video) {
throw new \Exception(sprintf('Image "%s" was not found on the page %s', $file, $this
->getSession()
->getCurrentUrl()));
}
}
public function iPause() {
$this
->getSession()
->wait(3000, 'false');
}
protected function getWysiwygInstance($instanceId) {
$instance = "Drupal.wysiwyg.instances['{$instanceId}']";
if (!$this
->getSession()
->evaluateScript("return !!{$instance}")) {
throw new \Exception(sprintf('The editor "%s" was not found on the page %s', $instanceId, $this
->getSession()
->getCurrentUrl()));
}
return $instance;
}
protected function getWysiwygToolbar($instanceId) {
$driver = $this
->getSession()
->getPage();
$wysiwyg = $driver
->find("xpath", "//div[@id='cke_{$instanceId}']");
if ($wysiwyg === NULL) {
throw new \Exception(sprintf('The wysiwyg "%s" was not found on the page', $instanceId));
}
$toolbarElement = $wysiwyg
->find("xpath", "//span[@class='cke_toolbar']");
if ($toolbarElement === NULL) {
throw new \Exception(sprintf('Toolbar for editor "%s" was not found on the page %s', $instanceId, $this
->getSession()
->getCurrentUrl()));
}
return $toolbarElement;
}
public function iTypeInTheWysiwygEditor($text, $instanceId) {
$instance = $this
->getWysiwygInstance($instanceId);
$this
->getSession()
->executeScript("{$instance}.insert(\"{$text}\");");
}
public function iFillInTheWysiwygEditor($instanceId, $text) {
$instance = $this
->getWysiwygInstance($instanceId);
$this
->getSession()
->executeScript("{$instance}.setContent(\"{$text}\");");
}
public function iClickTheButtonInTheWysiwygEditor($action, $instanceId) {
$driver = $this
->getSession()
->getDriver();
$instance = $this
->getWysiwygInstance($instanceId);
$toolbarElement = $this
->getWysiwygToolbar($instanceId);
$button = $toolbarElement
->find("xpath", "//span[@class='cke_toolgroup']/a[@title='{$action}']");
if (!$button) {
throw new \Exception(sprintf('Button "%s" was not found on the page %s', $action, $this
->getSession()
->getCurrentUrl()));
}
$button
->click();
$driver
->wait(1000, TRUE);
}
public function iExpandTheToolbarInTheWysiwygEditor($instanceId) {
$driver = $this
->getSession()
->getDriver();
$instance = $this
->getWysiwygInstance($instanceId);
$editorType = $this
->getSession()
->evaluateScript("return {$instance}.editor");
$toolbarElement = $this
->getWysiwygToolbar($instanceId, $editorType);
$action = 'Show/hide toolbars';
$button = $toolbarElement
->find("xpath", "//a[starts-with(@title, '{$action}')]");
if (!$button) {
throw new \Exception(sprintf('Button "%s" was not found on the page %s', $action, $this
->getSession()
->getCurrentUrl()));
}
if (strpos($button
->getAttribute('class'), 'mceButtonActive') !== FALSE) {
$button
->click();
}
}
public function assertContentInWysiwygEditor($text, $tag, $region) {
$instance = $this
->getWysiwygInstance($instanceId);
$content = $this
->evaluateScript("return {$instance}.getContent()");
if (strpos($text, $content) === FALSE) {
throw new \Exception(sprintf('The text "%s" was not found in the "%s" WYSWIYG editor on the page %s', $text, $instanceId, $this
->getSession()
->getCurrentUrl()));
}
}
public function assertContentNotInWysiwygEditor($text, $tag, $region) {
$instance = $this
->getWysiwygInstance($instanceId);
$content = $this
->evaluateScript("return {$instance}.getContent()");
if (strpos($text, $content) !== FALSE) {
throw new \Exception(sprintf('The text "%s" was found in the "%s" WYSWIYG editor on the page %s', $text, $instanceId, $this
->getSession()
->getCurrentUrl()));
}
}
public function deleteTestField() {
$this
->visit("admin/structure/types/manage/page/fields/field_test/delete");
$this
->pressButton("Delete");
}
}