WebElement.pm 10 KB

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