WebElement.pm 10 KB

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