Skip to content

Instantly share code, notes, and snippets.

@aczietlow
Created December 8, 2014 15:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aczietlow/1d2c8f7694e1471f2874 to your computer and use it in GitHub Desktop.
Save aczietlow/1d2c8f7694e1471f2874 to your computer and use it in GitHub Desktop.
Behat tests for checkout css properties
<?php
/**
* @Given /^The element "(?P<selector>[^"]*)" should have a css property "(?P<property>[^"]*)" with a value of "(?P<value>[^"]*)"$/
*
* @TODO Need to find a way to test for css styles on elements.
* Or possibly we should just be checking the markup, and not the styling... Research this more.
*/
public function assertElementHasCssValue($selector, $property, $value)
{
$page = $this->getSession()->getPage();
$element = $page->find('css', $selector);
if (empty($element)) {
$message = sprintf('Could not find element using the selector "%s"', $selector);
throw new \Exception($message);
}
$style = $this->elementHasCSSValue($element, $property, $value);
if (empty($style)) {
$message = sprintf('The property "%s" for the selector "%s" is not "%s"', $property, $selector, $value);
throw new \Exception($message);
}
}
/**
* Determine if a Mink NodeElement contains a specific css rule attribute value.
*
* @param NodeElement $element
* NodeElement previously selected with $this->getSession()->getPage()->find().
* @param string $property
* Name of the CSS property, such as "visibility".
* @param string $value
* Value of the specified rule, such as "hidden".
*
* @return NodeElement|bool
* The NodeElement selected if true, FALSE otherwise.
*/
protected function elementHasCSSValue($element, $property, $value)
{
$exists = FALSE;
$style = $element->getAttribute('style');
if ($style) {
if (preg_match("/(^{$property}:|; {$property}:) ([a-z0-9]+);/i", $style, $matches)) {
$found = array_pop($matches);
if ($found == $value) {
$exists = $element;
}
}
}
return $exists;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment