WebDriverBy.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. /**
  17. * The basic 8 mechanisms supported by webdriver to locate a web element.
  18. * ie. 'class name', 'css selector', 'id', 'name', 'link text',
  19. * 'partial link text', 'tag name' and 'xpath'.
  20. *
  21. * @see WebDriver::findElement, WebDriverElement::findElement
  22. */
  23. class WebDriverBy
  24. {
  25. /**
  26. * @var string
  27. */
  28. private $mechanism;
  29. /**
  30. * @var string
  31. */
  32. private $value;
  33. protected function __construct($mechanism, $value)
  34. {
  35. $this->mechanism = $mechanism;
  36. $this->value = $value;
  37. }
  38. /**
  39. * @return string
  40. */
  41. public function getMechanism()
  42. {
  43. return $this->mechanism;
  44. }
  45. /**
  46. * @return string
  47. */
  48. public function getValue()
  49. {
  50. return $this->value;
  51. }
  52. /**
  53. * Locates elements whose class name contains the search value; compound class
  54. * names are not permitted.
  55. *
  56. * @param string $class_name
  57. * @return WebDriverBy
  58. */
  59. public static function className($class_name)
  60. {
  61. return new static('class name', $class_name);
  62. }
  63. /**
  64. * Locates elements matching a CSS selector.
  65. *
  66. * @param string $css_selector
  67. * @return WebDriverBy
  68. */
  69. public static function cssSelector($css_selector)
  70. {
  71. return new static('css selector', $css_selector);
  72. }
  73. /**
  74. * Locates elements whose ID attribute matches the search value.
  75. *
  76. * @param string $id
  77. * @return WebDriverBy
  78. */
  79. public static function id($id)
  80. {
  81. return new static('id', $id);
  82. }
  83. /**
  84. * Locates elements whose NAME attribute matches the search value.
  85. *
  86. * @param string $name
  87. * @return WebDriverBy
  88. */
  89. public static function name($name)
  90. {
  91. return new static('name', $name);
  92. }
  93. /**
  94. * Locates anchor elements whose visible text matches the search value.
  95. *
  96. * @param string $link_text
  97. * @return WebDriverBy
  98. */
  99. public static function linkText($link_text)
  100. {
  101. return new static('link text', $link_text);
  102. }
  103. /**
  104. * Locates anchor elements whose visible text partially matches the search
  105. * value.
  106. *
  107. * @param string $partial_link_text
  108. * @return WebDriverBy
  109. */
  110. public static function partialLinkText($partial_link_text)
  111. {
  112. return new static('partial link text', $partial_link_text);
  113. }
  114. /**
  115. * Locates elements whose tag name matches the search value.
  116. *
  117. * @param string $tag_name
  118. * @return WebDriverBy
  119. */
  120. public static function tagName($tag_name)
  121. {
  122. return new static('tag name', $tag_name);
  123. }
  124. /**
  125. * Locates elements matching an XPath expression.
  126. *
  127. * @param string $xpath
  128. * @return WebDriverBy
  129. */
  130. public static function xpath($xpath)
  131. {
  132. return new static('xpath', $xpath);
  133. }
  134. }