Guest User

Untitled

a guest
Mar 24th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.06 KB | None | 0 0
  1. <?php
  2. /**
  3. * some functions to create a new tab/window (it's up to your personal settings),
  4. * to move between them (to the right, to the left or by it's number in the list of windows.
  5. * You can also close the current window or close all (the first window will be open as if you
  6. * close it, the Scenario will fails).
  7. *
  8. * I block the actual driver is not an instance of Selenium2 driver, but I don't really remember
  9. * if it's an obligation :)
  10. *
  11. * @author netraagal
  12. */
  13.  
  14. namespace App\Features;
  15.  
  16. use Behat\Mink\Driver\Selenium2Driver;
  17.  
  18. class WindowFeatureContext extends MinkContext implements SnippetAcceptingContext
  19. {
  20.  
  21. /**
  22. * iOpenANewWindow
  23. * I use directly count() without -1 because I have noticed the number is not
  24. * updated directly (if I do count()-1 i will get the second latest)
  25. *
  26. * @When /^(?:|I )open a new (?:window|tab)$/
  27. *
  28. * @return FeatureContext
  29. */
  30. public function iOpenANewWindow(): self
  31. {
  32. $driver = $this->getSession()->getDriver();
  33.  
  34. if (!$driver instanceof Selenium2Driver)
  35. return $this;
  36.  
  37. $driver->executeScript("window.open('/','_blank');");
  38. $this->iSwitchToTheWindow(count($driver->getWindowNames()), true);
  39.  
  40. return $this;
  41. }
  42.  
  43. /**
  44. * iOpenANewWindowUrl
  45. *
  46. * @When /^(?:|I )open (?:|a )new (?:window|tab) on "(?P<url>[^"]+)"$/
  47. *
  48. * @param string $url
  49. * @return FeatureContext
  50. */
  51. public function iOpenANewWindowUrl(string $url): self
  52. {
  53. $driver = $this->getSession()->getDriver();
  54.  
  55. if (!$driver instanceof Selenium2Driver)
  56. return $this;
  57.  
  58. $driver->executeScript("window.open('".$url."','_blank');");
  59. $this->iSwitchToTheWindow(count($driver->getWindowNames()), true);
  60.  
  61. return $this;
  62. }
  63.  
  64. /**
  65. * iSwitchToTheWindow
  66. * the $internalFunction allows me to not be bothered by the problem I mention in the iOpenANewWindow function
  67. * or if the user want to use a window which do not exists (less than 0 or more than the max of windows)
  68. *
  69. * @When /^(?:|I )switch to (?:|the )(?:window|tab) "(?P<nb>\d+)"$/
  70. *
  71. * @param int $nb
  72. * @param bool $internalFunction
  73. * @return FeatureContext
  74. */
  75. public function iSwitchToTheWindow(int $nb, bool $internalFunction = false): self
  76. {
  77. $driver = $this->getSession()->getDriver();
  78.  
  79. if (!$driver instanceof Selenium2Driver)
  80. return $this;
  81.  
  82. $windowNames = $driver->getWindowNames();
  83.  
  84. if (!$internalFunction && ( $nb < 0 || $nb >= count($windowNames) ) )
  85. {
  86. throw new \Exception(
  87. sprintf(
  88. "Error : index of window out of bounds, given '%b' but number of windows is '%b'",
  89. $nb,
  90. count($windowNames)
  91. )
  92. );
  93. }
  94. $driver->switchToWindow($windowNames[$nb-1]);
  95.  
  96. return $this;
  97. }
  98.  
  99. /**
  100. * iSwitchToTheLeftWindow
  101. * if I am on the first window, as for any browser, I go to the last
  102. *
  103. * @When /^(?:|I )switch to (?:|the )left (?:window|tab)$/
  104. *
  105. * @return FeatureContext
  106. */
  107. public function iSwitchToTheLeftWindow(): self
  108. {
  109. $driver = $this->getSession()->getDriver();
  110.  
  111. if (!$driver instanceof Selenium2Driver)
  112. return $this;
  113.  
  114. $windowNames = $driver->getWindowNames();
  115. $current_window = array_search($driver->getWindowName(), $windowNames);
  116.  
  117. if ($current_window === 0)
  118. {
  119. $driver->switchToWindow($windowNames[count($windowNames)-1]);
  120. }
  121. else {
  122. $driver->switchToWindow($windowNames[$current_window-1]);
  123. }
  124.  
  125. return $this;
  126. }
  127.  
  128. /**
  129. * iSwitchToTheRightWindow
  130. * if I am on the last window, as for any browser, I go to the first
  131. *
  132. * @When /^(?:|I )switch to (?:|the )right (?:window|tab)$/
  133. *
  134. * @return FeatureContext
  135. * @throws Exception
  136. */
  137. public function iSwitchToTheRightWindow(): self
  138. {
  139. $driver = $this->getSession()->getDriver();
  140.  
  141. if (!$driver instanceof Selenium2Driver)
  142. return $this;
  143.  
  144. $windowNames = $driver->getWindowNames();
  145. $current_window = array_search($driver->getWindowName(), $windowNames);
  146.  
  147. if ($current_window === count($windowNames)-1)
  148. {
  149. $driver->switchToWindow(0);
  150. }
  151. else {
  152. $driver->switchToWindow($windowNames[$current_window+1]);
  153. }
  154.  
  155. return $this;
  156. }
  157.  
  158. /**
  159. * reloadTheCurrentWindow
  160. *
  161. * @When /^(?:|I )reload (?:|the )current (?:window|tab)$/
  162. *
  163. * @return FeatureContext
  164. */
  165. public function reloadTheCurrentWindow(): self
  166. {
  167. if (!$this->getSession()->getDriver() instanceof Selenium2Driver)
  168. return $this;
  169.  
  170. $this->getSession()->getDriver()->reload();
  171.  
  172. return $this;
  173. }
  174.  
  175. /**
  176. * iCloseTheCurrentWindow
  177. *
  178. * @When /^(?:|I )close (?:|the )current (?:window|tab)$/
  179. *
  180. * @return FeatureContext
  181. */
  182. public function iCloseTheCurrentWindow(): self
  183. {
  184. if (!$this->getSession()->getDriver() instanceof Selenium2Driver)
  185. return $this;
  186.  
  187. $this->getSession()->getDriver()->executeScript("window.open('','_self').close();");
  188.  
  189. return $this;
  190. }
  191.  
  192. /**
  193. * iCloseAllWindows
  194. * warning : the first tab/window will always be open
  195. * as the driver must have a page to be functional
  196. *
  197. * @When /^(?:|I )close all (?:|the )(?:windows|tabs)$/
  198. *
  199. * @return FeatureContext
  200. */
  201. public function iCloseAllWindows(): self
  202. {
  203. if (!$this->getSession()->getDriver() instanceof Selenium2Driver)
  204. return $this;
  205.  
  206. $windowNames = $this->getSession()->getDriver()->getWindowNames();
  207. for ($i = count($windowNames); $i > 0; $i--)
  208. {
  209. $this->iSwitchToTheWindow($i, true);
  210. $this->iCloseTheCurrentWindow();
  211. $this->waitMilliseconds(500);
  212. }
  213. $this->iAmOnHomepage();
  214.  
  215. return $this;
  216. }
  217. }
Add Comment
Please, Sign In to add comment