1
0

WebElement.pm 10 KB

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