1
0

WebElement.pm 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. my $res = { 'command' => 'sendKeysToElement', 'id' => $self->id };
  66. map { $_ .= "" } @strings;
  67. my $params = {
  68. 'value' => \@strings,
  69. };
  70. return $self->_execute_command( $res, $params );
  71. }
  72. =head2 is_selected
  73. Description:
  74. Determine if an OPTION element, or an INPUT element of type checkbox or
  75. radiobutton is currently selected.
  76. Output:
  77. BOOLEAN - whether the element is selected
  78. Usage:
  79. $elem->is_selected();
  80. =cut
  81. sub is_selected {
  82. my ($self) = @_;
  83. my $res = { 'command' => 'isElementSelected', 'id' => $self->id };
  84. return $self->_execute_command($res);
  85. }
  86. =head2 set_selected
  87. Description:
  88. Select an OPTION element, or an INPUT element of type checkbox or radiobutton.
  89. Usage:
  90. $elem->set_selected();
  91. Note: DEPRECATED -- use click instead
  92. =cut
  93. sub set_selected {
  94. my ($self) = @_;
  95. my $res = { 'command' => 'setElementSelected', 'id' => $self->id };
  96. return $self->_execute_command($res);
  97. }
  98. =head2 toggle
  99. Description:
  100. Toggle whether an OPTION element, or an INPUT element of type checkbox or
  101. radiobutton is currently selected.
  102. Output:
  103. BOOLEAN - Whether the element is selected after toggling its state.
  104. Usage:
  105. $elem->toggle();
  106. Note: DEPRECATED -- use click instead
  107. =cut
  108. sub toggle {
  109. my ($self) = @_;
  110. my $res = { 'command' => 'toggleElement', 'id' => $self->id };
  111. return $self->_execute_command($res);
  112. }
  113. =head2 is_enabled
  114. Description:
  115. Determine if an element is currently enabled.
  116. Output:
  117. BOOLEAN - Whether the element is enabled.
  118. Usage:
  119. $elem->is_enabled();
  120. =cut
  121. sub is_enabled {
  122. my ($self) = @_;
  123. my $res = { 'command' => 'isElementEnabled', 'id' => $self->id };
  124. return $self->_execute_command($res);
  125. }
  126. =head2 get_element_location
  127. Description:
  128. Determine an element's location on the page. The point (0, 0) refers to the
  129. upper-left corner of the page.
  130. Output:
  131. HASH - The X and Y coordinates for the element on the page.
  132. Usage:
  133. $elem->get_element_location();
  134. =cut
  135. sub get_element_location {
  136. my ($self) = @_;
  137. my $res = { 'command' => 'getElementLocation', 'id' => $self->id };
  138. return $self->_execute_command($res);
  139. }
  140. =head2 get_element_location_in_view
  141. Description:
  142. Determine an element's location on the screen once it has been scrolled
  143. into view.
  144. Note: This is considered an internal command and should only be used to
  145. determine an element's location for correctly generating native events.
  146. Output:
  147. {x:number, y:number} The X and Y coordinates for the element on the page.
  148. Usage:
  149. $elem->get_element_location_in_view();
  150. =cut
  151. sub get_element_location_in_view {
  152. my ($self) = @_;
  153. my $res = { 'command' => 'getElementLocationInView', 'id' => $self->id };
  154. return $self->_execute_command($res);
  155. }
  156. =head2 get_tag_name
  157. Description:
  158. Query for an element's tag name.
  159. Output:
  160. STRING - The element's tag name, as a lowercase string.
  161. Usage:
  162. $elem->get_tag_name();
  163. =cut
  164. sub get_tag_name {
  165. my ($self) = @_;
  166. my $res = { 'command' => 'getElementTagName', 'id' => $self->id };
  167. return $self->_execute_command($res);
  168. }
  169. =head2 clear
  170. Description:
  171. Clear a TEXTAREA or text INPUT element's value.
  172. Usage:
  173. $elem->clear();
  174. =cut
  175. sub clear {
  176. my ($self) = @_;
  177. my $res = { 'command' => 'clearElement', 'id' => $self->id };
  178. return $self->_execute_command($res);
  179. }
  180. =head2 get_attribute
  181. Description:
  182. Get the value of an element's attribute.
  183. Input: 1
  184. Required:
  185. STRING - name of the attribute of the element
  186. Output:
  187. {STRING | NULL} The value of the attribute, or null if it is not set on the element.
  188. Usage:
  189. $elem->get_attribute('name');
  190. =cut
  191. sub get_attribute {
  192. my ( $self, $attr_name ) = @_;
  193. if ( not defined $attr_name ) {
  194. croak 'Attribute name not provided';
  195. }
  196. my $res = {
  197. 'command' => 'getElementAttribute',
  198. 'id' => $self->id,
  199. 'name' => $attr_name,
  200. };
  201. return $self->_execute_command($res);
  202. }
  203. =head2 get_value
  204. Description:
  205. Query for the value of an element, as determined by its value attribute.
  206. Output:
  207. {STRING | NULL} The element's value, or null if it doesn't have a value attribute.
  208. Usage:
  209. $elem->get_value();
  210. =cut
  211. sub get_value {
  212. my ($self) = @_;
  213. return $self->get_attribute('value');
  214. }
  215. =head2 is_displayed
  216. Description:
  217. Determine if an element is currently displayed.
  218. Output:
  219. BOOLEAN - Whether the element is displayed.
  220. Usage:
  221. $elem->is_displayed();
  222. =cut
  223. sub is_displayed {
  224. my ($self) = @_;
  225. my $res = { 'command' => 'isElementDisplayed', 'id' => $self->id };
  226. return $self->_execute_command($res);
  227. }
  228. =head2 drag
  229. Description:
  230. Drag and drop an element. The distance to drag an element should be
  231. specified relative to the upper-left corner of the page and it starts at 0,0
  232. Input: 2
  233. Required:
  234. NUMBER - X axis distance in pixels
  235. NUMBER - Y axis distance in pixels
  236. Usage:
  237. $elem->drag(216,158);
  238. =cut
  239. sub drag {
  240. my ( $self, $x, $y ) = @_;
  241. if ( ( not defined $x ) || ( not defined $y ) ) {
  242. croak 'X & Y pixel coordinates not provided';
  243. }
  244. my $res = { 'command' => 'dragElement', 'id' => $self->id };
  245. my $params = {
  246. 'x' => $x,
  247. 'y' => $y,
  248. };
  249. return $self->_execute_command( $res, $params );
  250. }
  251. =head2 get_size
  252. Description:
  253. Determine an element's size in pixels. The size will be returned with width
  254. and height properties.
  255. Output:
  256. HASH - The width and height of the element, in pixels.
  257. Usage:
  258. $elem->get_size();
  259. =cut
  260. sub get_size {
  261. my ($self) = @_;
  262. my $res = { 'command' => 'getElementSize', 'id' => $self->id };
  263. return $self->_execute_command($res);
  264. }
  265. =head2 get_text
  266. Description:
  267. Get the innerText of the element.
  268. Output:
  269. STRING - innerText of an element
  270. Usage:
  271. $elem->get_text();
  272. =cut
  273. sub get_text {
  274. my ($self) = @_;
  275. my $res = { 'command' => 'getElementText', 'id' => $self->id };
  276. return $self->_execute_command($res);
  277. }
  278. =head2 get_css_attribute
  279. Description:
  280. Query the value of an element's computed CSS property. The CSS property to
  281. query should be specified using the CSS property name, not the JavaScript
  282. property name (e.g. background-color instead of backgroundColor).
  283. Input: 1
  284. Required:
  285. STRING - name of the css-attribute
  286. Output:
  287. STRING - Value of the css attribute
  288. Usage:
  289. $elem->get_css_attribute('background-color');
  290. =cut
  291. sub get_css_attribute {
  292. my ( $self, $attr_name ) = @_;
  293. if ( not defined $attr_name ) {
  294. croak 'CSS attribute name not provided';
  295. }
  296. my $res = {
  297. 'command' => 'getElementValueOfCssProperty',
  298. 'id' => $self->id,
  299. 'property_name' => $attr_name,
  300. };
  301. return $self->_execute_command($res);
  302. }
  303. =head2 describe
  304. Description:
  305. Describe the identified element
  306. Usage:
  307. $elem->describe();
  308. =cut
  309. sub describe {
  310. my ($self) = @_;
  311. my $res = { 'command' => 'describeElement', 'id' => $self->id };
  312. return $self->_execute_command($res);
  313. }
  314. 1;