WebDriver-如何验证是否存在警报? [英] WebDriver - How to verify if an alert is present?

查看:91
本文介绍了WebDriver-如何验证是否存在警报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在selenium2(Webdriver)中如何验证警报是否存在?并继续执行某些操作(如果不存在)!

In selenium2 (Webdriver)How to verify if an alert is present? and continue doing something if its not present!!!

我正在这样做:

driver.findElement(By.id("btn_may_or_maynot_showalert")).click();
WebDriverWait wait = new WebDriverWait(driver, 2);
try{
    wait.until(ExpectedConditions.alertIsPresent());
    Alert alert = driver.switchTo().alert();
    alert.accept();
}
catch (Exception e){
    System.out.println("No alert");
}
driver.findElement(By.id("Cont_doing_something")).click();

这很好,但是还有更好的方法吗?

This works fine But is there a better way?

推荐答案

不,您正在按照图书馆的期望来做事。但是,该库的原理之一是,您应该始终知道对自动化代码的期望。这意味着您不应该遇到可能会或可能不会按钮引起警报的情况;您应该已经知道按下按钮是否会引起警报。如果它执行的操作超出了您的预期,那将是一个例外情况,并且应引发异常。

No, you're doing things the way the library expects you to. However, one of the principles of the library is that you should always know what to expect of your automation code. That means you shouldn't run into an instance where the button "may or may not" cause an alert; you should already know whether pressing the button will cause an alert or not. If it does something other than what you expect, that's an exceptional condition, and an exception should be thrown.

这篇关于WebDriver-如何验证是否存在警报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆