WebElement.pm 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. package Selenium::Remote::WebElement;
  2. use Moo;
  3. use Carp qw(croak);
  4. =head1 NAME
  5. Selenium::Remote::WebElement - Representation of an HTML Element used by Selenium Remote Driver
  6. =cut
  7. =head1 DESCRIPTION
  8. Selenium Webdriver represents all the HTML elements as WebElement. This module
  9. provides a mechanism to represent them as objects & perform various actions on
  10. the related elements. This module should not be instantiated directly by the end
  11. user. Selenium::Remote::Driver instantiates this module when required. Typically,
  12. the find_element method in Selenium::Remote::Driver returns this object on which
  13. various element related operations can be carried out.
  14. =cut
  15. =head1 FUNCTIONS
  16. =cut
  17. has 'id' => (
  18. is => 'rw',
  19. );
  20. has 'driver' => (
  21. is => 'rw',
  22. handles => [qw(_execute_command)],
  23. );
  24. =head2 click
  25. Description:
  26. Click the element.
  27. Usage:
  28. $elem->click();
  29. =cut
  30. sub click {
  31. my ($self) = @_;
  32. my $res = { 'command' => 'clickElement', 'id' => $self->id };
  33. return $self->_execute_command($res);
  34. }
  35. =head2 submit
  36. Description:
  37. Submit a FORM element. The submit command may also be applied to any element
  38. that is a descendant of a FORM element.
  39. Usage:
  40. $elem->submit();
  41. =cut
  42. sub submit {
  43. my ($self) = @_;
  44. my $res = { 'command' => 'submitElement', 'id' => $self->id };
  45. return $self->_execute_command($res);
  46. }
  47. =head2 send_keys
  48. Description:
  49. Send a sequence of key strokes to an element. If you want to send specific
  50. Keyboard events, then use the WDKeys module along with theis method. See e.g.
  51. for reference
  52. Input: 1
  53. Required:
  54. {ARRAY | STRING} - Array of strings or a string.
  55. Usage:
  56. $elem->send_keys('abcd', 'efg');
  57. $elem->send_keys('hijk');
  58. or
  59. # include the WDKeys module
  60. use Selenium::Remote::WDKeys;
  61. .
  62. .
  63. $elem->send_keys(KEYS->{'space'}, KEYS->{'enter'});
  64. =cut
  65. sub send_keys {
  66. my ( $self, @strings ) = @_;
  67. my $res = { 'command' => 'sendKeysToElement', 'id' => $self->id };
  68. map { $_ .= "" } @strings;
  69. my $params = {
  70. 'value' => \@strings,
  71. };
  72. return $self->_execute_command( $res, $params );
  73. }
  74. =head2 is_selected
  75. Description:
  76. Determine if an OPTION element, or an INPUT element of type checkbox or
  77. radiobutton is currently selected.
  78. Output:
  79. BOOLEAN - whether the element is selected
  80. Usage:
  81. $elem->is_selected();
  82. =cut
  83. sub is_selected {
  84. my ($self) = @_;
  85. my $res = { 'command' => 'isElementSelected', 'id' => $self->id };
  86. return $self->_execute_command($res);
  87. }
  88. =head2 set_selected
  89. Description:
  90. Select an OPTION element, or an INPUT element of type checkbox or radiobutton.
  91. Usage:
  92. $elem->set_selected();
  93. Note: DEPRECATED -- use click instead
  94. =cut
  95. sub set_selected {
  96. my ($self) = @_;
  97. my $res = { 'command' => 'setElementSelected', 'id' => $self->id };
  98. return $self->_execute_command($res);
  99. }
  100. =head2 toggle
  101. Description:
  102. Toggle whether an OPTION element, or an INPUT element of type checkbox or
  103. radiobutton is currently selected.
  104. Output:
  105. BOOLEAN - Whether the element is selected after toggling its state.
  106. Usage:
  107. $elem->toggle();
  108. Note: DEPRECATED -- use click instead
  109. =cut
  110. sub toggle {
  111. my ($self) = @_;
  112. my $res = { 'command' => 'toggleElement', 'id' => $self->id };
  113. return $self->_execute_command($res);
  114. }
  115. =head2 is_enabled
  116. Description:
  117. Determine if an element is currently enabled.
  118. Output:
  119. BOOLEAN - Whether the element is enabled.
  120. Usage:
  121. $elem->is_enabled();
  122. =cut
  123. sub is_enabled {
  124. my ($self) = @_;
  125. my $res = { 'command' => 'isElementEnabled', 'id' => $self->id };
  126. return $self->_execute_command($res);
  127. }
  128. =head2 get_element_location
  129. Description:
  130. Determine an element's location on the page. The point (0, 0) refers to the
  131. upper-left corner of the page.
  132. Output:
  133. HASH - The X and Y coordinates for the element on the page.
  134. Usage:
  135. $elem->get_element_location();
  136. =cut
  137. sub get_element_location {
  138. my ($self) = @_;
  139. my $res = { 'command' => 'getElementLocation', 'id' => $self->id };
  140. return $self->_execute_command($res);
  141. }
  142. =head2 get_element_location_in_view
  143. Description:
  144. Determine an element's location on the screen once it has been scrolled
  145. into view.
  146. Note: This is considered an internal command and should only be used to
  147. determine an element's location for correctly generating native events.
  148. Output:
  149. {x:number, y:number} The X and Y coordinates for the element on the page.
  150. Usage:
  151. $elem->get_element_location_in_view();
  152. =cut
  153. sub get_element_location_in_view {
  154. my ($self) = @_;
  155. my $res = { 'command' => 'getElementLocationInView', 'id' => $self->id };
  156. return $self->_execute_command($res);
  157. }
  158. =head2 get_tag_name
  159. Description:
  160. Query for an element's tag name.
  161. Output:
  162. STRING - The element's tag name, as a lowercase string.
  163. Usage:
  164. $elem->get_tag_name();
  165. =cut
  166. sub get_tag_name {
  167. my ($self) = @_;
  168. my $res = { 'command' => 'getElementTagName', 'id' => $self->id };
  169. return $self->_execute_command($res);
  170. }
  171. =head2 clear
  172. Description:
  173. Clear a TEXTAREA or text INPUT element's value.
  174. Usage:
  175. $elem->clear();
  176. =cut
  177. sub clear {
  178. my ($self) = @_;
  179. my $res = { 'command' => 'clearElement', 'id' => $self->id };
  180. return $self->_execute_command($res);
  181. }
  182. =head2 get_attribute
  183. Description:
  184. Get the value of an element's attribute.
  185. Input: 1
  186. Required:
  187. STRING - name of the attribute of the element
  188. Output:
  189. {STRING | NULL} The value of the attribute, or null if it is not set on the element.
  190. Usage:
  191. $elem->get_attribute('name');
  192. =cut
  193. sub get_attribute {
  194. my ( $self, $attr_name ) = @_;
  195. if ( not defined $attr_name ) {
  196. croak 'Attribute name not provided';
  197. }
  198. my $res = {
  199. 'command' => 'getElementAttribute',
  200. 'id' => $self->id,
  201. 'name' => $attr_name,
  202. };
  203. return $self->_execute_command($res);
  204. }
  205. =head2 get_value
  206. Description:
  207. Query for the value of an element, as determined by its value attribute.
  208. Output:
  209. {STRING | NULL} The element's value, or null if it doesn't have a value attribute.
  210. Usage:
  211. $elem->get_value();
  212. =cut
  213. sub get_value {
  214. my ($self) = @_;
  215. return $self->get_attribute('value');
  216. }
  217. =head2 is_displayed
  218. Description:
  219. Determine if an element is currently displayed.
  220. Output:
  221. BOOLEAN - Whether the element is displayed.
  222. Usage:
  223. $elem->is_displayed();
  224. =cut
  225. sub is_displayed {
  226. my ($self) = @_;
  227. my $res = { 'command' => 'isElementDisplayed', 'id' => $self->id };
  228. return $self->_execute_command($res);
  229. }
  230. =head2 drag
  231. Description:
  232. Drag and drop an element. The distance to drag an element should be
  233. specified relative to the upper-left corner of the page and it starts at 0,0
  234. Input: 2
  235. Required:
  236. NUMBER - X axis distance in pixels
  237. NUMBER - Y axis distance in pixels
  238. Usage:
  239. $elem->drag(216,158);
  240. =cut
  241. sub drag {
  242. my ( $self, $x, $y ) = @_;
  243. if ( ( not defined $x ) || ( not defined $y ) ) {
  244. croak 'X & Y pixel coordinates not provided';
  245. }
  246. my $res = { 'command' => 'dragElement', 'id' => $self->id };
  247. my $params = {
  248. 'x' => $x,
  249. 'y' => $y,
  250. };
  251. return $self->_execute_command( $res, $params );
  252. }
  253. =head2 get_size
  254. Description:
  255. Determine an element's size in pixels. The size will be returned with width
  256. and height properties.
  257. Output:
  258. HASH - The width and height of the element, in pixels.
  259. Usage:
  260. $elem->get_size();
  261. =cut
  262. sub get_size {
  263. my ($self) = @_;
  264. my $res = { 'command' => 'getElementSize', 'id' => $self->id };
  265. return $self->_execute_command($res);
  266. }
  267. =head2 get_text
  268. Description:
  269. Get the innerText of the element.
  270. Output:
  271. STRING - innerText of an element
  272. Usage:
  273. $elem->get_text();
  274. =cut
  275. sub get_text {
  276. my ($self) = @_;
  277. my $res = { 'command' => 'getElementText', 'id' => $self->id };
  278. return $self->_execute_command($res);
  279. }
  280. =head2 get_css_attribute
  281. Description:
  282. Query the value of an element's computed CSS property. The CSS property to
  283. query should be specified using the CSS property name, not the JavaScript
  284. property name (e.g. background-color instead of backgroundColor).
  285. Input: 1
  286. Required:
  287. STRING - name of the css-attribute
  288. Output:
  289. STRING - Value of the css attribute
  290. Usage:
  291. $elem->get_css_attribute('background-color');
  292. =cut
  293. sub get_css_attribute {
  294. my ( $self, $attr_name ) = @_;
  295. if ( not defined $attr_name ) {
  296. croak 'CSS attribute name not provided';
  297. }
  298. my $res = {
  299. 'command' => 'getElementValueOfCssProperty',
  300. 'id' => $self->id,
  301. 'property_name' => $attr_name,
  302. };
  303. return $self->_execute_command($res);
  304. }
  305. =head2 describe
  306. Description:
  307. Describe the identified element
  308. Usage:
  309. $elem->describe();
  310. =cut
  311. sub describe {
  312. my ($self) = @_;
  313. my $res = { 'command' => 'describeElement', 'id' => $self->id };
  314. return $self->_execute_command($res);
  315. }
  316. 1;
  317. =head1 SEE ALSO
  318. For more information about Selenium , visit the website at
  319. L<http://code.google.com/p/selenium/>.
  320. =head1 BUGS
  321. The Selenium issue tracking system is available online at
  322. L<http://github.com/gempesaw/Selenium-Remote-Driver/issues>.
  323. =head1 CURRENT MAINTAINER
  324. Daniel Gempesaw C<< <gempesaw@gmail.com> >>
  325. =head1 AUTHOR
  326. Perl Bindings for Selenium Remote Driver by Aditya Ivaturi C<< <ivaturi@gmail.com> >>
  327. =head1 LICENSE
  328. Copyright (c) 2010-2011 Aditya Ivaturi, Gordon Child
  329. Licensed under the Apache License, Version 2.0 (the "License");
  330. you may not use this file except in compliance with the License.
  331. You may obtain a copy of the License at
  332. http://www.apache.org/licenses/LICENSE-2.0
  333. Unless required by applicable law or agreed to in writing, software
  334. distributed under the License is distributed on an "AS IS" BASIS,
  335. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  336. See the License for the specific language governing permissions and
  337. limitations under the License.
  338. 480: hit eof while in pod documentation (no =cut seen)
  339. this can cause trouble with some pod utilities