Skip to content

Instantly share code, notes, and snippets.

@netraagal
Last active March 16, 2018 15:34
Show Gist options
  • Save netraagal/2baf60685b06782d85da9c67c7f5aa98 to your computer and use it in GitHub Desktop.
Save netraagal/2baf60685b06782d85da9c67c7f5aa98 to your computer and use it in GitHub Desktop.
Behat, MinkExtension & Selenium2 Driver - select a NodeElement
<?php
/**
* those functions allos me to use only 1 Step definitio to get an element, even if I don't use the same
* parameters to get it.
* You can search with:
* - the xpath (Which can be really tricky sometimes)
* - an attribute of the element : id (the best) or name or value
* - the css (the more used)
* I pass the type of element and the element above but you can only use css and xpath (as the attributes
* can also be used on the css)
*
* @author netraagal
*/
namespace App\Features;
use Behat\Mink\Driver\Selenium2Driver;
use Behat\Mink\Element\NodeElement;
use Behat\Mink\Exception\ElementNotFoundException;
use \Exception;
/**
* select an element based on it's type an any value which can identify it:
* - xpath | css | css attribute (name|id|value)
*/
class GetElementFeatureContext extends MinkContext implements SnippetAcceptingContext
{
/**
* iSelect
*
* @When /^(?:|I )select "(?P<element>[^"]+)" (?:|of type )"(?P<type>[^"]+)"$/
* @When /^(?:|I )select "(?P<type>[^"]+)" "(?P<element>[^"]+)"$/
*
* @param string $element
* @param string $type
* @return NodeElement
*/
public function iSelect($element, $type): NodeElement
{
return $this->getElement($element, $type);
}
/**
* getElement
*
* @param string $element
* @param string $type
* @return NodeElement
* @throws Exception
*/
protected function getElement($element, $type): NodeElement
{
$value = null;
if (substr($element, 0, 2) === "//") //xpath
{
try {
$value = $this->assertSession()->elementExists('xpath', $element);
}
catch (ElementNotFoundException $e) {
$value = null;
}
}
else { //simple string
$value = $this->getElementByCSS($element, $type);
}
if ($value === null)
{
throw new Exception(
sprintf(
"Error : research on NodeElement with '%s' and type '%s' has returned null",
$element,
$type
)
);
}
return $value;
}
/**
* getElementByCSS
*
* @param string $value
* @param string $type
* @return NodeElement|null
*/
protected function getElementByCSS($value, $type): ?NodeElement
{
try {
$element = $this->assertSession()->elementExists('css', $value);
}
catch(ElementNotFoundException $e) {
$element = $this->getElementByCSSAttribute($value, $type);
}
return $element;
}
/**
* getElementByCSSAttribute
*
* @param string $selector
* @param string $value
* @param string $type
* @return NodeElement|null
*/
protected function getElementByCSSAttribute($value, $type, string $selector = "id"): ?NodeElement
{
try {
$element = $this->assertSession()->elementExists('css', $type."[".$selector."='".$value."']");
}
catch(ElementNotFoundException $e) {
if ($selector === "id")
$element = $this->getElementByCSSAttribute($value, $type, "name");
else if ($selector === "name")
$element = $this->getElementByCSSAttribute($value, $type, "value");
else
$element = null;
}
return $element;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment