Skip to content

Commit

Permalink
Updated for behat 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bmehdi committed Dec 8, 2014
1 parent a187d0d commit 363341e
Showing 1 changed file with 39 additions and 48 deletions.
87 changes: 39 additions & 48 deletions src/Pfd/Behat/Context/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

namespace Pfd\Behat\Context;

use Behat\Behat\Context\BehatContext;
use Behat\MinkExtension\Context\MinkContext;
use Behat\Mink\Session;
use Behat\Mink\Exception\ElementNotFoundException;
use WebDriver\Key;
use Behat\Mink\Element\NodeElement;
Expand All @@ -29,11 +27,14 @@
/**
* Useful behat feature contexts and methods
*/
class FeatureContext extends BehatContext
class FeatureContext extends MinkContext
{
public function __construct()
/**
* @Given /^que je suis connecté avec le pseudo "([^"]*)" et le mot de passe "([^"]*)"$/
*/
public function queJeSuisConnecteEnTantQue($login, $password)
{
$this->useContext('mink', new MinkContext());
$this->login('Utilisateur', $login, 'Mot de passe', $password, 'Valider');
}

/**
Expand All @@ -48,55 +49,53 @@ public function __construct()
*
* @return array
*/
public function login($loginLabel, $login, $passwordLabel, $password, $submitLabel, $page = '/login')
protected function login($loginLabel, $login, $passwordLabel, $password, $submitLabel, $page = '/login')
{
$minkContext = $this->getSubcontext('mink');

$minkContext->visit($page);
$minkContext->fillField($loginLabel, $login);
$minkContext->fillField($passwordLabel, $password);
$minkContext->pressButton($submitLabel);
$this->visit($page);
$this->fillField($loginLabel, $login);
$this->fillField($passwordLabel, $password);
$this->pressButton($submitLabel);
}

/**
* @Given /^(?:|que )j'attends quelques secondes$/
*/
public function jAttendsQuelquesSecondes()
{
$this->getMinkSession()->wait(30000, '(0 === jQuery.active)');
$this->getSession()->wait(30000, '(0 === jQuery.active)');
}

/**
* @Given /^(?:|que )j'attends (?P<num>\d+) secondes?$/
*/
public function jAttendsSecondes($num)
{
$this->getMinkSession()->wait($num * 1000);
$this->getSession()->wait($num * 1000);
}

/**
* @Given /^(?:|que )je sélectionne strictement "([^"]*)" depuis "([^"]*)"$/
*/
public function jeSelectionneStrictementDepuis($arg1, $arg2)
{
$this->getMinkSession()->getDriver()->wait(2000, false);
$this->getSession()->getDriver()->wait(2000, false);
$element = $this->getVisibleField($arg2);

if (false === $element) {
throw new ElementNotFoundException(
$this->getMinkSession()
$this->getSession()
);
}

$option = $element->find('xpath', sprintf('//option[. = %s]', '"' . $arg1 . '"'));
$option = $element->find('xpath', sprintf('//option[. = %s]', '"'.$arg1.'"'));

if ( !is_object($option)) {
if (!is_object($option)) {
throw new ElementNotFoundException(
$this->getMinkSession()
$this->getSession()
);
}

$this->getMinkSession()->getDriver()->selectOption($element->getXpath(), $option->getAttribute('value'));
$this->getSession()->getDriver()->selectOption($element->getXpath(), $option->getAttribute('value'));
}

/**
Expand All @@ -120,7 +119,7 @@ public function jeSelectionneDepuisLElement($option, $cssSelector)
*/
public function behatRefuseLesPopups()
{
$this->getMinkSession()->executeScript('
$this->getSession()->executeScript('
window.alert = window.confirm = function (msg) {
document.getElementById("__alert_container__").innerHTML = msg;
Expand All @@ -137,7 +136,7 @@ public function behatRefuseLesPopups()
*/
public function behatAccepteLesPopups()
{
$this->getMinkSession()->executeScript('
$this->getSession()->executeScript('
window.alert = window.confirm = function (msg) {
document.getElementById("__alert_container__").innerHTML = msg;
Expand All @@ -158,7 +157,7 @@ public function behatAccepteLesPopups()
*/
public function jeDevraisVoirDansLaPopup($message)
{
$element = $this->getMinkSession()->getPage()->findById('__alert_container__');
$element = $this->getSession()->getPage()->findById('__alert_container__');

return $message == $element->getHtml();
}
Expand All @@ -168,7 +167,7 @@ public function jeDevraisVoirDansLaPopup($message)
*/
public function jeSurvole($selector)
{
$element = $this->getMinkSession()->getPage()->find('css', $selector);
$element = $this->getSession()->getPage()->find('css', $selector);

if (null === $element) {
throw new \InvalidArgumentException(sprintf('Could not evaluate CSS selector: "%s"', $selector));
Expand All @@ -193,28 +192,28 @@ public function lOptionDevraitEtreSelectionneeDans($optionValue, $cssSelector)
*/
public function jeDevraisVoirLElementCorrespondantAuXpath($element)
{
$this->getSubcontext('mink')->assertSession()->elementExists('xpath', $element);
$this->assertSession()->elementExists('xpath', $element);
}

/**
* @Then /^(?:|que )je devrais voir (\d+) éléments? correspondant au xpath "([^"]*)"$/
*/
public function jeDevraisVoirElementsCorrespondantAuXpath($num, $element)
{
$this->getSubcontext('mink')->assertSession()->elementsCount('xpath', $element, intval($num));
$this->assertSession()->elementsCount('xpath', $element, intval($num));
}

/**
* @When /^je vide le champ "([^"]*)"$/
*/
public function jeVideLeChamp($field)
{
$this->getSubcontext('mink')->fillField($field, Key::BACKSPACE);
$this->fillField($field, Key::BACKSPACE);
}

protected function getElementWithCssSelector($cssSelector)
{
$session = $this->getMinkSession();
$session = $this->getSession();
$element = $session->getPage()->find(
'xpath',
$session->getSelectorsHandler()->selectorToXpath('css', $cssSelector)
Expand All @@ -235,9 +234,9 @@ protected function getElementWithCssSelector($cssSelector)
*/
protected function getVisibleField($locator)
{
$page = $this->getMinkSession()->getPage();
$page = $this->getSession()->getPage();
$elements = $page->findAll('named', array(
'field', $page->getSession()->getSelectorsHandler()->xpathLiteral($locator)
'field', $page->getSession()->getSelectorsHandler()->xpathLiteral($locator),
));
$element = false;

Expand All @@ -251,21 +250,13 @@ protected function getVisibleField($locator)
return $element;
}

/**
* @return Session
*/
public function getMinkSession()
{
return $this->getSubcontext('mink')->getSession();
}

/**
* @Then /^(?:|que )l\'élément "([^"]*)" a la propriété "([^"]*)" avec la valeur "([^"]*)"$/
*/
public function lElementALaProprieteAvecLaValeur($element, $property, $value)
{
$element = $this->getElementWithCssSelector($element);
$nodeElement = new NodeElement($element->getXpath(), $this->getMinkSession());
$nodeElement = new NodeElement($element->getXpath(), $this->getSession());

return $nodeElement->getAttribute($property) === $value;
}
Expand All @@ -276,7 +267,7 @@ public function lElementALaProprieteAvecLaValeur($element, $property, $value)
public function lElementNAPasLaProprieteAvecLaValeur($element, $property, $value)
{
$element = $this->getElementWithCssSelector($element);
$nodeElement = new NodeElement($element->getXpath(), $this->getMinkSession());
$nodeElement = new NodeElement($element->getXpath(), $this->getSession());

return (!$nodeElement->hasAttribute($property)) || ($nodeElement->getAttribute($property) !== $value);
}
Expand All @@ -286,7 +277,7 @@ public function lElementNAPasLaProprieteAvecLaValeur($element, $property, $value
*/
public function jeVaisSurLaFenetre($name)
{
$this->getMinkSession()->switchToWindow($name);
$this->getSession()->switchToWindow($name);
}

/**
Expand All @@ -302,7 +293,7 @@ public function lElementDevraitContenirLaDateCouranteAvecUneApproximationDeSecon
sprintf("Le motif de date %s n'a pas été trouvé dans l'élément %s", $pattern, $cssSelector)
);
}
if (time() - \DateTime::createFromFormat('d/m/Y H:i:s', $matches[0])->getTimestamp() > $approximation ) {
if (time() - \DateTime::createFromFormat('d/m/Y H:i:s', $matches[0])->getTimestamp() > $approximation) {
throw new \PHPUnit_Framework_ExpectationFailedException(
sprintf("La date %s se trouvant dans l'élément %s est en dehors de l'approximation de(s) %d seconde(s)",
$matches[0],
Expand Down Expand Up @@ -341,10 +332,10 @@ public function lElementDevraitContenirLaDateCourante($cssSelector)
*/
private function getRadioButton($label)
{
$radioButton = $this->getMinkSession()->getPage()->findField($label);
$radioButton = $this->getSession()->getPage()->findField($label);

if (null === $radioButton) {
throw new ElementNotFoundException($this->getMinkSession(), 'form field', 'id|name|label|value', $label);
throw new ElementNotFoundException($this->getSession(), 'form field', 'id|name|label|value', $label);
}

return $radioButton;
Expand All @@ -363,7 +354,7 @@ public function jeSelectionneLeBoutonRadio($label)
{
$radioButton = $this->getRadioButton($label);

$this->getMinkSession()->getDriver()->click($radioButton->getXPath());
$this->getSession()->getDriver()->click($radioButton->getXPath());
}

/**
Expand Down Expand Up @@ -432,9 +423,9 @@ public function leLienDoitEtreActive($locator)
*/
protected function elementIsDisabled($type, $locator)
{
$session = $this->getMinkSession();
$session = $this->getSession();
$element = $session->getPage()->find('named', array(
$type, $session->getSelectorsHandler()->xpathLiteral($locator)
$type, $session->getSelectorsHandler()->xpathLiteral($locator),
));

if (null === $element) {
Expand All @@ -454,7 +445,7 @@ protected function elementIsDisabled($type, $locator)
public function driverSupportsJs()
{
try {
$this->getMinkSession()->getDriver()->evaluateScript('');
$this->getSession()->getDriver()->evaluateScript('');
} catch (UnsupportedDriverActionException $exception) {
return false;
}
Expand Down

0 comments on commit 363341e

Please sign in to comment.