1
0

WebElement.pm 9.8 KB

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