WebElement.pm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. package Selenium::Remote::WebElement;
  2. # ABSTRACT: Representation of an HTML Element used by Selenium Remote Driver
  3. use strict;
  4. use warnings;
  5. use Moo;
  6. use Carp qw(carp croak);
  7. =head1 DESCRIPTION
  8. Selenium Webdriver represents all the HTML elements as WebElements.
  9. This module provides a mechanism to represent them as objects &
  10. perform various actions on the related elements. This module should
  11. not be instantiated directly by the end user. Selenium::Remote::Driver
  12. instantiates this module when required. Typically, the find_element
  13. method in Selenium::Remote::Driver returns this object on which
  14. various element related operations can be carried out.
  15. What is probably most useful on this page is the list of methods below
  16. that you can perform on an element once you've found one and S::R::D
  17. has made an instance of this for you.
  18. =head1 CONSTRUCTOR
  19. =head2 new
  20. =over 4
  21. =item B<id>
  22. Required: Pass in a string representing the ID of the object. The
  23. string should be obtained from the response object of making one of
  24. the C<find_element> calls from L<Selenium::Remote::Driver>.
  25. The attribute is also set up to handle spec compliant element response
  26. objects via its `coerce` such that any of the following will work and
  27. are all equivalent:
  28. my $old_elem = Selenium::Remote::WebElement->new(
  29. id => 1,
  30. driver => $driver
  31. );
  32. my $new_remote_elem = Selenium::Remote::WebElement->new(
  33. id => { ELEMENT => 1 },
  34. driver => $driver
  35. );
  36. my $new_spec_elem = Selenium::Remote::WebElement->new(
  37. id => { 'element-6066-11e4-a52e-4f735466cecf' => 1 },
  38. driver => $driver
  39. );
  40. and then after instantiation, all three would give the following for
  41. `id`:
  42. print $elem->id; # prints 1
  43. =item B<driver>
  44. Required: Pass in a Selenium::Remote::Driver instance or one of its
  45. subclasses. The WebElement needs the appropriate Driver session to
  46. execute its commands properly.
  47. =back
  48. For typical usage of S::R::D and this module, none of this
  49. matters and it should Just Work without you having to worry about it
  50. at all. For further reading, the L<W3C
  51. spec|https://www.w3.org/TR/webdriver/#elements> strictly dictates the
  52. exact behavior.
  53. =cut
  54. has 'id' => (
  55. is => 'ro',
  56. required => 1,
  57. coerce => sub {
  58. my ($value) = @_;
  59. if (ref($value) eq 'HASH') {
  60. if (exists $value->{ELEMENT}) {
  61. # The JSONWireProtocol web element object looks like
  62. #
  63. # { "ELEMENT": $INTEGER_ID }
  64. return $value->{ELEMENT};
  65. }
  66. elsif (exists $value->{'element-6066-11e4-a52e-4f735466cecf'}) {
  67. # but the WebDriver spec web element uses a magic
  68. # string. See the spec for more information:
  69. #
  70. # https://www.w3.org/TR/webdriver/#elements
  71. return $value->{'element-6066-11e4-a52e-4f735466cecf'};
  72. }
  73. else {
  74. 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.';
  75. }
  76. }
  77. else {
  78. return $value;
  79. }
  80. }
  81. );
  82. has 'driver' => (
  83. is => 'ro',
  84. required => 1,
  85. handles => [qw(_execute_command)],
  86. );
  87. =head1 FUNCTIONS
  88. =head2 click
  89. Description:
  90. Click the element.
  91. Usage:
  92. $elem->click();
  93. =cut
  94. sub click {
  95. my ($self) = @_;
  96. my $res = { 'command' => 'clickElement', 'id' => $self->id };
  97. return $self->_execute_command($res);
  98. }
  99. =head2 submit
  100. Description:
  101. Submit a FORM element. The submit command may also be applied to any element
  102. that is a descendant of a FORM element.
  103. Usage:
  104. $elem->submit();
  105. =cut
  106. sub submit {
  107. my ($self) = @_;
  108. return $self->driver->execute_script("return arguments[0].submit();", {'element-6066-11e4-a52e-4f735466cecf'=> $self->{id}} ) if $self->driver->{is_wd3} && !(grep { $self->driver->browser_name eq $_ } qw{chrome MicrosoftEdge});
  109. my $res = { 'command' => 'submitElement', 'id' => $self->id };
  110. return $self->_execute_command($res);
  111. }
  112. =head2 send_keys
  113. Description:
  114. Send a sequence of key strokes to an element. If you want to send specific
  115. Keyboard events, then use the WDKeys module along with theis method. See e.g.
  116. for reference
  117. Input: 1
  118. Required:
  119. {ARRAY | STRING} - Array of strings or a string.
  120. Usage:
  121. $elem->send_keys('abcd', 'efg');
  122. $elem->send_keys('hijk');
  123. or
  124. # include the WDKeys module
  125. use Selenium::Remote::WDKeys;
  126. .
  127. .
  128. $elem->send_keys(KEYS->{'space'}, KEYS->{'enter'});
  129. =cut
  130. sub send_keys {
  131. my ( $self, @strings ) = @_;
  132. croak "no keys to send" unless scalar @strings >= 1;
  133. my $res = { 'command' => 'sendKeysToElement', 'id' => $self->id };
  134. # We need to send an array of single characters to be WebDriver
  135. # spec compatible. That is, for @strings = ('hel', 'lo'), the
  136. # corresponding value must be ('h', 'e', 'l', 'l', 'o' ). This
  137. # format conforms with the Spec AND works with the Selenium
  138. # standalone server.
  139. my $strings = join('', map { $_."" } @strings);
  140. my $params = {
  141. 'value' => [ split('', $strings) ],
  142. text => $strings,
  143. };
  144. return $self->_execute_command( $res, $params );
  145. }
  146. =head2 is_selected
  147. Description:
  148. Determine if an OPTION element, or an INPUT element of type checkbox or
  149. radiobutton is currently selected.
  150. Output:
  151. BOOLEAN - whether the element is selected
  152. Usage:
  153. $elem->is_selected();
  154. =cut
  155. sub is_selected {
  156. my ($self) = @_;
  157. return $self->get_property('checked') if $self->driver->{is_wd3} && !(grep { $self->driver->browser_name eq $_ } qw{chrome MicrosoftEdge});
  158. my $res = { 'command' => 'isElementSelected', 'id' => $self->id };
  159. return $self->_execute_command($res);
  160. }
  161. =head2 set_selected
  162. Description:
  163. Select an OPTION element, or an INPUT element of type checkbox or radiobutton.
  164. Forces selected=1 on the element..
  165. Usage:
  166. $elem->set_selected();
  167. =cut
  168. sub set_selected {
  169. my ($self) = @_;
  170. if ($self->driver->{is_wd3}) {
  171. return if $self->is_selected();
  172. return $self->click();
  173. }
  174. my $res = { 'command' => 'setElementSelected', 'id' => $self->id };
  175. return $self->_execute_command($res);
  176. }
  177. =head2 toggle
  178. Description:
  179. Toggle whether an OPTION element, or an INPUT element of type checkbox or
  180. radiobutton is currently selected.
  181. Output:
  182. BOOLEAN - Whether the element is selected after toggling its state.
  183. Usage:
  184. $elem->toggle();
  185. =cut
  186. sub toggle {
  187. my ($self) = @_;
  188. if ($self->driver->{is_wd3}) {
  189. return $self->click() unless $self->is_selected();
  190. return $self->driver->execute_script(qq/ if (arguments[0].checked) { arguments[0].checked = 0 }; return arguments[0].checked; /, {'element-6066-11e4-a52e-4f735466cecf'=> $self->{id}});
  191. }
  192. my $res = { 'command' => 'toggleElement', 'id' => $self->id };
  193. return $self->_execute_command($res);
  194. }
  195. =head2 is_enabled
  196. Description:
  197. Determine if an element is currently enabled.
  198. Output:
  199. BOOLEAN - Whether the element is enabled.
  200. Usage:
  201. $elem->is_enabled();
  202. =cut
  203. sub is_enabled {
  204. my ($self) = @_;
  205. return $self->get_property('enabled') ? 1 : 0 if $self->driver->{is_wd3} && !(grep { $self->driver->browser_name eq $_ } qw{chrome MicrosoftEdge});
  206. my $res = { 'command' => 'isElementEnabled', 'id' => $self->id };
  207. return $self->_execute_command($res);
  208. }
  209. =head2 get_element_location
  210. Description:
  211. Determine an element's location on the page. The point (0, 0) refers to the
  212. upper-left corner of the page.
  213. Compatibility:
  214. On WebDriver 3 enabled servers, this is an alias for get_element_rect().
  215. Output:
  216. HASH - The X and Y coordinates for the element on the page.
  217. Usage:
  218. $elem->get_element_location();
  219. This method is DEPRECATED on webdriver3 enabled servers.
  220. =cut
  221. sub get_element_location {
  222. my ($self) = @_;
  223. if ($self->driver->{is_wd3} && !(grep { $self->driver->browser_name eq $_ } qw{chrome MicrosoftEdge})) {
  224. my $data = $self->get_element_rect();
  225. delete $data->{height};
  226. delete $data->{width};
  227. return $data;
  228. }
  229. my $res = { 'command' => 'getElementLocation', 'id' => $self->id };
  230. return $self->_execute_command($res);
  231. }
  232. =head2 get_size
  233. Description:
  234. Determine an element's size in pixels. The size will be returned with width
  235. and height properties.
  236. Compatibility:
  237. On WebDriver 3 enabled servers, this is an alias for get_element_rect().
  238. Output:
  239. HASH - The width and height of the element, in pixels.
  240. Usage:
  241. $elem->get_size();
  242. This method is DEPRECATED on webdriver3 enabled servers.
  243. =cut
  244. sub get_size {
  245. my ($self) = @_;
  246. if ($self->driver->{is_wd3} && !(grep { $self->driver->browser_name eq $_ } qw{chrome MicrosoftEdge})) {
  247. my $data = $self->get_element_rect();
  248. delete $data->{x};
  249. delete $data->{y};
  250. return $data;
  251. }
  252. my $res = { 'command' => 'getElementSize', 'id' => $self->id };
  253. return $self->_execute_command($res);
  254. }
  255. =head2 get_element_rect
  256. Get the element's size AND location in a hash.
  257. Example Output:
  258. { x => 0, y => 0, height => 10, width => 10 }
  259. =cut
  260. sub get_element_rect {
  261. my ($self) = @_;
  262. my $res = { 'command' => 'getElementRect', 'id' => $self->id };
  263. return $self->_execute_command($res);
  264. }
  265. =head2 get_element_location_in_view
  266. Description:
  267. Determine an element's location on the screen once it has been scrolled
  268. into view.
  269. Note: This is considered an internal command and should only be used to
  270. determine an element's location for correctly generating native events.
  271. Compatibility:
  272. Not available on WebDriver3 enabled selenium servers.
  273. Output:
  274. {x:number, y:number} The X and Y coordinates for the element on the page.
  275. Usage:
  276. $elem->get_element_location_in_view();
  277. =cut
  278. sub get_element_location_in_view {
  279. my ($self) = @_;
  280. return $self->driver->execute_script(qq{
  281. arguments[0].scrollIntoView();
  282. var pos = arguments[0].getBoundingClientRect();
  283. return {y:pos.top,x:pos.left};
  284. }, {'element-6066-11e4-a52e-4f735466cecf'=> $self->{id}} ) if $self->driver->{is_wd3} && grep { $self->driver->browser_name eq $_ } ('firefox','internet explorer');
  285. my $res = { 'command' => 'getElementLocationInView', 'id' => $self->id };
  286. return $self->_execute_command($res);
  287. }
  288. =head2 get_tag_name
  289. Description:
  290. Query for an element's tag name.
  291. Output:
  292. STRING - The element's tag name, as a lowercase string.
  293. Usage:
  294. $elem->get_tag_name();
  295. =cut
  296. sub get_tag_name {
  297. my ($self) = @_;
  298. my $res = { 'command' => 'getElementTagName', 'id' => $self->id };
  299. return $self->_execute_command($res);
  300. }
  301. =head2 clear
  302. Description:
  303. Clear a TEXTAREA or text INPUT element's value.
  304. Usage:
  305. $elem->clear();
  306. =cut
  307. sub clear {
  308. my ($self) = @_;
  309. my $res = { 'command' => 'clearElement', 'id' => $self->id };
  310. return $self->_execute_command($res);
  311. }
  312. =head2 get_attribute
  313. Description:
  314. Get the value of an element's attribute.
  315. Compatibility:
  316. In older webDriver, this actually got the value of an element's property.
  317. If you want to get the initial condition (e.g. the values in the tag hardcoded in HTML), pass 1 as the second argument.
  318. This can only done on WebDriver 3 enabled servers.
  319. Input: 2
  320. Required:
  321. STRING - name of the attribute of the element
  322. Optional:
  323. BOOLEAN - "I really mean that I want the initial condition, quit being so compatible!!!"
  324. Output:
  325. {STRING | NULL} The value of the attribute, or null if it is not set on the element.
  326. Usage:
  327. $elem->get_attribute('name',1);
  328. =cut
  329. sub get_attribute {
  330. my ( $self, $attr_name, $no_i_really_mean_it ) = @_;
  331. if ( not defined $attr_name ) {
  332. croak 'Attribute name not provided';
  333. }
  334. return $self->get_property($attr_name) if $self->driver->{is_wd3} && !(grep { $self->driver->browser_name eq $_ } qw{chrome MicrosoftEdge}) && !$no_i_really_mean_it;
  335. my $res = {
  336. 'command' => 'getElementAttribute',
  337. 'id' => $self->id,
  338. 'name' => $attr_name,
  339. };
  340. return $self->_execute_command($res);
  341. }
  342. =head2 get_property
  343. Gets the C<Current Value> of an element's attribute.
  344. Takes a named property as an argument.
  345. Only available on WebDriver 3 enabled servers.
  346. =cut
  347. sub get_property {
  348. my ($self,$prop) = @_;
  349. return $self->get_attribute($prop) if $self->driver->{is_wd3} && (grep { $self->driver->browser_name eq $_ } qw{chrome MicrosoftEdge});
  350. my $res = { 'command' => 'getElementProperty', id => $self->id, name => $prop };
  351. return $self->_execute_command($res);
  352. }
  353. =head2 get_value
  354. Description:
  355. Query for the value of an element, as determined by its value attribute.
  356. Output:
  357. {STRING | NULL} The element's value, or null if it doesn't have a value attribute.
  358. Usage:
  359. $elem->get_value();
  360. =cut
  361. sub get_value {
  362. my ($self) = @_;
  363. return $self->get_attribute('value');
  364. }
  365. =head2 get_style
  366. =head2 is_displayed
  367. Description:
  368. Determine if an element is currently displayed.
  369. Note: This does *not* tell you an element's 'visibility' property; as it still takes up space in the DOM and is therefore considered 'displayed'.
  370. Output:
  371. BOOLEAN - Whether the element is displayed.
  372. Usage:
  373. $elem->is_displayed();
  374. =cut
  375. sub is_displayed {
  376. my ($self) = @_;
  377. if ($self->driver->{is_wd3} && !(grep { $self->driver->browser_name eq $_ } qw{chrome MicrosoftEdge})) {
  378. return 0 if $self->get_tag_name() eq 'input' && $self->get_property('type') eq 'hidden'; #hidden type inputs
  379. return int($self->get_css_attribute('display') ne 'none');
  380. }
  381. my $res = { 'command' => 'isElementDisplayed', 'id' => $self->id };
  382. return $self->_execute_command($res);
  383. }
  384. =head2 is_hidden
  385. Description:
  386. Determine if an element is currently hidden.
  387. Output:
  388. BOOLEAN - Whether the element is hidden.
  389. Usage:
  390. $elem->is_hidden();
  391. =cut
  392. sub is_hidden {
  393. my ($self) = @_;
  394. return ! $self->is_displayed();
  395. }
  396. =head2 drag
  397. Alias for Selenium::ActionChains::drag_and_drop().
  398. Provide element you wish to drag to as argument.
  399. my $target = $driver->find_element('receptacle','id');
  400. my $subject = $driver->find_element('thingy','id');
  401. $subject->drag($target);
  402. =cut
  403. sub drag {
  404. my ($self,$target) = @_;
  405. require Selenium::ActionChains;
  406. my $chain = Selenium::ActionChians->new( driver => $self->driver );
  407. return $chain->drag_and_drop($self,$target)->perform();
  408. }
  409. =head2 get_text
  410. Description:
  411. Get the innerText of the element.
  412. Output:
  413. STRING - innerText of an element
  414. Usage:
  415. $elem->get_text();
  416. =cut
  417. sub get_text {
  418. my ($self) = @_;
  419. my $res = { 'command' => 'getElementText', 'id' => $self->id };
  420. return $self->_execute_command($res);
  421. }
  422. =head2 get_css_attribute
  423. Description:
  424. Query the value of an element's computed CSS property. The CSS property to
  425. query should be specified using the CSS property name, not the JavaScript
  426. property name (e.g. background-color instead of backgroundColor).
  427. Input: 1
  428. Required:
  429. STRING - name of the css-attribute
  430. Output:
  431. STRING - Value of the css attribute
  432. Usage:
  433. $elem->get_css_attribute('background-color');
  434. =cut
  435. sub get_css_attribute {
  436. my ( $self, $attr_name ) = @_;
  437. if ( not defined $attr_name ) {
  438. croak 'CSS attribute name not provided';
  439. }
  440. my $res = {
  441. 'command' => 'getElementValueOfCssProperty',
  442. 'id' => $self->id,
  443. 'property_name' => $attr_name,
  444. };
  445. return $self->_execute_command($res);
  446. }
  447. =head2 describe
  448. Description:
  449. Describe the identified element
  450. Usage:
  451. $elem->describe();
  452. Note: DEPRECATED as of 2.42.2 -- use get_text, get_value, is_displayed, or
  453. whatever appropriate WebElement function you need instead
  454. Entirely unsupported on WebDriver 3 enabled servers.
  455. =cut
  456. sub describe {
  457. my ($self) = @_;
  458. my $res = { 'command' => 'describeElement', 'id' => $self->id };
  459. return $self->_execute_command($res);
  460. }
  461. =head2 screenshot
  462. Description:
  463. Get a screenshot of the visible region that is a subset of the element's bounding box as a base64 encoded image.
  464. Compatibility:
  465. Only available on Webdriver3 enabled selenium servers.
  466. Input (optional):
  467. $scroll_into_view - BOOLEAN default true. If false, will not scroll the element into the viewport first.
  468. Failing to do so may result in an image being cropped partially or entirely.
  469. Output:
  470. STRING - base64 encoded image
  471. Usage:
  472. print $element->screenshot();
  473. To conveniently write the screenshot to a file, see L</capture_screenshot>.
  474. =cut
  475. sub screenshot {
  476. my ($self, $scroll) = @_;
  477. $scroll //= 1;
  478. my $res = { 'command' => 'elementScreenshot', id => $self->id };
  479. my $input = {scroll => int($scroll) };
  480. return $self->_execute_command($res, $input);
  481. }
  482. =head2 capture_screenshot
  483. Description:
  484. Capture a screenshot of said element and save as a PNG to provided file name.
  485. Compatibility:
  486. Only available on Webdriver3 enabled selenium servers.
  487. Input (optional):
  488. $scroll_into_view - BOOLEAN default true. If false, will not scroll the element into the viewport first.
  489. Failing to do so may result in an image being cropped partially or entirely.
  490. Output:
  491. TRUE - (Screenshot is written to file)
  492. Usage:
  493. $element->capture_screenshot($filename);
  494. =cut
  495. sub capture_screenshot {
  496. my ( $self, $filename, $scroll ) = @_;
  497. croak '$filename is required' unless $filename;
  498. open( my $fh, '>', $filename );
  499. binmode $fh;
  500. print $fh MIME::Base64::decode_base64( $self->screenshot($scroll) );
  501. CORE::close $fh;
  502. return 1;
  503. }
  504. 1;