WebElement.pm 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. package Selenium::Remote::WebElement;
  2. # ABSTRACT: Representation of an HTML Element used by Selenium Remote Driver
  3. use Moo;
  4. use Carp qw(croak);
  5. =head1 DESCRIPTION
  6. Selenium Webdriver represents all the HTML elements as WebElement. This module
  7. provides a mechanism to represent them as objects & perform various actions on
  8. the related elements. This module should not be instantiated directly by the end
  9. user. Selenium::Remote::Driver instantiates this module when required. Typically,
  10. the find_element method in Selenium::Remote::Driver returns this object on which
  11. various element related operations can be carried out.
  12. =cut
  13. =head1 FUNCTIONS
  14. =cut
  15. has 'id' => (
  16. is => 'rw',
  17. );
  18. has 'driver' => (
  19. is => 'rw',
  20. handles => [qw(_execute_command)],
  21. );
  22. =head2 click
  23. Description:
  24. Click the element.
  25. Usage:
  26. $elem->click();
  27. =cut
  28. sub click {
  29. my ($self) = @_;
  30. my $res = { 'command' => 'clickElement', 'id' => $self->id };
  31. return $self->_execute_command($res);
  32. }
  33. =head2 submit
  34. Description:
  35. Submit a FORM element. The submit command may also be applied to any element
  36. that is a descendant of a FORM element.
  37. Usage:
  38. $elem->submit();
  39. =cut
  40. sub submit {
  41. my ($self) = @_;
  42. my $res = { 'command' => 'submitElement', 'id' => $self->id };
  43. return $self->_execute_command($res);
  44. }
  45. =head2 send_keys
  46. Description:
  47. Send a sequence of key strokes to an element. If you want to send specific
  48. Keyboard events, then use the WDKeys module along with theis method. See e.g.
  49. for reference
  50. Input: 1
  51. Required:
  52. {ARRAY | STRING} - Array of strings or a string.
  53. Usage:
  54. $elem->send_keys('abcd', 'efg');
  55. $elem->send_keys('hijk');
  56. or
  57. # include the WDKeys module
  58. use Selenium::Remote::WDKeys;
  59. .
  60. .
  61. $elem->send_keys(KEYS->{'space'}, KEYS->{'enter'});
  62. =cut
  63. sub send_keys {
  64. my ( $self, @strings ) = @_;
  65. croak "no keys to send" unless scalar @strings >= 1;
  66. my $res = { 'command' => 'sendKeysToElement', 'id' => $self->id };
  67. map { $_ .= "" } @strings;
  68. my $params = {
  69. 'value' => \@strings,
  70. };
  71. return $self->_execute_command( $res, $params );
  72. }
  73. =head2 is_selected
  74. Description:
  75. Determine if an OPTION element, or an INPUT element of type checkbox or
  76. radiobutton is currently selected.
  77. Output:
  78. BOOLEAN - whether the element is selected
  79. Usage:
  80. $elem->is_selected();
  81. =cut
  82. sub is_selected {
  83. my ($self) = @_;
  84. my $res = { 'command' => 'isElementSelected', 'id' => $self->id };
  85. return $self->_execute_command($res);
  86. }
  87. =head2 set_selected
  88. Description:
  89. Select an OPTION element, or an INPUT element of type checkbox or radiobutton.
  90. Usage:
  91. $elem->set_selected();
  92. Note: DEPRECATED -- use click instead
  93. =cut
  94. sub set_selected {
  95. my ($self) = @_;
  96. my $res = { 'command' => 'setElementSelected', 'id' => $self->id };
  97. return $self->_execute_command($res);
  98. }
  99. =head2 toggle
  100. Description:
  101. Toggle whether an OPTION element, or an INPUT element of type checkbox or
  102. radiobutton is currently selected.
  103. Output:
  104. BOOLEAN - Whether the element is selected after toggling its state.
  105. Usage:
  106. $elem->toggle();
  107. Note: DEPRECATED -- use click instead
  108. =cut
  109. sub toggle {
  110. my ($self) = @_;
  111. my $res = { 'command' => 'toggleElement', 'id' => $self->id };
  112. return $self->_execute_command($res);
  113. }
  114. =head2 is_enabled
  115. Description:
  116. Determine if an element is currently enabled.
  117. Output:
  118. BOOLEAN - Whether the element is enabled.
  119. Usage:
  120. $elem->is_enabled();
  121. =cut
  122. sub is_enabled {
  123. my ($self) = @_;
  124. my $res = { 'command' => 'isElementEnabled', 'id' => $self->id };
  125. return $self->_execute_command($res);
  126. }
  127. =head2 get_element_location
  128. Description:
  129. Determine an element's location on the page. The point (0, 0) refers to the
  130. upper-left corner of the page.
  131. Output:
  132. HASH - The X and Y coordinates for the element on the page.
  133. Usage:
  134. $elem->get_element_location();
  135. =cut
  136. sub get_element_location {
  137. my ($self) = @_;
  138. my $res = { 'command' => 'getElementLocation', 'id' => $self->id };
  139. return $self->_execute_command($res);
  140. }
  141. =head2 get_element_location_in_view
  142. Description:
  143. Determine an element's location on the screen once it has been scrolled
  144. into view.
  145. Note: This is considered an internal command and should only be used to
  146. determine an element's location for correctly generating native events.
  147. Output:
  148. {x:number, y:number} The X and Y coordinates for the element on the page.
  149. Usage:
  150. $elem->get_element_location_in_view();
  151. =cut
  152. sub get_element_location_in_view {
  153. my ($self) = @_;
  154. my $res = { 'command' => 'getElementLocationInView', 'id' => $self->id };
  155. return $self->_execute_command($res);
  156. }
  157. =head2 get_tag_name
  158. Description:
  159. Query for an element's tag name.
  160. Output:
  161. STRING - The element's tag name, as a lowercase string.
  162. Usage:
  163. $elem->get_tag_name();
  164. =cut
  165. sub get_tag_name {
  166. my ($self) = @_;
  167. my $res = { 'command' => 'getElementTagName', 'id' => $self->id };
  168. return $self->_execute_command($res);
  169. }
  170. =head2 clear
  171. Description:
  172. Clear a TEXTAREA or text INPUT element's value.
  173. Usage:
  174. $elem->clear();
  175. =cut
  176. sub clear {
  177. my ($self) = @_;
  178. my $res = { 'command' => 'clearElement', 'id' => $self->id };
  179. return $self->_execute_command($res);
  180. }
  181. =head2 get_attribute
  182. Description:
  183. Get the value of an element's attribute.
  184. Input: 1
  185. Required:
  186. STRING - name of the attribute of the element
  187. Output:
  188. {STRING | NULL} The value of the attribute, or null if it is not set on the element.
  189. Usage:
  190. $elem->get_attribute('name');
  191. =cut
  192. sub get_attribute {
  193. my ( $self, $attr_name ) = @_;
  194. if ( not defined $attr_name ) {
  195. croak 'Attribute name not provided';
  196. }
  197. my $res = {
  198. 'command' => 'getElementAttribute',
  199. 'id' => $self->id,
  200. 'name' => $attr_name,
  201. };
  202. return $self->_execute_command($res);
  203. }
  204. =head2 get_value
  205. Description:
  206. Query for the value of an element, as determined by its value attribute.
  207. Output:
  208. {STRING | NULL} The element's value, or null if it doesn't have a value attribute.
  209. Usage:
  210. $elem->get_value();
  211. =cut
  212. sub get_value {
  213. my ($self) = @_;
  214. return $self->get_attribute('value');
  215. }
  216. =head2 is_displayed
  217. Description:
  218. Determine if an element is currently displayed.
  219. Output:
  220. BOOLEAN - Whether the element is displayed.
  221. Usage:
  222. $elem->is_displayed();
  223. =cut
  224. sub is_displayed {
  225. my ($self) = @_;
  226. my $res = { 'command' => 'isElementDisplayed', 'id' => $self->id };
  227. return $self->_execute_command($res);
  228. }
  229. =head2 drag
  230. Description:
  231. Drag and drop an element. The distance to drag an element should be
  232. specified relative to the upper-left corner of the page and it starts at 0,0
  233. Input: 2
  234. Required:
  235. NUMBER - X axis distance in pixels
  236. NUMBER - Y axis distance in pixels
  237. Usage:
  238. $elem->drag(216,158);
  239. =cut
  240. sub drag {
  241. my ( $self, $x, $y ) = @_;
  242. if ( ( not defined $x ) || ( not defined $y ) ) {
  243. croak 'X & Y pixel coordinates not provided';
  244. }
  245. my $res = { 'command' => 'dragElement', 'id' => $self->id };
  246. my $params = {
  247. 'x' => $x,
  248. 'y' => $y,
  249. };
  250. return $self->_execute_command( $res, $params );
  251. }
  252. =head2 get_size
  253. Description:
  254. Determine an element's size in pixels. The size will be returned with width
  255. and height properties.
  256. Output:
  257. HASH - The width and height of the element, in pixels.
  258. Usage:
  259. $elem->get_size();
  260. =cut
  261. sub get_size {
  262. my ($self) = @_;
  263. my $res = { 'command' => 'getElementSize', 'id' => $self->id };
  264. return $self->_execute_command($res);
  265. }
  266. =head2 get_text
  267. Description:
  268. Get the innerText of the element.
  269. Output:
  270. STRING - innerText of an element
  271. Usage:
  272. $elem->get_text();
  273. =cut
  274. sub get_text {
  275. my ($self) = @_;
  276. my $res = { 'command' => 'getElementText', 'id' => $self->id };
  277. return $self->_execute_command($res);
  278. }
  279. =head2 get_css_attribute
  280. Description:
  281. Query the value of an element's computed CSS property. The CSS property to
  282. query should be specified using the CSS property name, not the JavaScript
  283. property name (e.g. background-color instead of backgroundColor).
  284. Input: 1
  285. Required:
  286. STRING - name of the css-attribute
  287. Output:
  288. STRING - Value of the css attribute
  289. Usage:
  290. $elem->get_css_attribute('background-color');
  291. =cut
  292. sub get_css_attribute {
  293. my ( $self, $attr_name ) = @_;
  294. if ( not defined $attr_name ) {
  295. croak 'CSS attribute name not provided';
  296. }
  297. my $res = {
  298. 'command' => 'getElementValueOfCssProperty',
  299. 'id' => $self->id,
  300. 'property_name' => $attr_name,
  301. };
  302. return $self->_execute_command($res);
  303. }
  304. =head2 describe
  305. Description:
  306. Describe the identified element
  307. Usage:
  308. $elem->describe();
  309. =cut
  310. sub describe {
  311. my ($self) = @_;
  312. my $res = { 'command' => 'describeElement', 'id' => $self->id };
  313. return $self->_execute_command($res);
  314. }
  315. 1;