WebElement.pm 12 KB

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