Created
May 6, 2014 17:14
-
-
Save EvanAgee/a6d4ceb40d866f7aa819 to your computer and use it in GitHub Desktop.
FeatureContext.php for Drupal sites
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Behat\Behat\Context\ClosuredContextInterface, | |
Behat\Behat\Context\TranslatedContextInterface, | |
Behat\Behat\Context\BehatContext, | |
Behat\Behat\Exception\PendingException; | |
use Behat\Gherkin\Node\PyStringNode, | |
Behat\Gherkin\Node\TableNode; | |
use Behat\MinkExtension\Context\MinkContext; | |
// | |
// Require 3rd-party libraries here: | |
// | |
// require_once 'PHPUnit/Autoload.php'; | |
// require_once 'PHPUnit/Framework/Assert/Functions.php'; | |
// | |
/** | |
* Features context. | |
*/ | |
// class FeatureContext extends BehatContext | |
class FeatureContext extends Drupal\DrupalExtension\Context\DrupalContext | |
{ | |
/** | |
* Initializes context. | |
* Every scenario gets it's own context object. | |
* | |
* @param array $parameters context parameters (set them up through behat.yml) | |
*/ | |
public function __construct(array $parameters) | |
{ | |
// Initialize your context here | |
} | |
/** | |
* @Then /^"([^"]*)" should be visible$/ | |
*/ | |
public function shouldBeVisible($selector) { | |
$el = $this->getSession()->getPage()->find('css', $selector); | |
$style = ''; | |
if(!empty($el)){ | |
$style = preg_replace('/\s/', '', $el->getAttribute('style')); | |
} else { | |
throw new Exception("Element ({$selector}) not found"); | |
} | |
return true; | |
} | |
/** | |
* @Then /^the element "([^"]*)" should exist$/ | |
*/ | |
public function theElementShouldExist($arg1) | |
{ | |
$session = $this->getSession(); // get the mink session | |
$element = $session->getPage()->find('css', $arg1); // runs the actual query and returns the element | |
// errors must not pass silently | |
if (null === $element) { | |
throw new \InvalidArgumentException(sprintf('Could not find element: "%s"', $arg1)); | |
} | |
return true; | |
} | |
/** | |
* @When /^I hover over the element "([^"]*)"$/ | |
*/ | |
public function iHoverOverTheElement($locator) | |
{ | |
$session = $this->getSession(); // get the mink session | |
$element = $session->getPage()->find('css', $locator); // runs the actual query and returns the element | |
// errors must not pass silently | |
if (null === $element) { | |
throw new \InvalidArgumentException(sprintf('Could not evaluate CSS selector: "%s"', $locator)); | |
} | |
// ok, let's hover it | |
$element->mouseOver(); | |
} | |
/** | |
* @Then /^the url should match "(?P<pattern>[^"]+)"$/ | |
*/ | |
public function assertUrlRegExp($pattern) | |
{ | |
if (!preg_match('/^\/.*\/$/', $pattern)) { | |
return new Then("I should be on \"$pattern\""); | |
} | |
return true; | |
} | |
/** | |
* @Given /^I am on the Content Creation page$/ | |
*/ | |
public function iAmOnTheContentCreationPage() { | |
$session = $this->getSession(); | |
return $session->getDriver()->visit("node/add"); | |
} | |
/** | |
* @Given /^the element "([^"]*)" should have a background image$/ | |
*/ | |
public function theElementShouldHaveABackgroundImage($arg1) { | |
$session = $this->getSession(); | |
$page = $session->getPage(); | |
$element = $page->find('css', $arg1); | |
$style = $element->getAttribute('style'); | |
if (null === $element) { | |
throw new \LogicException(sprintf('Could not find the element "%s"',$arg1)); | |
} | |
if (strpos($style,'background-image') !== false) { | |
return true; | |
} else { | |
throw new \LogicException(sprintf('Element "%s" did not have a background image', $arg1)); | |
} | |
} | |
/** | |
* Take screenshot when step fails. Works only with Selenium2Driver. | |
* | |
* If on a Mac, runnining from the command line, with the SHOW_SNAPSHOT | |
* environment variable set to 1, the snapshot will open in Preview. | |
* | |
* Screenshot is saved at [Date]/[Feature]/[Scenario]/[Step].jpg . | |
* | |
* @AfterStep @javascript | |
*/ | |
public function takeScreenshotAfterFailedStep(Behat\Behat\Event\StepEvent $event) { | |
if ($event->getResult() === Behat\Behat\Event\StepEvent::FAILED) { | |
$driver = $this->getSession()->getDriver(); | |
if ($driver instanceof Behat\Mink\Driver\Selenium2Driver) { | |
$step = $event->getStep(); | |
$path = array( | |
'date' => date("Ymd-Hi"), | |
'feature' => $step->getParent()->getFeature()->getTitle(), | |
'scenario' => $step->getParent()->getTitle(), | |
'step' => $step->getType() . ' ' . $step->getText(), | |
); | |
$path = preg_replace('/[^\-\.\w]/', '_', $path); | |
$filename = 'screenshots/' . implode('/', $path) . '.jpg'; | |
// Create directories if needed. | |
if (!@is_dir(dirname($filename))) { | |
@mkdir(dirname($filename), 0775, TRUE); | |
} | |
file_put_contents($filename, $driver->getScreenshot()); | |
if (PHP_OS === "Darwin" && (bool) getenv('SHOW_SNAPSHOT') === TRUE && PHP_SAPI === "cli") { | |
exec('open -a "Preview.app" ' . $filename); | |
} | |
} | |
} | |
} | |
/** | |
* @When /^wait (\d+) seconds?$/ | |
*/ | |
public function waitSeconds($seconds) | |
{ | |
$this->getSession()->wait(1000*$seconds); | |
} | |
/** | |
* Fills in WYSIWYG editor with specified id. | |
* | |
* @Given /^(?:|I )fill in "(?P<text>[^"]*)" in WYSIWYG editor "(?P<iframe>[^"]*)"$/ | |
*/ | |
public function iFillInInWYSIWYGEditor($text, $iframe) | |
{ | |
try { | |
$this->getSession()->switchToIFrame($iframe); | |
} | |
catch (Exception $e) { | |
throw new \Exception(sprintf("No iframe with id '%s' found on the page '%s'.", $iframe, $this->getSession()->getCurrentUrl())); | |
} | |
$this->getSession()->executeScript("document.body.innerHTML = '<p>".$text."</p>'"); | |
$this->getSession()->switchToIFrame(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment