WebElement.pm 9.9 KB

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