我创建了几个函数来检查警报,然后根据需要取消或与之交互。
use Try::Tiny qw( try catch );
# checks if there is a javascript alert/confirm/input on the screen
sub alert_is_present
{
my $d = shift;
my $alertPresent = 0;
try{
my $alertTxt = $d->get_alert_text();
logIt( "alert open: $alertTxt", 'DEBUG' ) if $alertTxt;
$alertPresent++;
}catch{
my $err = $_;
if( $err =~ 'modal dialog when one was not open' ){
logIt( 'no alert open', 'DEBUG2' );
}else{
logIt( "ERROR: getting alert_text: $_", 'ERROR' );
}
};
return $alertPresent;
}
# Assumes caller confirmed an alert is present!! Either cancels the alert or
types any passed in data and accepts it.
sub handle_alert
{
my ( $d, $action, $data ) = @_;
logIt( "handle_alert called with: $action, $data", 'DEBUG' );
if( $action eq 'CANCEL' ){
$d->dismiss_alert();
}else{
$d->send_keys_to_alert( $data )
if $data;
$d->accept_alert();
}
$d->pause( 500 );
}