| 1 | package Selenium::Waiter; | ||||||||||||||||||||||||||||
| 2 | 14 | $Selenium::Waiter::VERSION = '1.49'; | |||||||||||||||||||||||||||
| 3 | 14 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 14 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # ABSTRACT: Provides a utility wait_until function | ||||||||||||||||||||||||||||
| 7 | 14 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 14 | require Exporter; | |||||||||||||||||||||||||||
| 9 | 14 | our @ISA = qw/Exporter/; | |||||||||||||||||||||||||||
| 10 | 14 | our @EXPORT = qw/wait_until/; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | sub wait_until (&%) { | ||||||||||||||||||||||||||||
| 14 | 8 | ▶ | my $assert = shift; | ||||||||||||||||||||||||||
|
Subroutine
wait_until: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 8 | my $args = { | |||||||||||||||||||||||||||
| 16 | timeout => 30, | ||||||||||||||||||||||||||||
| 17 | interval => 1, | ||||||||||||||||||||||||||||
| 18 | debug => 0, | ||||||||||||||||||||||||||||
| 19 | die => 0, | ||||||||||||||||||||||||||||
| 20 | @_ | ||||||||||||||||||||||||||||
| 21 | }; | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 8 | my $start = time; | |||||||||||||||||||||||||||
| 24 | my $timeout_not_elapsed = sub { | ||||||||||||||||||||||||||||
| 25 | 11 | ▶ | my $elapsed = time - $start; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 11 | return $elapsed < $args->{timeout}; | |||||||||||||||||||||||||||
| 27 | 8 | }; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 8 | my $exception = ''; | |||||||||||||||||||||||||||
| 30 | 8 | ▶ | while ( $timeout_not_elapsed->() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 8 | my $assert_ret; | |||||||||||||||||||||||||||
| 32 | my $try_ret = try { | ||||||||||||||||||||||||||||
| 33 | 8 | ▶ | $assert_ret = $assert->(); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 34 | 6 | ▶ | return $assert_ret if $assert_ret; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | catch { | ||||||||||||||||||||||||||||
| 37 | 2 | ▶ | $exception = $_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 38 | 2 | ▶ | die $_ if $args->{die}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 1 | ▶ | warn $_ if $args->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 1 | return ''; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | finally { | ||||||||||||||||||||||||||||
| 43 | 8 | ▶ | if ( !$assert_ret ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 4 | sleep( $args->{interval} ); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 8 | }; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 7 | ▶ | return $try_ret if $try_ret; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 3 | ▶ | warn 'timeout' if $args->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # No need to repeat ourselves if we're already debugging. | ||||||||||||||||||||||||||||
| 54 | 3 | ▶ | warn $exception if $exception && !$args->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$exception and not $args->{"debug"}
MC/DC50%1/2
$exception and not $args->{"debug"}
$exception
not $args->{"debug"}
Truth table66%2/3
A and not(B)
A= $exceptionB= not $args->{"debug"}
| |||||||||||||||||||||||||||||
| 55 | 3 | return ''; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 14 | 1; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | __END__ | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =pod | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =head1 NAME | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | Selenium::Waiter - Provides a utility wait_until function | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =head1 VERSION | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | version 1.49 | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | use Selenium::Waiter qw/wait_until/; | ||||||||||||||||||||||||||||
| 77 | my $d = Selenium::Remote::Driver->new; | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | my $div = wait_until { $d->find_element('div', 'css') }; | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =head2 wait_until | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | Exported by default, it takes a BLOCK (required) and optionally a | ||||||||||||||||||||||||||||
| 86 | hash of configuration params. It uses a prototype to take its | ||||||||||||||||||||||||||||
| 87 | arguments, so usage looks look like: | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | use Selenium::Waiter; | ||||||||||||||||||||||||||||
| 90 | my $div = wait_until { $driver->find_element('div', 'css') }; | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | The above snippet will search for C<css=div> for thirty seconds; if it | ||||||||||||||||||||||||||||
| 93 | ever finds the element, it will immediately return. More generally, | ||||||||||||||||||||||||||||
| 94 | Once the BLOCK returns anything truthy, the C<wait_until> will stop | ||||||||||||||||||||||||||||
| 95 | evaluating and the return of the BLOCK will be returned to you. If the | ||||||||||||||||||||||||||||
| 96 | BLOCK never returns a truthy value, we'll wait until the elapsed time | ||||||||||||||||||||||||||||
| 97 | has increased past the timeout and then return an empty string C<''>. | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | B<Achtung!> Please make sure that the BLOCK you pass in can be | ||||||||||||||||||||||||||||
| 100 | executed in a timely fashion. For Webdriver, that means that you | ||||||||||||||||||||||||||||
| 101 | should set the appropriate C<implicit_wait> timeout low (a second or | ||||||||||||||||||||||||||||
| 102 | less!) so that we can rerun the assert sub repeatedly. We don't do | ||||||||||||||||||||||||||||
| 103 | anything fancy behind the scenes: we just execute the BLOCK you pass | ||||||||||||||||||||||||||||
| 104 | in and sleep between iterations. If your BLOCK actively blocks for | ||||||||||||||||||||||||||||
| 105 | thirty seconds, like a C<find_element> would do with an | ||||||||||||||||||||||||||||
| 106 | C<implicit_wait> of 30 seconds, we won't be able to help you at all - | ||||||||||||||||||||||||||||
| 107 | that blocking behavior is on the webdriver server side, and is out of | ||||||||||||||||||||||||||||
| 108 | our control. We'd run one iteration, get blocked for thirty seconds, | ||||||||||||||||||||||||||||
| 109 | and return control to you at that point. | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =head4 Dying | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | PLEASE check the return value before proceeding, as we unwisely | ||||||||||||||||||||||||||||
| 114 | suppress any attempts your BLOCK may make to die or croak. The BLOCK | ||||||||||||||||||||||||||||
| 115 | you pass is called in a L<Try::Tiny/try>, and if any of the | ||||||||||||||||||||||||||||
| 116 | invocations of your function throw and the BLOCK never becomes true, | ||||||||||||||||||||||||||||
| 117 | we'll carp exactly once at the end immediately before returning | ||||||||||||||||||||||||||||
| 118 | false. We overwrite the death message from each iteration, so at the | ||||||||||||||||||||||||||||
| 119 | end, you'll only see the most recent death message. | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # warns once after thirty seconds: "kept from dying"; | ||||||||||||||||||||||||||||
| 122 | wait_until { die 'kept from dying' }; | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | The output of C<die>s from each iteration can be exposed if you wish | ||||||||||||||||||||||||||||
| 125 | to see the massacre: | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # carps: "kept from dying" once a second for thirty seconds | ||||||||||||||||||||||||||||
| 128 | wait_until { die 'kept from dying' } debug => 1; | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | If you want to die anyways, just pass die => 1 to wait_until instead: | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # Dies on the first failure, do your own error handling: | ||||||||||||||||||||||||||||
| 133 | wait_until { die 'oops' } die => 1; | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =head4 Timeouts and Intervals | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | You can also customize the timeout, and/or the retry interval between | ||||||||||||||||||||||||||||
| 138 | iterations. | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # prints hi three four times at 0, 3, 6, and 9 seconds | ||||||||||||||||||||||||||||
| 141 | wait_until { print 'hi'; '' } timeout => 10, interval => 3; | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | Current Maintainers: | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =over 4 | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =item * | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | George S. Baugh <george@troglodyne.net> | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =back | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | Previous maintainers: | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =over 4 | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =item * | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | Daniel Gempesaw <gempesaw@gmail.com> | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =item * | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Emmanuel Peroumalnaïk <peroumalnaik.emmanuel@gmail.com> | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =item * | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | Luke Closs <cpan@5thplane.com> | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =item * | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | Mark Stosberg <mark@stosberg.com> | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =back | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | Original authors: | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =over 4 | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =item * | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | Aditya Ivaturi <ivaturi@gmail.com> | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =back | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | Copyright (c) 2010-2011 Aditya Ivaturi, Gordon Child | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | Copyright (c) 2014-2017 Daniel Gempesaw | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | Copyright (c) 2018-2021 George S. Baugh | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||||||||||||
| 196 | you may not use this file except in compliance with the License. | ||||||||||||||||||||||||||||
| 197 | You may obtain a copy of the License at | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||||||
| 202 | distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||||||
| 203 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||||||
| 204 | See the License for the specific language governing permissions and | ||||||||||||||||||||||||||||
| 205 | limitations under the License. | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =cut | ||||||||||||||||||||||||||||