File Coverage

blib/lib/Weasel/Element.pm
Criterion Covered Total %
statement 16 33 48.4
branch n/a
condition 0 2 0.0
subroutine 6 14 42.8
pod 10 10 100.0
total 32 59 54.2


line stmt bran cond sub pod time code
1              
2             =head1 NAME
3              
4             Weasel::Element - The base HTML/Widget element class
5              
6             =head1 VERSION
7              
8             0.02
9              
10             =head1 SYNOPSIS
11              
12             my $element = $session->page->find("./input[\@name='phone']");
13             my $value = $element->send_keys('555-885-321');
14              
15             =head1 DESCRIPTION
16              
17             This module provides the base class for all page elements, encapsulating
18             the regular element interactions, such as finding child element, querying
19             attributes and the tag name, etc.
20              
21             =cut
22              
23             =head1 DEPENDENCIES
24              
25              
26              
27             =cut
28              
29             package Weasel::Element;
30              
31 2     2   1849 use strict;
  2         4  
  2         67  
32 2     2   11 use warnings;
  2         5  
  2         58  
33              
34 2     2   10 use Moose;
  2         4  
  2         15  
35 2     2   13400 use namespace::autoclean;
  2         5  
  2         17  
36              
37             =head1 ATTRIBUTES
38              
39             =over
40              
41             =item session
42              
43             Required. Holds a reference to the L<Weasel::Session> to which the element
44             belongs. Used to access the session's driver to query element properties.x
45              
46             =cut
47              
48             has session => (is => 'ro',
49             isa => 'Weasel::Session',
50             required => 1,
51             );
52              
53             =item _id
54              
55             Required. Holds the I<element_id> used by the session's driver to identify
56             the element.
57              
58             =cut
59              
60             has _id => (is => 'ro',
61             required => 1);
62              
63             =back
64              
65             =head1 SUBROUTINES/METHODS
66              
67             =over
68              
69             =item find($locator [, scheme => $scheme] [, widget_args => \@args ] [, %locator_args])
70              
71             Finds the first child element matching c<$locator>. Returns C<undef>
72             when not found. Optionally takes a scheme argument to identify non-xpath
73             type locators.
74              
75             In case the C<$locator> is a mnemonic (starts with an asterisk ['*']),
76             additional arguments may be provided for expansion of the mnemonic. See
77             L<Weasel::FindExpanders::HTML> for documentation of the standard expanders.
78              
79             Any arguments passed in the C<$widget_args> array reference, are passed to
80             the widget's constructor.
81              
82             =cut
83              
84             sub find {
85 0     0 1 0 my ($self, @args) = @_;
86              
87 0         0 return $self->session->find($self, @args);
88             }
89              
90             =item find_all($locator [, scheme => $scheme] [, widget_args => \@args ] [, %locator_args])
91              
92             Returns, depending on scalar vs array context, a list or an arrayref
93             with matching elements. Returns an empty list or ref to an empty array
94             when none found. Optionally takes a scheme argument to identify non-xpath
95             type locators.
96              
97             In case the C<$locator> is a mnemonic (starts with an asterisk ['*']),
98             additional arguments may be provided for expansion of the mnemonic. See
99             L<Weasel::FindExpanders::HTML> for documentation of the standard expanders.
100              
101             Any arguments passed in the C<$widget_args> array reference, are passed to
102             the widget's constructor.
103              
104             =cut
105              
106             sub find_all {
107 2     2 1 7 my ($self, @args) = @_;
108              
109             # expand $locator based on framework plugins (e.g. Dojo)
110 2         50 return $self->session->find_all($self, @args);
111             }
112              
113             =item get_attribute($attribute)
114              
115             Returns the value of the element's attribute named in C<$attribute> or
116             C<undef> if none exists.
117              
118             Note: Some browsers apply default values to attributes which are not
119             part of the original page. As such, there's no direct relation between
120             the existence of attributes in the original page and this function
121             returning C<undef>.
122              
123             Note: Those users familiar with Selenium might be looking for a method
124             called C<is_selected> or C<set_selected>. Weasel doesn't have that
125             short-hand. Please use C<get_attribute>/C<set_attribute> with an
126             attribute name of C<selected> instead.
127              
128             =cut
129              
130             sub get_attribute {
131 0     0 1 0 my ($self, $attribute) = @_;
132              
133 0         0 return $self->session->get_attribute($self, $attribute);
134             }
135              
136             =item set_attribute($attribute, $value)
137              
138             Sets the value of the element's attribute named in C<$attribute>.
139              
140             Note: Those users familiar with Selenium might be looking for a method
141             called C<is_selected> or C<set_selected>. Weasel doesn't have that
142             short-hand. Please use C<get_attribute>/C<set_attribute> with an
143             attribute name of C<selected> instead.
144              
145             =cut
146              
147             sub set_attribute {
148 0     0 1 0 my ($self, $attribute, $value) = @_;
149              
150 0         0 return $self->session->set_attribute($self, $attribute, $value);
151             }
152              
153             =item get_text()
154              
155             Returns the element's 'innerHTML'.
156              
157             =cut
158              
159             sub get_text {
160 0     0 1 0 my ($self) = @_;
161              
162 0         0 return $self->session->get_text($self);
163             }
164              
165              
166             =item has_class
167              
168             =cut
169              
170             sub has_class {
171 0     0 1 0 my ($self, $class) = @_;
172              
173 0   0     0 return grep { $_ eq $class }
  0         0  
174             split /\s+/x, ($self->get_attribute('class') // '');
175             }
176              
177             =item is_displayed
178              
179             Returns a boolean indicating if an element is visible (e.g.
180             can potentially be scrolled into the viewport for interaction).
181              
182             =cut
183              
184             sub is_displayed {
185 0     0 1 0 my ($self) = @_;
186              
187 0         0 return $self->session->is_displayed($self);
188             }
189              
190             =item click()
191              
192             Scrolls the element into the viewport and simulates it being clicked on.
193              
194             =cut
195              
196             sub click {
197 0     0 1 0 my ($self) = @_;
198 0         0 return $self->session->click($self);
199             }
200              
201             =item send_keys(@keys)
202              
203             Focusses the element and simulates keyboard input. C<@keys> can be any
204             number of strings containing unicode characters to be sent. E.g.
205              
206             $element->send_keys("hello", ' ', "world");
207              
208             =cut
209              
210             sub send_keys {
211 0     0 1 0 my ($self, @keys) = @_;
212              
213 0         0 return $self->session->send_keys($self, @keys);
214             }
215              
216             =item tag_name()
217              
218             Returns the name of the tag of the element, e.g. 'div' or 'input'.
219              
220             =cut
221              
222             sub tag_name {
223 4     4 1 6 my ($self) = @_;
224              
225 4         112 return $self->session->tag_name($self);
226             }
227              
228             =back
229              
230             =cut
231              
232             =head1 AUTHOR
233              
234             Erik Huelsmann
235              
236             =head1 CONTRIBUTORS
237              
238             Erik Huelsmann
239             Yves Lavoie
240              
241             =head1 MAINTAINERS
242              
243             Erik Huelsmann
244              
245             =head1 BUGS AND LIMITATIONS
246              
247             Bugs can be filed in the GitHub issue tracker for the Weasel project:
248             https://github.com/perl-weasel/weasel/issues
249              
250             =head1 SOURCE
251              
252             The source code repository for Weasel is at
253             https://github.com/perl-weasel/weasel
254              
255             =head1 SUPPORT
256              
257             Community support is available through
258             L<perl-weasel@googlegroups.com|mailto:perl-weasel@googlegroups.com>.
259              
260             =head1 LICENSE AND COPYRIGHT
261              
262             (C) 2016-2023 Erik Huelsmann
263              
264             Licensed under the same terms as Perl.
265              
266             =cut
267              
268              
269             __PACKAGE__->meta->make_immutable;
270              
271             1;
272