2013-02-12 80 views
1

Selenium 2. *在Linux上以Firefox作为浏览器运行。Selenium + Perl:检查警报是否存在?

我使用perl和Selenium :: Remote :: Driver模块来与服务器交互。

是否有任何可用来检查警报是否存在? perl模块提供了几个函数来在警报上单击确定或从中获取文本,但是如果没有警报,这会引发错误 - 如何避免错误并仍然排除任何警报?

基本上,我想删除页面完成加载(如果有的话)所有警报,但不知道如何?

我尝试过的另一个选择是通过在firefox配置文件中设置一个变量来禁用所有的警报(当您使用浏览器时它会自动运行),但是当Selenium使用浏览器时警报仍然存在,因为我认为Selenium处理警报本身,因为“handlesAlerts”功能,它始终设置为true,我不知道如何禁用它。如果无法检查是否存在警报,则可能是解决方案。

任何人有想法吗?

回答

1

你可以尝试关闭快讯,使用eval块来处理异常

eval { 
    $driver->accept_alert; 
}; 
if ([email protected]){ 
warn "Maybe no alert?": 
warn [email protected]; 
} 
+0

嘿谢谢你,帮助! – WolfPRD 2013-02-14 11:27:52

+0

这似乎工作,谢谢!我有一个页面,有时确认对话框没有按预期弹出,并且accept_alert导致了一个致命的错误。在一个评估中包装它似乎工作到目前为止,但我必须跑一段时间才能确定。 – 2013-07-24 13:57:08

0

我创建了几个用于检查警报功能,然后取消或者根据需要与其进行交互。

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); 
} 
相关问题