| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- package Selenium::Remote::WebElement;
- use strict;
- use warnings;
- use Data::Dumper;
- =head1 NAME
- Selenium::Remote::WebElement - Representation of an HTML Element used by Selenium Remote Driver
- =cut
- =head1 DESCRIPTION
- Selenium Webdriver represents all the HTML elements as WebElement. This module
- provides a mechanism to represent them as objects & perform various actions on
- the related elements. This module should not be instantiated directly by the end
- user. Selenium::Remote::Driver instantiates this module when required. Typically,
- the find_element method in Selenium::Remote::Driver returns this object on which
- various element related operations can be carried out.
- =cut
- =head1 FUNCTIONS
- =cut
- sub new {
- my ($class, $id, $parent) = @_;
- my $self = {
- id => $id,
- driver => $parent,
- };
- bless $self, $class or die "Can't bless $class: $!";
- return $self;
- }
- sub _execute_command {
- my ($self) = shift;
- return $self->{driver}->_execute_command(@_);
- }
- =head2 click
- Description:
- Click the element.
- Usage:
- $elem->click();
- =cut
- sub click {
- my ($self) = @_;
- my $res = { 'command' => 'clickElement', 'id' => $self->{id} };
- return $self->_execute_command($res);
- }
- =head2 get_value
- Description:
- Query for the value of an element, as determined by its value attribute.
- Output:
- {STRING | NULL} The element's value, or null if it does'nt have a value attribute.
- Usage:
- $elem->get_value();
- =cut
- sub get_value {
- my ($self) = @_;
- my $res = { 'command' => 'getElementValue', 'id' => $self->{id} };
- return $self->_execute_command($res);
- }
- =head2 submit
- Description:
- Submit a FORM element. The submit command may also be applied to any element
- that is a descendant of a FORM element.
- Usage:
- $elem->submit();
- =cut
- sub submit {
- my ($self) = @_;
- my $res = { 'command' => 'submitElement', 'id' => $self->{id} };
- return $self->_execute_command($res);
- }
- =head2 send_keys
- Description:
- Send a sequence of key strokes to an element.
- Input: 1
- Required:
- {ARRAY | STRING} - Array of strings or a string.
- Usage:
- $elem->send_keys('abcd', 'efg');
- $elem->send_keys('hijk');
- =cut
- sub send_keys {
- my ($self, @strings) = @_;
- my $res = { 'command' => 'sendKeysToElement', 'id' => $self->{id} };
- my $params = {
- 'value' => \@strings,
- };
- return $self->_execute_command($res, $params);
- }
- =head2 is_selected
- Description:
- Determine if an OPTION element, or an INPUT element of type checkbox or
- radiobutton is currently selected.
- Output:
- BOOLEAN - whether the element is selected
- Usage:
- $elem->is_selected();
- =cut
- sub is_selected {
- my ($self) = @_;
- my $res = { 'command' => 'isElementSelected', 'id' => $self->{id} };
- return $self->_execute_command($res);
- }
- =head2 set_selected
- Description:
- Select an OPTION element, or an INPUT element of type checkbox or radiobutton.
- Usage:
- $elem->set_selected();
- =cut
- sub set_selected {
- my ($self) = @_;
- my $res = { 'command' => 'setElementSelected', 'id' => $self->{id} };
- return $self->_execute_command($res);
- }
- =head2 toggle
- Description:
- Toggle whether an OPTION element, or an INPUT element of type checkbox or
- radiobutton is currently selected.
-
- Output:
- BOOLEAN - Whether the element is selected after toggling its state.
- Usage:
- $elem->toggle();
- =cut
- sub toggle {
- my ($self) = @_;
- my $res = { 'command' => 'toggleElement', 'id' => $self->{id} };
- return $self->_execute_command($res);
- }
- =head2 is_enabled
- Description:
- Determine if an element is currently enabled.
-
- Output:
- BOOLEAN - Whether the element is enabled.
- Usage:
- $elem->is_enabled();
- =cut
- sub is_enabled {
- my ($self) = @_;
- my $res = { 'command' => 'isElementEnabled', 'id' => $self->{id} };
- return $self->_execute_command($res);
- }
- =head2 get_element_location
- Description:
- Determine an element's location on the page. The point (0, 0) refers to the
- upper-left corner of the page.
-
- Output:
- HASH - The X and Y coordinates for the element on the page.
- Usage:
- $elem->get_element_location();
- =cut
- sub get_element_location {
- my ($self) = @_;
- my $res = { 'command' => 'getElementLocation', 'id' => $self->{id} };
- return $self->_execute_command($res);
- }
- =head2 get_element_location_in_view
- Description:
- Determine an element's location on the screen once it has been scrolled
- into view.
-
- Note: This is considered an internal command and should only be used to
- determine an element's location for correctly generating native events.
-
- Output:
- {x:number, y:number} The X and Y coordinates for the element on the page.
- Usage:
- $elem->get_element_location_in_view();
- =cut
- sub get_element_location_in_view {
- my ($self) = @_;
- my $res = { 'command' => 'getElementLocationInView', 'id' => $self->{id} };
- return $self->_execute_command($res);
- }
- =head2 get_tag_name
- Description:
- Query for an element's tag name.
-
- Output:
- STRING - The element's tag name, as a lowercase string.
- Usage:
- $elem->get_tag_name();
- =cut
- sub get_tag_name {
- my ($self) = @_;
- my $res = { 'command' => 'getElementTagName', 'id' => $self->{id} };
- return $self->_execute_command($res);
- }
- =head2 clear
- Description:
- Clear a TEXTAREA or text INPUT element's value.
-
- Usage:
- $elem->clear();
- =cut
- sub clear {
- my ($self) = @_;
- my $res = { 'command' => 'clearElement', 'id' => $self->{id} };
- return $self->_execute_command($res);
- }
- =head2 get_attribute
- Description:
- Get the value of an element's attribute.
- Input: 1
- Required:
- STRING - name of the attribute of the element
-
- Output:
- {STRING | NULL} The value of the attribute, or null if it is not set on the element.
- Usage:
- $elem->get_attribute('name');
- =cut
- sub get_attribute {
- my ($self, $attr_name) = @_;
- if (not defined $attr_name) {
- return 'Attribute name not provided';
- }
- my $res = {'command' => 'getElementAttribute',
- 'id' => $self->{id},
- 'name' => $attr_name,
- };
- return $self->_execute_command($res);
- }
- =head2 is_displayed
- Description:
- Determine if an element is currently displayed.
-
- Output:
- BOOLEAN - Whether the element is displayed.
- Usage:
- $elem->is_displayed();
- =cut
- sub is_displayed {
- my ($self) = @_;
- my $res = { 'command' => 'isElementDisplayed', 'id' => $self->{id} };
- return $self->_execute_command($res);
- }
- =head2 drag
- Description:
- Drag and drop an element. The distance to drag an element should be
- specified relative to the upper-left corner of the page and it starts at 0,0
- Input: 2
- Required:
- NUMBER - X axis distance in pixels
- NUMBER - Y axis distance in pixels
-
- Usage:
- $elem->drag(216,158);
- =cut
- sub drag {
- my ($self, $x, $y) = @_;
- if ((not defined $x) || (not defined $y)){
- return 'X & Y pixel coordinates not provided';
- }
- my $res = {'command' => 'dragElement','id' => $self->{id}};
- my $params = {
- 'x' => $x,
- 'y' => $y,
- };
- return $self->_execute_command($res, $params);
- }
- =head2 get_size
- Description:
- Determine an element's size in pixels. The size will be returned with width
- and height properties.
- Output:
- HASH - The width and height of the element, in pixels.
-
- Usage:
- $elem->get_size();
- =cut
- sub get_size {
- my ($self) = @_;
- my $res = { 'command' => 'getElementSize', 'id' => $self->{id} };
- return $self->_execute_command($res);
- }
- =head2 get_text
- Description:
- Get the innerText of the element.
- Output:
- STRING - innerText of an element
-
- Usage:
- $elem->get_text();
- =cut
- sub get_text {
- my ($self) = @_;
- my $res = { 'command' => 'getElementText', 'id' => $self->{id} };
- return $self->_execute_command($res);
- }
- =head2 get_css_attribute
- Description:
- Query the value of an element's computed CSS property. The CSS property to
- query should be specified using the CSS property name, not the JavaScript
- property name (e.g. background-color instead of backgroundColor).
- Input: 1
- Required:
- STRING - name of the css-attribute
- Output:
- STRING - Value of the css attribute
-
- Usage:
- $elem->get_css_attribute('background-color');
- =cut
- sub get_css_attribute {
- my ($self, $attr_name) = @_;
- if (not defined $attr_name) {
- return 'CSS attribute name not provided';
- }
- my $res = {'command' => 'getElementValueOfCssProperty',
- 'id' => $self->{id},
- 'property_name' => $attr_name,
- };
- return $self->_execute_command($res);
- }
- =head2 hover
- Description:
- Move the mouse over an element.
- Usage:
- $elem->hover();
- =cut
- sub hover {
- my ($self) = @_;
- my $res = { 'command' => 'hoverOverElement', 'id' => $self->{id} };
- return $self->_execute_command($res);
- }
- 1;
- =head1 SEE ALSO
- For more information about Selenium , visit the website at
- L<http://code.google.com/p/selenium/>.
- =head1 BUGS
- The Selenium issue tracking system is available online at
- L<http://github.com/aivaturi/Selenium-Remote-Driver/issues>.
- =head1 AUTHOR
- Perl Bindings for Selenium Remote Driver by Aditya Ivaturi <ivaturi@gmail.com>
- =head1 LICENSE
- Copyright (c) 2010 Aditya Ivaturi
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
|