Skip to content

Instantly share code, notes, and snippets.

@netraagal
Last active March 16, 2018 15:35
Show Gist options
  • Save netraagal/3eb5e0ce7d1f53c0d2884a2f7de3dec4 to your computer and use it in GitHub Desktop.
Save netraagal/3eb5e0ce7d1f53c0d2884a2f7de3dec4 to your computer and use it in GitHub Desktop.
Behat, MinkExtension & Selenium2 Driver - asserts on form elements
<?php
/**
* All of those functions can be used to verify the validity of what is on the page
*
* @author netraagal
*/
namespace App\Features;
use Behat\Mink\Driver\Selenium2Driver;
use Behat\Mink\Element\NodeElement;
use \Exception;
/**
* NOTE:
* the function GetElement used is on my other gist GetElementFeatureContext.php
*/
class AssertFeatureContext extends MinkContext implements SnippetAcceptingContext
{
/**
* assertInputContains
*
* @Then /^(?:|the )(?:element|input) "(?P<element>[^"]+)" should contains "(?P<value>[^"]+)"$/
*
* @param string $element
* @param string $value
* @return FeatureContext
*/
public function assertInputContains(string $element, string $value): self
{
$result = $this->getElement($element, "input");
if ($result->getValue() !== $value){
throw new Exception(
sprintf(
"Error : element '%s' of type input should contains '%s' but contains '%s'",
$element,
$value,
$result->getValue()
)
);
}
return $this;
}
/**
* assertIsChecked
*
* @Then /^(?:|the )"(?P<element>[^"]+)" should be checked$/
*
* @param string $element
* @return FeatureContext
* @throws Exception
*/
public function assertIsChecked(string $element): self
{
$result = $this->getElement($element, "input");
if ($result===null)
{
throw new Exception(
sprintf(
"Error : element '%s' of type radio button or checkbox not found",
$element
)
);
}
if (!$result->isChecked())
{
throw new Exception(
sprintf(
"Error : element '%s' should be checked",
$element
)
);
}
return $this;
}
/**
* assertIsNotChecked
*
* @Then /^(?:|the )"(?P<element>[^"]+)" should not be checked$/
*
* @param string $element
* @return FeatureContext
* @throws Exception
*/
public function assertIsNotChecked(string $element): self
{
$result = $this->getElement($element, "input");
if ($result===null)
{
throw new Exception(
sprintf(
"Error : element '%s' of type radio button or checkbox not found",
$element
)
);
}
if ($result->isChecked())
{
throw new Exception(
sprintf(
"Error : element '%s' should not be checked",
$element
)
);
}
return $this;
}
/**
* assertIsSelected
*
* @Then /^(?:|the |the option )"(?P<option>[^"]+)" should be selected in "(?P<select>[^"]+)"$/
*
* @param string $option
* @param string $select
* @return FeatureContext
* @throws Exception
*/
public function assertIsSelected($option, $select): self
{
if ($this->getElement($select, "select")->getValue() !== $this->getElement($option, "option")->getAttribute('value')) {
throw new Exception(
sprintf(
"Error : the option '%s' is not selected in the select '%s'.",
$option,
$select
)
);
}
return $this;
}
/**
* assertIsNotSelected
*
* @Then /^(?:|the |the option )"(?P<option>[^"]+)" should not be selected in "(?P<select>[^"]+)"$/
*
* @param string $option
* @param string $select
* @return FeatureContext
* @throws Exception
*/
public function assertIsNotSelected($option, $select): self
{
if ($this->getElement($select, "select")->getValue() === $this->getElement($option, "option")->getAttribute('value')) {
throw new Exception(
sprintf(
"Error : the option '%s' is selected in the select '%s'.",
$option,
$select
)
);
}
return $this;
}
/**
* assertElementWithClassExists
*
* @Then /^(?:|the )element(?:|s) with class(?:|es) "(?P<class>[^"]+)" (?:exist|exists|is visible|are visible)$/
*
* @param string $class
* @return FeatureContext
*/
public function assertElementWithClassExists(string $class): self
{
$classes = ".".implode(".", explode(", ", $class));
$element = $this->assertSession()->elementExists('css', $classes);
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment