Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Reference GitHub project at: https://github.com/Behat/Mink/pull/341
- *
- * Sample steps:
- * Then I switch to popup
- * Then I do stuff #Whatever actions to take in the popup window
- * Then I switch back to original window
- */
- use Behat\Behat\Context\ClosuredContextInterface,
- Behat\Behat\Context\TranslatedContextInterface,
- Behat\Behat\Context\BehatContext,
- Behat\Behat\Exception\PendingException;
- use Behat\Gherkin\Node\PyStringNode,
- Behat\Gherkin\Node\TableNode;
- use Behat\Mink\Exception\ResponseTextException;
- use Behat\MinkExtension\Context\MinkContext;
- //
- // Require 3rd-party libraries here:
- //
- // require_once 'PHPUnit/Autoload.php';
- // require_once 'PHPUnit/Framework/Assert/Functions.php';
- //
- /**
- * Features context.
- */
- class FeatureContext extends MinkContext {
- var $originalWindowName = '';
- /**
- * Initializes context.
- * Every scenario gets it's own context object.
- *
- * @param array $parameters context parameters (set them up through behat.yml)
- */
- public function __construct(array $parameters) {
- if (!isset($parameters['base_url'])) {
- $parameters['base_url'] = 'http://api.google.com';
- }
- }
- /**
- * Finds the system name for the current window.
- *
- * @Then /^I find this windows name$/
- */
- public function iFindThisWindowsName() {
- $name = $this->getSession()->getWindowName();
- echo "Window name is: '$name'\n\n";
- echo "Original window name is: '{$this->originalWindowName}'\n\n";
- $names = $this->getSession()->getWindowNames();
- echo "Window names are: '" . print_r($names, true) . "'\n\n";
- #die();
- }
- /**
- * Sets the current window as the original window.
- *
- * @return string The original window name
- */
- private function saveOriginalWindow() {
- $originalWindowName = $this->getSession()->getWindowName(); //Get the original name
- if (empty($this->originalWindowName)) {
- $this->originalWindowName = $originalWindowName;
- }
- return $originalWindowName;
- }
- /**
- * Saves the current window as the original window.
- *
- * @Then /^I save my current window$/
- */
- public function iSaveMyCurrentWindow() {
- $originalWindowName = $this->saveOriginalWindow();
- }
- /**
- * Switches over to the most recent popup window.
- *
- * @Then /^I switch to popup$/
- */
- public function iSwitchToPopup() {
- $originalWindowName = $this->saveOriginalWindow();
- $this->getSession()->getPage()->pressButton("Withdraw"); //Pressing the withdraw button (do I need this?)
- $popupName = $this->getNewPopup($originalWindowName);
- //Switch to the popup Window
- $this->getSession()->switchToWindow($popupName);
- }
- /**
- * Switches back to the original window.
- *
- * @Then /^I switch back to original window$/
- */
- public function iSwitchBackToOriginalWindow() {
- //Switch to the original window
- $this->getSession()->switchToWindow($this->originalWindowName);
- }
- /**
- * This gets the window name of the new popup.
- */
- private function getNewPopup($originalWindowName = NULL) {
- //Get all of the window names first
- $names = $this->getSession()->getWindowNames();
- //Now it should be the last window name
- $last = array_pop($names);
- if (!empty($originalWindowName)) {
- while ($last == $originalWindowName && !empty($names)) {
- $last = array_pop($names);
- }
- }
- return $last;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment