WebElement.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. package Selenium::Remote::WebElement;
  2. # ABSTRACT: Representation of an HTML Element used by Selenium Remote Driver
  3. use Moo;
  4. use Carp qw(carp croak);
  5. =head1 DESCRIPTION
  6. Selenium Webdriver represents all the HTML elements as WebElements.
  7. This module provides a mechanism to represent them as objects &
  8. perform various actions on the related elements. This module should
  9. not be instantiated directly by the end user. Selenium::Remote::Driver
  10. instantiates this module when required. Typically, the find_element
  11. method in Selenium::Remote::Driver returns this object on which
  12. various element related operations can be carried out.
  13. What is probably most useful on this page is the list of methods below
  14. that you can perform on an element once you've found one and S::R::D
  15. has made an instance of this for you.
  16. =attr id
  17. Required: Pass in a string representing the ID of the object. The
  18. string should be obtained from the response object of making one of
  19. the C<find_element> calls from L</Selenium::Remote::Driver>.
  20. The attribute is also set up to handle spec compliant element response
  21. objects via its `coerce` such that any of the following will work and
  22. are all equivalent:
  23. my $old_elem = Selenium::Remote::WebElement->new(
  24. id => 1,
  25. driver => $driver
  26. );
  27. my $new_remote_elem = Selenium::Remote::WebElement->new(
  28. id => { ELEMENT => 1 },
  29. driver => $driver
  30. );
  31. my $new_spec_elem = Selenium::Remote::WebElement->new(
  32. id => { 'element-6066-11e4-a52e-4f735466cecf' => 1 },
  33. driver => $driver
  34. );
  35. and then after instantiation, all three would give the following for
  36. `id`:
  37. print $elem->id; # prints 1
  38. Again, for typical usage of S::R::D and this module, none of this
  39. matters and it should Just Work without you having to worry about it
  40. at all. For further reading, the L<W3C
  41. spec|https://www.w3.org/TR/webdriver/#elements> strictly dictates the
  42. exact behavior.
  43. =cut
  44. has 'id' => (
  45. is => 'ro',
  46. required => 1,
  47. coerce => sub {
  48. my ($value) = @_;
  49. if (ref($value) eq 'HASH') {
  50. if (exists $value->{ELEMENT}) {
  51. # The JSONWireProtocol web element object looks like
  52. #
  53. # { "ELEMENT": $INTEGER_ID }
  54. return $value->{ELEMENT};
  55. }
  56. elsif (exists $value->{'element-6066-11e4-a52e-4f735466cecf'}) {
  57. # but the WebDriver spec web element uses a magic
  58. # string. See the spec for more information:
  59. #
  60. # https://www.w3.org/TR/webdriver/#elements
  61. return $value->{'element-6066-11e4-a52e-4f735466cecf'};
  62. }
  63. else {
  64. croak 'When passing in an object to the WebElement id attribute, it must have at least one of the ELEMENT or element-6066-11e4-a52e-4f735466cecf keys.';
  65. }
  66. }
  67. else {
  68. return $value;
  69. }
  70. }
  71. );
  72. =attr driver
  73. Required: Pass in a Selenium::Remote::Driver instance or one of its
  74. subclasses. The WebElement needs the appropriate Driver session to
  75. execute its commands properly.
  76. =cut
  77. has 'driver' => (
  78. is => 'ro',
  79. required => 1,
  80. handles => [qw(_execute_command)],
  81. );
  82. =head1 FUNCTIONS
  83. =head2 click
  84. Description:
  85. Click the element.
  86. Usage:
  87. $elem->click();
  88. =cut
  89. sub click {
  90. my ($self) = @_;
  91. my $res = { 'command' => 'clickElement', 'id' => $self->id };
  92. return $self->_execute_command($res);
  93. }
  94. =head2 submit
  95. Description:
  96. Submit a FORM element. The submit command may also be applied to any element
  97. that is a descendant of a FORM element.
  98. Usage:
  99. $elem->submit();
  100. =cut
  101. sub submit {
  102. my ($self) = @_;
  103. my $res = { 'command' => 'submitElement', 'id' => $self->id };
  104. return $self->_execute_command($res);
  105. }
  106. =head2 send_keys
  107. Description:
  108. Send a sequence of key strokes to an element. If you want to send specific
  109. Keyboard events, then use the WDKeys module along with theis method. See e.g.
  110. for reference
  111. Input: 1
  112. Required:
  113. {ARRAY | STRING} - Array of strings or a string.
  114. Usage:
  115. $elem->send_keys('abcd', 'efg');
  116. $elem->send_keys('hijk');
  117. or
  118. # include the WDKeys module
  119. use Selenium::Remote::WDKeys;
  120. .
  121. .
  122. $elem->send_keys(KEYS->{'space'}, KEYS->{'enter'});
  123. =cut
  124. sub send_keys {
  125. my ( $self, @strings ) = @_;
  126. croak "no keys to send" unless scalar @strings >= 1;
  127. my $res = { 'command' => 'sendKeysToElement', 'id' => $self->id };
  128. map { $_ .= "" } @strings;
  129. my $params = {
  130. 'value' => \@strings,
  131. };
  132. return $self->_execute_command( $res, $params );
  133. }
  134. =head2 is_selected
  135. Description:
  136. Determine if an OPTION element, or an INPUT element of type checkbox or
  137. radiobutton is currently selected.
  138. Output:
  139. BOOLEAN - whether the element is selected
  140. Usage:
  141. $elem->is_selected();
  142. =cut
  143. sub is_selected {
  144. my ($self) = @_;
  145. my $res = { 'command' => 'isElementSelected', 'id' => $self->id };
  146. return $self->_execute_command($res);
  147. }
  148. =head2 set_selected
  149. Description:
  150. Select an OPTION element, or an INPUT element of type checkbox or radiobutton.
  151. Usage:
  152. $elem->set_selected();
  153. Note: DEPRECATED -- use click instead
  154. =cut
  155. sub set_selected {
  156. my ($self) = @_;
  157. my $res = { 'command' => 'setElementSelected', 'id' => $self->id };
  158. return $self->_execute_command($res);
  159. }
  160. =head2 toggle
  161. Description:
  162. Toggle whether an OPTION element, or an INPUT element of type checkbox or
  163. radiobutton is currently selected.
  164. Output:
  165. BOOLEAN - Whether the element is selected after toggling its state.
  166. Usage:
  167. $elem->toggle();
  168. Note: DEPRECATED -- use click instead
  169. =cut
  170. sub toggle {
  171. my ($self) = @_;
  172. my $res = { 'command' => 'toggleElement', 'id' => $self->id };
  173. return $self->_execute_command($res);
  174. }
  175. =head2 is_enabled
  176. Description:
  177. Determine if an element is currently enabled.
  178. Output:
  179. BOOLEAN - Whether the element is enabled.
  180. Usage:
  181. $elem->is_enabled();
  182. =cut
  183. sub is_enabled {
  184. my ($self) = @_;
  185. my $res = { 'command' => 'isElementEnabled', 'id' => $self->id };
  186. return $self->_execute_command($res);
  187. }
  188. =head2 get_element_location
  189. Description:
  190. Determine an element's location on the page. The point (0, 0) refers to the
  191. upper-left corner of the page.
  192. Output:
  193. HASH - The X and Y coordinates for the element on the page.
  194. Usage:
  195. $elem->get_element_location();
  196. =cut
  197. sub get_element_location {
  198. my ($self) = @_;
  199. my $res = { 'command' => 'getElementLocation', 'id' => $self->id };
  200. return $self->_execute_command($res);
  201. }
  202. =head2 get_element_location_in_view
  203. Description:
  204. Determine an element's location on the screen once it has been scrolled
  205. into view.
  206. Note: This is considered an internal command and should only be used to
  207. determine an element's location for correctly generating native events.
  208. Output:
  209. {x:number, y:number} The X and Y coordinates for the element on the page.
  210. Usage:
  211. $elem->get_element_location_in_view();
  212. =cut
  213. sub get_element_location_in_view {
  214. my ($self) = @_;
  215. my $res = { 'command' => 'getElementLocationInView', 'id' => $self->id };
  216. return $self->_execute_command($res);
  217. }
  218. =head2 get_tag_name
  219. Description:
  220. Query for an element's tag name.
  221. Output:
  222. STRING - The element's tag name, as a lowercase string.
  223. Usage:
  224. $elem->get_tag_name();
  225. =cut
  226. sub get_tag_name {
  227. my ($self) = @_;
  228. my $res = { 'command' => 'getElementTagName', 'id' => $self->id };
  229. return $self->_execute_command($res);
  230. }
  231. =head2 clear
  232. Description:
  233. Clear a TEXTAREA or text INPUT element's value.
  234. Usage:
  235. $elem->clear();
  236. =cut
  237. sub clear {
  238. my ($self) = @_;
  239. my $res = { 'command' => 'clearElement', 'id' => $self->id };
  240. return $self->_execute_command($res);
  241. }
  242. =head2 get_attribute
  243. Description:
  244. Get the value of an element's attribute.
  245. Input: 1
  246. Required:
  247. STRING - name of the attribute of the element
  248. Output:
  249. {STRING | NULL} The value of the attribute, or null if it is not set on the element.
  250. Usage:
  251. $elem->get_attribute('name');
  252. =cut
  253. sub get_attribute {
  254. my ( $self, $attr_name ) = @_;
  255. if ( not defined $attr_name ) {
  256. croak 'Attribute name not provided';
  257. }
  258. my $res = {
  259. 'command' => 'getElementAttribute',
  260. 'id' => $self->id,
  261. 'name' => $attr_name,
  262. };
  263. return $self->_execute_command($res);
  264. }
  265. =head2 get_value
  266. Description:
  267. Query for the value of an element, as determined by its value attribute.
  268. Output:
  269. {STRING | NULL} The element's value, or null if it doesn't have a value attribute.
  270. Usage:
  271. $elem->get_value();
  272. =cut
  273. sub get_value {
  274. my ($self) = @_;
  275. return $self->get_attribute('value');
  276. }
  277. =head2 is_displayed
  278. Description:
  279. Determine if an element is currently displayed.
  280. Output:
  281. BOOLEAN - Whether the element is displayed.
  282. Usage:
  283. $elem->is_displayed();
  284. =cut
  285. sub is_displayed {
  286. my ($self) = @_;
  287. my $res = { 'command' => 'isElementDisplayed', 'id' => $self->id };
  288. return $self->_execute_command($res);
  289. }
  290. =head2 is_hidden
  291. Description:
  292. Determine if an element is currently hidden.
  293. Output:
  294. BOOLEAN - Whether the element is hidden.
  295. Usage:
  296. $elem->is_hidden();
  297. =cut
  298. sub is_hidden {
  299. my ($self) = @_;
  300. return ! $self->is_displayed();
  301. }
  302. =head2 drag
  303. Description:
  304. Drag and drop an element. The distance to drag an element should be
  305. specified relative to the upper-left corner of the page and it starts at 0,0
  306. Input: 2
  307. Required:
  308. NUMBER - X axis distance in pixels
  309. NUMBER - Y axis distance in pixels
  310. Usage:
  311. $elem->drag(216,158);
  312. Note: DEPRECATED - drag is no longer available in the
  313. JSONWireProtocol. We are working on an ActionsChains implementation,
  314. but drag and drop doesn't currently work on the Webdriver side for
  315. HTML5 pages. For reference, see:
  316. http://elementalselenium.com/tips/39-drag-and-drop
  317. https://gist.github.com/rcorreia/2362544
  318. Check out the mouse_move_to_location, button_down, and button_up
  319. functions on Selenium::Remote::Driver.
  320. https://metacpan.org/pod/Selenium::Remote::Driver#mouse_move_to_location
  321. https://metacpan.org/pod/Selenium::Remote::Driver#button_down
  322. https://metacpan.org/pod/Selenium::Remote::Driver#button_up
  323. =cut
  324. sub drag {
  325. carp 'drag is no longer available in the JSONWireProtocol.';
  326. }
  327. =head2 get_size
  328. Description:
  329. Determine an element's size in pixels. The size will be returned with width
  330. and height properties.
  331. Output:
  332. HASH - The width and height of the element, in pixels.
  333. Usage:
  334. $elem->get_size();
  335. =cut
  336. sub get_size {
  337. my ($self) = @_;
  338. my $res = { 'command' => 'getElementSize', 'id' => $self->id };
  339. return $self->_execute_command($res);
  340. }
  341. =head2 get_text
  342. Description:
  343. Get the innerText of the element.
  344. Output:
  345. STRING - innerText of an element
  346. Usage:
  347. $elem->get_text();
  348. =cut
  349. sub get_text {
  350. my ($self) = @_;
  351. my $res = { 'command' => 'getElementText', 'id' => $self->id };
  352. return $self->_execute_command($res);
  353. }
  354. =head2 get_css_attribute
  355. Description:
  356. Query the value of an element's computed CSS property. The CSS property to
  357. query should be specified using the CSS property name, not the JavaScript
  358. property name (e.g. background-color instead of backgroundColor).
  359. Input: 1
  360. Required:
  361. STRING - name of the css-attribute
  362. Output:
  363. STRING - Value of the css attribute
  364. Usage:
  365. $elem->get_css_attribute('background-color');
  366. =cut
  367. sub get_css_attribute {
  368. my ( $self, $attr_name ) = @_;
  369. if ( not defined $attr_name ) {
  370. croak 'CSS attribute name not provided';
  371. }
  372. my $res = {
  373. 'command' => 'getElementValueOfCssProperty',
  374. 'id' => $self->id,
  375. 'property_name' => $attr_name,
  376. };
  377. return $self->_execute_command($res);
  378. }
  379. =head2 describe
  380. Description:
  381. Describe the identified element
  382. Usage:
  383. $elem->describe();
  384. Note: DEPRECATED as of 2.42.2 -- use get_text, get_value, is_displayed, or
  385. whatever appropriate WebElement function you need instead
  386. =cut
  387. sub describe {
  388. my ($self) = @_;
  389. my $res = { 'command' => 'describeElement', 'id' => $self->id };
  390. return $self->_execute_command($res);
  391. }
  392. 1;