WebElement.pm 10 KB

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