RemoteTargetLocator.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\Remote;
  16. use Facebook\WebDriver\WebDriver;
  17. use Facebook\WebDriver\WebDriverAlert;
  18. use Facebook\WebDriver\WebDriverElement;
  19. use Facebook\WebDriver\WebDriverTargetLocator;
  20. /**
  21. * Used to locate a given frame or window for RemoteWebDriver.
  22. */
  23. class RemoteTargetLocator implements WebDriverTargetLocator
  24. {
  25. /**
  26. * @var ExecuteMethod
  27. */
  28. protected $executor;
  29. /**
  30. * @var WebDriver
  31. */
  32. protected $driver;
  33. public function __construct($executor, $driver)
  34. {
  35. $this->executor = $executor;
  36. $this->driver = $driver;
  37. }
  38. /**
  39. * Switch to the main document if the page contains iframes. Otherwise, switch
  40. * to the first frame on the page.
  41. *
  42. * @return WebDriver The driver focused on the top window or the first frame.
  43. */
  44. public function defaultContent()
  45. {
  46. $params = ['id' => null];
  47. $this->executor->execute(DriverCommand::SWITCH_TO_FRAME, $params);
  48. return $this->driver;
  49. }
  50. /**
  51. * Switch to the iframe by its id or name.
  52. *
  53. * @param WebDriverElement|string $frame The WebDriverElement,
  54. * the id or the name of the frame.
  55. * @return WebDriver The driver focused on the given frame.
  56. */
  57. public function frame($frame)
  58. {
  59. if ($frame instanceof WebDriverElement) {
  60. $id = ['ELEMENT' => $frame->getID()];
  61. } else {
  62. $id = (string) $frame;
  63. }
  64. $params = ['id' => $id];
  65. $this->executor->execute(DriverCommand::SWITCH_TO_FRAME, $params);
  66. return $this->driver;
  67. }
  68. /**
  69. * Switch the focus to another window by its handle.
  70. *
  71. * @param string $handle The handle of the window to be focused on.
  72. * @return WebDriver The driver focused on the given window.
  73. * @see WebDriver::getWindowHandles
  74. */
  75. public function window($handle)
  76. {
  77. $params = ['name' => (string) $handle];
  78. $this->executor->execute(DriverCommand::SWITCH_TO_WINDOW, $params);
  79. return $this->driver;
  80. }
  81. /**
  82. * Switch to the currently active modal dialog for this particular driver
  83. * instance.
  84. *
  85. * @return WebDriverAlert
  86. */
  87. public function alert()
  88. {
  89. return new WebDriverAlert($this->executor);
  90. }
  91. /**
  92. * Switches to the element that currently has focus within the document
  93. * currently "switched to", or the body element if this cannot be detected.
  94. *
  95. * @return RemoteWebElement
  96. */
  97. public function activeElement()
  98. {
  99. $response = $this->driver->execute(DriverCommand::GET_ACTIVE_ELEMENT, []);
  100. $method = new RemoteExecuteMethod($this->driver);
  101. return new RemoteWebElement($method, $response['ELEMENT']);
  102. }
  103. }