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 5, 2014
1 parent c66979f commit 876e8b1
Showing 1 changed file with 38 additions and 47 deletions.
85 changes: 38 additions & 47 deletions src/Pfd/Behat/Context/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,22 @@

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;

/**
* 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 @@ -47,55 +48,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 @@ -119,7 +118,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 @@ -136,7 +135,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 @@ -157,7 +156,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 @@ -167,7 +166,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 @@ -192,28 +191,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 @@ -234,9 +233,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 @@ -250,21 +249,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 @@ -275,7 +266,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 @@ -285,7 +276,7 @@ public function lElementNAPasLaProprieteAvecLaValeur($element, $property, $value
*/
public function jeVaisSurLaFenetre($name)
{
$this->getMinkSession()->switchToWindow($name);
$this->getSession()->switchToWindow($name);
}

/**
Expand All @@ -301,7 +292,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 @@ -340,10 +331,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 @@ -362,7 +353,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 @@ -431,9 +422,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 Down

0 comments on commit 876e8b1

Please sign in to comment.