1
0

WebElement.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. my $res = { 'command' => 'submitElement', 'id' => $self->id };
  109. return $self->_execute_command($res);
  110. }
  111. =head2 send_keys
  112. Description:
  113. Send a sequence of key strokes to an element. If you want to send specific
  114. Keyboard events, then use the WDKeys module along with theis method. See e.g.
  115. for reference
  116. Input: 1
  117. Required:
  118. {ARRAY | STRING} - Array of strings or a string.
  119. Usage:
  120. $elem->send_keys('abcd', 'efg');
  121. $elem->send_keys('hijk');
  122. or
  123. # include the WDKeys module
  124. use Selenium::Remote::WDKeys;
  125. .
  126. .
  127. $elem->send_keys(KEYS->{'space'}, KEYS->{'enter'});
  128. =cut
  129. sub send_keys {
  130. my ( $self, @strings ) = @_;
  131. croak "no keys to send" unless scalar @strings >= 1;
  132. my $res = { 'command' => 'sendKeysToElement', 'id' => $self->id };
  133. # We need to send an array of single characters to be WebDriver
  134. # spec compatible. That is, for @strings = ('hel', 'lo'), the
  135. # corresponding value must be ('h', 'e', 'l', 'l', 'o' ). This
  136. # format conforms with the Spec AND works with the Selenium
  137. # standalone server.
  138. my $strings = join('', map { $_."" } @strings);
  139. my $params = {
  140. 'value' => [ split('', $strings) ],
  141. text => $strings,
  142. };
  143. return $self->_execute_command( $res, $params );
  144. }
  145. =head2 is_selected
  146. Description:
  147. Determine if an OPTION element, or an INPUT element of type checkbox or
  148. radiobutton is currently selected.
  149. Output:
  150. BOOLEAN - whether the element is selected
  151. Usage:
  152. $elem->is_selected();
  153. =cut
  154. sub is_selected {
  155. my ($self) = @_;
  156. my $res = { 'command' => 'isElementSelected', 'id' => $self->id };
  157. return $self->_execute_command($res);
  158. }
  159. =head2 set_selected
  160. Description:
  161. Select an OPTION element, or an INPUT element of type checkbox or radiobutton.
  162. Usage:
  163. $elem->set_selected();
  164. Note: DEPRECATED -- use click instead
  165. =cut
  166. sub set_selected {
  167. my ($self) = @_;
  168. my $res = { 'command' => 'setElementSelected', 'id' => $self->id };
  169. return $self->_execute_command($res);
  170. }
  171. =head2 toggle
  172. Description:
  173. Toggle whether an OPTION element, or an INPUT element of type checkbox or
  174. radiobutton is currently selected.
  175. Output:
  176. BOOLEAN - Whether the element is selected after toggling its state.
  177. Usage:
  178. $elem->toggle();
  179. Note: DEPRECATED -- use click instead
  180. =cut
  181. sub toggle {
  182. my ($self) = @_;
  183. my $res = { 'command' => 'toggleElement', 'id' => $self->id };
  184. return $self->_execute_command($res);
  185. }
  186. =head2 is_enabled
  187. Description:
  188. Determine if an element is currently enabled.
  189. Output:
  190. BOOLEAN - Whether the element is enabled.
  191. Usage:
  192. $elem->is_enabled();
  193. =cut
  194. sub is_enabled {
  195. my ($self) = @_;
  196. my $res = { 'command' => 'isElementEnabled', 'id' => $self->id };
  197. return $self->_execute_command($res);
  198. }
  199. =head2 get_element_location
  200. Description:
  201. Determine an element's location on the page. The point (0, 0) refers to the
  202. upper-left corner of the page.
  203. Output:
  204. HASH - The X and Y coordinates for the element on the page.
  205. Usage:
  206. $elem->get_element_location();
  207. =cut
  208. sub get_element_location {
  209. my ($self) = @_;
  210. my $res = { 'command' => 'getElementLocation', 'id' => $self->id };
  211. return $self->_execute_command($res);
  212. }
  213. =head2 get_element_location_in_view
  214. Description:
  215. Determine an element's location on the screen once it has been scrolled
  216. into view.
  217. Note: This is considered an internal command and should only be used to
  218. determine an element's location for correctly generating native events.
  219. Output:
  220. {x:number, y:number} The X and Y coordinates for the element on the page.
  221. Usage:
  222. $elem->get_element_location_in_view();
  223. =cut
  224. sub get_element_location_in_view {
  225. my ($self) = @_;
  226. my $res = { 'command' => 'getElementLocationInView', 'id' => $self->id };
  227. return $self->_execute_command($res);
  228. }
  229. =head2 get_tag_name
  230. Description:
  231. Query for an element's tag name.
  232. Output:
  233. STRING - The element's tag name, as a lowercase string.
  234. Usage:
  235. $elem->get_tag_name();
  236. =cut
  237. sub get_tag_name {
  238. my ($self) = @_;
  239. my $res = { 'command' => 'getElementTagName', 'id' => $self->id };
  240. return $self->_execute_command($res);
  241. }
  242. =head2 clear
  243. Description:
  244. Clear a TEXTAREA or text INPUT element's value.
  245. Usage:
  246. $elem->clear();
  247. =cut
  248. sub clear {
  249. my ($self) = @_;
  250. my $res = { 'command' => 'clearElement', 'id' => $self->id };
  251. return $self->_execute_command($res);
  252. }
  253. =head2 get_attribute
  254. Description:
  255. Get the value of an element's attribute.
  256. Input: 1
  257. Required:
  258. STRING - name of the attribute of the element
  259. Output:
  260. {STRING | NULL} The value of the attribute, or null if it is not set on the element.
  261. Usage:
  262. $elem->get_attribute('name');
  263. =cut
  264. sub get_attribute {
  265. my ( $self, $attr_name ) = @_;
  266. if ( not defined $attr_name ) {
  267. croak 'Attribute name not provided';
  268. }
  269. my $res = {
  270. 'command' => 'getElementAttribute',
  271. 'id' => $self->id,
  272. 'name' => $attr_name,
  273. };
  274. return $self->_execute_command($res);
  275. }
  276. =head2 get_value
  277. Description:
  278. Query for the value of an element, as determined by its value attribute.
  279. Output:
  280. {STRING | NULL} The element's value, or null if it doesn't have a value attribute.
  281. Usage:
  282. $elem->get_value();
  283. =cut
  284. sub get_value {
  285. my ($self) = @_;
  286. return $self->get_attribute('value');
  287. }
  288. =head2 is_displayed
  289. Description:
  290. Determine if an element is currently displayed.
  291. Output:
  292. BOOLEAN - Whether the element is displayed.
  293. Usage:
  294. $elem->is_displayed();
  295. =cut
  296. sub is_displayed {
  297. my ($self) = @_;
  298. my $res = { 'command' => 'isElementDisplayed', 'id' => $self->id };
  299. return $self->_execute_command($res);
  300. }
  301. =head2 is_hidden
  302. Description:
  303. Determine if an element is currently hidden.
  304. Output:
  305. BOOLEAN - Whether the element is hidden.
  306. Usage:
  307. $elem->is_hidden();
  308. =cut
  309. sub is_hidden {
  310. my ($self) = @_;
  311. return ! $self->is_displayed();
  312. }
  313. =head2 drag
  314. Description:
  315. Drag and drop an element. The distance to drag an element should be
  316. specified relative to the upper-left corner of the page and it starts at 0,0
  317. Input: 2
  318. Required:
  319. NUMBER - X axis distance in pixels
  320. NUMBER - Y axis distance in pixels
  321. Usage:
  322. $elem->drag(216,158);
  323. Note: DEPRECATED - drag is no longer available in the
  324. JSONWireProtocol. We are working on an ActionsChains implementation,
  325. but drag and drop doesn't currently work on the Webdriver side for
  326. HTML5 pages. For reference, see:
  327. http://elementalselenium.com/tips/39-drag-and-drop
  328. https://gist.github.com/rcorreia/2362544
  329. Check out the mouse_move_to_location, button_down, and button_up
  330. functions on Selenium::Remote::Driver.
  331. https://metacpan.org/pod/Selenium::Remote::Driver#mouse_move_to_location
  332. https://metacpan.org/pod/Selenium::Remote::Driver#button_down
  333. https://metacpan.org/pod/Selenium::Remote::Driver#button_up
  334. =cut
  335. sub drag {
  336. carp 'drag is no longer available in the JSONWireProtocol.';
  337. }
  338. =head2 get_size
  339. Description:
  340. Determine an element's size in pixels. The size will be returned with width
  341. and height properties.
  342. Output:
  343. HASH - The width and height of the element, in pixels.
  344. Usage:
  345. $elem->get_size();
  346. =cut
  347. sub get_size {
  348. my ($self) = @_;
  349. my $res = { 'command' => 'getElementSize', 'id' => $self->id };
  350. return $self->_execute_command($res);
  351. }
  352. =head2 get_text
  353. Description:
  354. Get the innerText of the element.
  355. Output:
  356. STRING - innerText of an element
  357. Usage:
  358. $elem->get_text();
  359. =cut
  360. sub get_text {
  361. my ($self) = @_;
  362. my $res = { 'command' => 'getElementText', 'id' => $self->id };
  363. return $self->_execute_command($res);
  364. }
  365. =head2 get_css_attribute
  366. Description:
  367. Query the value of an element's computed CSS property. The CSS property to
  368. query should be specified using the CSS property name, not the JavaScript
  369. property name (e.g. background-color instead of backgroundColor).
  370. Input: 1
  371. Required:
  372. STRING - name of the css-attribute
  373. Output:
  374. STRING - Value of the css attribute
  375. Usage:
  376. $elem->get_css_attribute('background-color');
  377. =cut
  378. sub get_css_attribute {
  379. my ( $self, $attr_name ) = @_;
  380. if ( not defined $attr_name ) {
  381. croak 'CSS attribute name not provided';
  382. }
  383. my $res = {
  384. 'command' => 'getElementValueOfCssProperty',
  385. 'id' => $self->id,
  386. 'property_name' => $attr_name,
  387. };
  388. return $self->_execute_command($res);
  389. }
  390. =head2 describe
  391. Description:
  392. Describe the identified element
  393. Usage:
  394. $elem->describe();
  395. Note: DEPRECATED as of 2.42.2 -- use get_text, get_value, is_displayed, or
  396. whatever appropriate WebElement function you need instead
  397. =cut
  398. sub describe {
  399. my ($self) = @_;
  400. my $res = { 'command' => 'describeElement', 'id' => $self->id };
  401. return $self->_execute_command($res);
  402. }
  403. 1;