patrickt

Sample of using new Mink getWindowName() functions

Nov 14th, 2013
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.71 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Reference GitHub project at: https://github.com/Behat/Mink/pull/341
  4.  *
  5.  * Sample steps:
  6.  *      Then I switch to popup
  7.  *      Then I do stuff  #Whatever actions to take in the popup window
  8.  *      Then I switch back to original window
  9.  */
  10.  
  11. use Behat\Behat\Context\ClosuredContextInterface,
  12.     Behat\Behat\Context\TranslatedContextInterface,
  13.     Behat\Behat\Context\BehatContext,
  14.     Behat\Behat\Exception\PendingException;
  15. use Behat\Gherkin\Node\PyStringNode,
  16.     Behat\Gherkin\Node\TableNode;
  17. use Behat\Mink\Exception\ResponseTextException;
  18. use Behat\MinkExtension\Context\MinkContext;
  19.  
  20. //
  21. // Require 3rd-party libraries here:
  22. //
  23. //   require_once 'PHPUnit/Autoload.php';
  24. //   require_once 'PHPUnit/Framework/Assert/Functions.php';
  25. //
  26.  
  27. /**
  28.  * Features context.
  29.  */
  30. class FeatureContext extends MinkContext {
  31.     var $originalWindowName = '';
  32.  
  33.     /**
  34.      * Initializes context.
  35.      * Every scenario gets it's own context object.
  36.      *
  37.      * @param array $parameters context parameters (set them up through behat.yml)
  38.      */
  39.     public function __construct(array $parameters) {
  40.         if (!isset($parameters['base_url'])) {
  41.             $parameters['base_url'] = 'http://api.google.com';
  42.         }
  43.     }
  44.  
  45.     /**
  46.      * Finds the system name for the current window.
  47.      *
  48.      * @Then /^I find this windows name$/
  49.      */
  50.     public function iFindThisWindowsName() {
  51.         $name = $this->getSession()->getWindowName();
  52.         echo "Window name is: '$name'\n\n";
  53.         echo "Original window name is: '{$this->originalWindowName}'\n\n";
  54.  
  55.         $names = $this->getSession()->getWindowNames();
  56.         echo "Window names are: '" . print_r($names, true) . "'\n\n";
  57.  
  58.         #die();
  59.    }
  60.  
  61.     /**
  62.      * Sets the current window as the original window.
  63.      *
  64.      * @return string The original window name
  65.      */
  66.     private function saveOriginalWindow() {
  67.         $originalWindowName = $this->getSession()->getWindowName(); //Get the original name
  68.  
  69.         if (empty($this->originalWindowName)) {
  70.             $this->originalWindowName = $originalWindowName;
  71.         }
  72.  
  73.         return $originalWindowName;
  74.     }
  75.  
  76.     /**
  77.      * Saves the current window as the original window.
  78.      *
  79.      * @Then /^I save my current window$/
  80.      */
  81.     public function iSaveMyCurrentWindow() {
  82.         $originalWindowName = $this->saveOriginalWindow();
  83.     }
  84.  
  85.     /**
  86.      * Switches over to the most recent popup window.
  87.      *
  88.      * @Then /^I switch to popup$/
  89.      */
  90.     public function iSwitchToPopup() {
  91.         $originalWindowName = $this->saveOriginalWindow();
  92.         $this->getSession()->getPage()->pressButton("Withdraw"); //Pressing the withdraw button (do I need this?)
  93.         $popupName = $this->getNewPopup($originalWindowName);
  94.  
  95.         //Switch to the popup Window
  96.         $this->getSession()->switchToWindow($popupName);
  97.     }
  98.  
  99.     /**
  100.      * Switches back to the original window.
  101.      *
  102.      * @Then /^I switch back to original window$/
  103.      */
  104.     public function iSwitchBackToOriginalWindow() {
  105.         //Switch to the original window
  106.         $this->getSession()->switchToWindow($this->originalWindowName);
  107.     }
  108.  
  109.     /**
  110.      * This gets the window name of the new popup.
  111.      */
  112.     private function getNewPopup($originalWindowName = NULL) {
  113.         //Get all of the window names first
  114.         $names = $this->getSession()->getWindowNames();
  115.  
  116.         //Now it should be the last window name
  117.         $last = array_pop($names);
  118.  
  119.         if (!empty($originalWindowName)) {
  120.             while ($last == $originalWindowName && !empty($names)) {
  121.                 $last = array_pop($names);
  122.             }
  123.         }
  124.  
  125.         return $last;
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment