/usr/share/perl5/Weasel/Session.pm is in libweasel-perl 0.11-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 | =head1 NAME
Weasel::Session - Connection to an encapsulated test driver
=head1 VERSION
0.02
=head1 SYNOPSIS
use Weasel;
use Weasel::Session;
use Weasel::Driver::Selenium2;
my $weasel = Weasel->new(
default_session => 'default',
sessions => {
default => Weasel::Session->new(
driver => Weasel::Driver::Selenium2->new(%opts),
),
});
$weasel->session->get('http://localhost/index');
=head1 DESCRIPTION
=cut
package Weasel::Session;
use strict;
use warnings;
use Moose;
use Module::Runtime qw/ use_module /;;
use Weasel::FindExpanders qw/ expand_finder_pattern /;
use Weasel::WidgetHandlers qw| best_match_handler_class |;
our $VERSION = '0.02';
=head1 ATTRIBUTES
=over
=item driver
Holds a reference to the sessions's driver.
=cut
has 'driver' => (is => 'ro',
required => 1,
handles => {
'start' => 'start',
'stop' => 'stop',
'restart' => 'restart',
'started' => 'started',
});
=item widget_groups
Contains the list of widget groups to be used with the session, or
uses all groups when undefined.
Note: this functionality allows one to load multiple groups into the running
perl instance, while using different groups in various sessions.
=cut
has 'widget_groups' => (is => 'rw');
=item base_url
Holds the prefix that will be prepended to every URL passed
to this API.
The prefix can be an environment variable, e.g. ${VARIABLE}.
It will be expanded and default to hppt://localhost:5000 if not defined.
If it is not an environment variable, it will be used as is.
=cut
has 'base_url' => (is => 'rw',
isa => 'Str',
default => '' );
=item page
Holds the root element of the target HTML page (the 'html' tag).
=cut
has 'page' => (is => 'ro',
isa => 'Weasel::Element::Document',
builder => '_page_builder',
lazy => 1);
sub _page_builder {
my $self = shift;
my $class = use_module($self->page_class);
return $class->new(session => $self);
}
=item log_hook
Upon instantiation can be set to log consumer; a function of 3 arguments:
1. the name of the event
2. the text to be logged (or a coderef to be called without arguments returning such)
=cut
has 'log_hook' => (is => 'ro',
isa => 'Maybe[CodeRef]');
=item page_class
Upon instantiation can be set to an alternative class name for the C<page>
attribute.
=cut
has 'page_class' => (is => 'ro',
isa => 'Str',
default => 'Weasel::Element::Document');
=item retry_timeout
The number of seconds to poll for a condition to become true. Global
setting for the C<wait_for> function.
=cut
has 'retry_timeout' => (is => 'rw',
default => 15,
isa => 'Num',
);
=item poll_delay
The number of seconds to wait between state polling attempts. Global
setting for the C<wait_for> function.
=cut
has 'poll_delay' => (is => 'rw',
default => 0.5,
isa => 'Num',
);
=back
=head1 METHODS
=over
=item clear($element)
Clears any input entered into elements supporting it. Generally applies to
textarea elements and input elements of type text and password.
=cut
sub clear {
my ($self, $element) = @_;
$self->_logged(sub { $self->driver->clear($element->_id); },
'clear', 'clearing input element');
}
=item click([$element])
Simulates a single mouse click. If an element argument is provided, that
element is clicked. Otherwise, the browser window is clicked at the
current mouse location.
=cut
sub click {
my ($self, $element) = @_;
$self->_logged(
sub {
$self->driver->click(($element) ? $element->_id : undef);
},
'click', ($element) ? 'clicking element' : 'clicking window');
}
=item find($element, $locator [, scheme => $scheme] [, %locator_args])
Finds the first child of C<$element> matching C<$locator>.
See L<Weasel::Element>'s C<find> function for more documentation.
=cut
sub find {
my ($self, @args) = @_;
my $rv;
$self->_logged(
sub {
$self->wait_for(
sub {
my @rv = @{$self->find_all(@args)};
return $rv = shift @rv;
});
}, 'find', 'find ' . $args[1]);
return $rv;
}
=item find_all($element, $locator, [, scheme => $scheme] [, %locator_args ])
Finds all child elements of C<$element> matching C<$locator>. Returns,
depending on scalar or list context, an arrayref or a list with matching
elements.
See L<Weasel::Element>'s C<find_all> function for more documentation.
=cut
sub find_all {
my ($self, $element, $pattern, %args) = @_;
my $expanded_pattern = expand_finder_pattern($pattern, \%args);
my @rv = $self->_logged(
sub {
return
map { $self->_wrap_widget($_) }
$self->driver->find_all($element->_id,
$expanded_pattern,
$args{scheme});
},
'find_all',
sub {
my ($rv) = @_;
return "found " . scalar(@$rv) . " elements for $pattern "
. (join(', ', %args)) . "\n"
. (join("\n",
map { ' - ' . ref($_)
. ' (' . $_->tag_name . ")" } @$rv));
},
"pattern: $pattern");
return wantarray ? @rv : \@rv;
}
=item get($url)
Loads C<$url> into the active browser window of the driver connection,
after prefixing with C<base_url>.
=cut
sub get {
my ($self, $url) = @_;
my $base = $self->base_url =~ /\$\{([a-zA-Z0-9_]+)\}/
? $ENV{$1} // "http://localhost:5000"
: $self->base_url;
$url = $base . $url;
###TODO add logging warning of urls without protocol part
# which might indicate empty 'base_url' where one is assumed to be set
$self->_logged(
sub {
return $self->driver->get($url);
}, 'get', "loading URL: $url");
}
=item get_attribute($element, $attribute)
Returns the value of the attribute named by C<$attribute> of the element
identified by C<$element>, or C<undef> if the attribute isn't defined.
=cut
sub get_attribute {
my ($self, $element, $attribute) = @_;
return $self->_logged(
sub {
return $self->driver->get_attribute($element->_id, $attribute);
}, 'get_attribute', "element attribute '$attribute'");
}
=item get_text($element)
Returns the 'innerHTML' of the element identified by C<$element>.
=cut
sub get_text {
my ($self, $element) = @_;
return $self->_logged(
sub {
return $self->driver->get_text($element->_id);
},
'get_text', 'element text');
}
=item is_displayed($element)
Returns a boolean value indicating if the element identified by
C<$element> is visible on the page, i.e. that it can be scrolled into
the viewport for interaction.
=cut
sub is_displayed {
my ($self, $element) = @_;
return $self->_logged(
sub {
return $self->driver->is_displayed($element->_id);
},
'is_displayed', 'query is_displayed');
}
=item screenshot($fh)
Writes a screenshot of the browser's window to the filehandle C<$fh>.
Note: this version assumes pictures of type PNG will be written;
later versions may provide a means to query the exact image type of
screenshots being generated.
=cut
sub screenshot {
my ($self, $fh) = @_;
$self->_logged(
sub {
$self->driver->screenshot($fh);
}, 'screenshot', 'screenshot');
}
=item get_page_source($fh)
Writes a get_page_source of the browser's window to the filehandle C<$fh>.
=cut
sub get_page_source {
my ($self) = @_;
$self->_logged(
sub {
$self->driver->get_page_source();
}, 'get_page_source', 'get_page_source');
}
=item send_keys($element, @keys)
Send the characters specified in the strings in C<@keys> to C<$element>,
simulating keyboard input.
=cut
sub send_keys {
my ($self, $element, @keys) = @_;
$self->_logged(
sub {
$self->driver->send_keys($element->_id, @keys);
},
'send_keys', 'sending keys: ' . join('', @keys // ()));
}
=item tag_name($element)
Returns the tag name of the element identified by C<$element>.
=cut
sub tag_name {
my ($self, $element) = @_;
return $self->_logged(sub { return $self->driver->tag_name($element->_id) },
'tag_name',
sub { my $tag = shift; return "found tag with name $tag" },
'getting tag name');
}
=item wait_for($callback, [ retry_timeout => $number,] [poll_delay => $number])
Polls $callback->() until it returns true, or C<wait_timeout> expires
-- whichever comes first.
The arguments retry_timeout and poll_delay can be used to override the
session-global settings.
=cut
sub wait_for {
my ($self, $callback, %args) = @_;
$self->_logged(
sub {
$self->driver->wait_for($callback,
retry_timeout => $self->retry_timeout,
poll_delay => $self->poll_delay,
%args);
},
'wait_for', 'waiting for condition');
}
before 'BUILDARGS', sub {
my ($class, @args) = @_;
my $args = (ref $args[0]) ? $args[0] : { @args };
confess "Driver used to construct session object uses old API version;
some functionality may not work correctly"
if ($args->{driver}
&& $args->{driver}->implements < $Weasel::DriverRole::VERSION);
};
sub _appending_wrap {
my ($str) = @_;
return sub {
my $rv = shift;
if ($rv) {
return "$str ($rv)";
}
else {
return $str;
}
}
}
=item _logged($wrapped_fn, $event, $log_item, $log_item_pre)
Invokes C<log_hook> when it's defined, before and after calling C<$wrapped_fn>
with no arguments, with the 'pre_' and 'post_' prefixes to the event name.
C<$log_item> can be a fixed string or a function of one argument returning
the string to be logged. The argument passed into the function is the value
returned by the C<$wrapped_fn>.
In case there is no C<$log_item_pre> to be called on the 'pre_' event,
C<$log_item> will be used instead, with no arguments.
For performance reasons, the C<$log_item> and C<$log_item_pre> - when
coderefs - aren't called; instead they are passed as-is to the
C<$log_hook> for lazy evaluation.
=cut
sub _logged {
my ($self, $f, $e, $l, $lp) = @_;
my $hook = $self->log_hook;
return $f->() if ! defined $hook;
$lp //= $l;
my $pre = (ref $lp eq 'CODE') ? $lp : _appending_wrap($lp);
my $post = (ref $l eq 'CODE') ? $l : _appending_wrap($l);
$hook->("pre_$e", $pre);
if (wantarray) {
my @rv = $f->();
$hook->("post_$e", sub { return $l->(\@rv); });
return @rv;
}
else {
my $rv = $f->();
$hook->("post_$e", sub { return $l->($rv); });
return $rv;
}
};
=item _wrap_widget($_id)
Finds all matching widget selectors to wrap the driver element in.
In case of multiple matches, selects the most specific match
(the one with the highest number of requirements).
=cut
sub _wrap_widget {
my ($self, $_id) = @_;
my $best_class = best_match_handler_class(
$self->driver, $_id, $self->widget_groups) // 'Weasel::Element';
return $best_class->new(_id => $_id, session => $self);
}
=back
=head1 SEE ALSO
L<Weasel>
=head1 COPYRIGHT
(C) 2016 Erik Huelsmann
Licensed under the same terms as Perl.
=cut
1;
|