WebElement.pm 10 KB

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