diff --git a/src/Pfd/Behat/Context/FeatureContext.php b/src/Pfd/Behat/Context/FeatureContext.php index 97e861e..0d0c29e 100644 --- a/src/Pfd/Behat/Context/FeatureContext.php +++ b/src/Pfd/Behat/Context/FeatureContext.php @@ -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; @@ -28,11 +26,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'); } /** @@ -47,14 +48,12 @@ 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); } /** @@ -62,7 +61,7 @@ public function login($loginLabel, $login, $passwordLabel, $password, $submitLab */ public function jAttendsQuelquesSecondes() { - $this->getMinkSession()->wait(30000, '(0 === jQuery.active)'); + $this->getSession()->wait(30000, '(0 === jQuery.active)'); } /** @@ -70,7 +69,7 @@ public function jAttendsQuelquesSecondes() */ public function jAttendsSecondes($num) { - $this->getMinkSession()->wait($num * 1000); + $this->getSession()->wait($num * 1000); } /** @@ -78,12 +77,12 @@ public function jAttendsSecondes($num) */ 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() ); } @@ -91,11 +90,11 @@ public function jeSelectionneStrictementDepuis($arg1, $arg2) 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')); } /** @@ -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; @@ -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; @@ -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(); } @@ -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)); @@ -192,7 +191,7 @@ public function lOptionDevraitEtreSelectionneeDans($optionValue, $cssSelector) */ public function jeDevraisVoirLElementCorrespondantAuXpath($element) { - $this->getSubcontext('mink')->assertSession()->elementExists('xpath', $element); + $this->assertSession()->elementExists('xpath', $element); } /** @@ -200,7 +199,7 @@ public function jeDevraisVoirLElementCorrespondantAuXpath($element) */ public function jeDevraisVoirElementsCorrespondantAuXpath($num, $element) { - $this->getSubcontext('mink')->assertSession()->elementsCount('xpath', $element, intval($num)); + $this->assertSession()->elementsCount('xpath', $element, intval($num)); } /** @@ -208,12 +207,12 @@ public function jeDevraisVoirElementsCorrespondantAuXpath($num, $element) */ 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) @@ -234,7 +233,7 @@ 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) )); @@ -250,21 +249,13 @@ protected function getVisibleField($locator) return $element; } - /** - * @return Session - */ - public function getMinkSession() - { - return $this->getSubcontext('mink')->getSession(); - } - /** * @Given /^(?:|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; } @@ -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); } @@ -285,7 +276,7 @@ public function lElementNAPasLaProprieteAvecLaValeur($element, $property, $value */ public function jeVaisSurLaFenetre($name) { - $this->getMinkSession()->switchToWindow($name); + $this->getSession()->switchToWindow($name); } /** @@ -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; @@ -362,7 +353,7 @@ public function jeSelectionneLeBoutonRadio($label) { $radioButton = $this->getRadioButton($label); - $this->getMinkSession()->getDriver()->click($radioButton->getXPath()); + $this->getSession()->getDriver()->click($radioButton->getXPath()); } /** @@ -431,7 +422,7 @@ 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) ));