1
0

WebElement.pm 12 KB

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