WebDriverWindow.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. // Copyright 2004-present Facebook. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. namespace Facebook\WebDriver;
  16. use Facebook\WebDriver\Exception\IndexOutOfBoundsException;
  17. use Facebook\WebDriver\Remote\DriverCommand;
  18. use Facebook\WebDriver\Remote\ExecuteMethod;
  19. /**
  20. * An abstraction allowing the driver to manipulate the browser's window
  21. */
  22. class WebDriverWindow
  23. {
  24. /**
  25. * @var ExecuteMethod
  26. */
  27. protected $executor;
  28. public function __construct(ExecuteMethod $executor)
  29. {
  30. $this->executor = $executor;
  31. }
  32. /**
  33. * Get the position of the current window, relative to the upper left corner
  34. * of the screen.
  35. *
  36. * @return WebDriverPoint The current window position.
  37. */
  38. public function getPosition()
  39. {
  40. $position = $this->executor->execute(
  41. DriverCommand::GET_WINDOW_POSITION,
  42. [':windowHandle' => 'current']
  43. );
  44. return new WebDriverPoint(
  45. $position['x'],
  46. $position['y']
  47. );
  48. }
  49. /**
  50. * Get the size of the current window. This will return the outer window
  51. * dimension, not just the view port.
  52. *
  53. * @return WebDriverDimension The current window size.
  54. */
  55. public function getSize()
  56. {
  57. $size = $this->executor->execute(
  58. DriverCommand::GET_WINDOW_SIZE,
  59. [':windowHandle' => 'current']
  60. );
  61. return new WebDriverDimension(
  62. $size['width'],
  63. $size['height']
  64. );
  65. }
  66. /**
  67. * Maximizes the current window if it is not already maximized
  68. *
  69. * @return WebDriverWindow The instance.
  70. */
  71. public function maximize()
  72. {
  73. $this->executor->execute(
  74. DriverCommand::MAXIMIZE_WINDOW,
  75. [':windowHandle' => 'current']
  76. );
  77. return $this;
  78. }
  79. /**
  80. * Set the size of the current window. This will change the outer window
  81. * dimension, not just the view port.
  82. *
  83. * @param WebDriverDimension $size
  84. * @return WebDriverWindow The instance.
  85. */
  86. public function setSize(WebDriverDimension $size)
  87. {
  88. $params = [
  89. 'width' => $size->getWidth(),
  90. 'height' => $size->getHeight(),
  91. ':windowHandle' => 'current',
  92. ];
  93. $this->executor->execute(DriverCommand::SET_WINDOW_SIZE, $params);
  94. return $this;
  95. }
  96. /**
  97. * Set the position of the current window. This is relative to the upper left
  98. * corner of the screen.
  99. *
  100. * @param WebDriverPoint $position
  101. * @return WebDriverWindow The instance.
  102. */
  103. public function setPosition(WebDriverPoint $position)
  104. {
  105. $params = [
  106. 'x' => $position->getX(),
  107. 'y' => $position->getY(),
  108. ':windowHandle' => 'current',
  109. ];
  110. $this->executor->execute(DriverCommand::SET_WINDOW_POSITION, $params);
  111. return $this;
  112. }
  113. /**
  114. * Get the current browser orientation.
  115. *
  116. * @return string Either LANDSCAPE|PORTRAIT
  117. */
  118. public function getScreenOrientation()
  119. {
  120. return $this->executor->execute(DriverCommand::GET_SCREEN_ORIENTATION);
  121. }
  122. /**
  123. * Set the browser orientation. The orientation should either
  124. * LANDSCAPE|PORTRAIT
  125. *
  126. * @param string $orientation
  127. * @throws IndexOutOfBoundsException
  128. * @return WebDriverWindow The instance.
  129. */
  130. public function setScreenOrientation($orientation)
  131. {
  132. $orientation = strtoupper($orientation);
  133. if (!in_array($orientation, ['PORTRAIT', 'LANDSCAPE'])) {
  134. throw new IndexOutOfBoundsException(
  135. 'Orientation must be either PORTRAIT, or LANDSCAPE'
  136. );
  137. }
  138. $this->executor->execute(
  139. DriverCommand::SET_SCREEN_ORIENTATION,
  140. ['orientation' => $orientation]
  141. );
  142. return $this;
  143. }
  144. }