WebElement.pm 10 KB

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