【问题标题】:How to check if an alert exists using WebDriver?如何使用 WebDriver 检查是否存在警报?
【发布时间】:2012-07-13 03:01:50
【问题描述】:

我需要检查 WebDriver 中是否存在 Alert。

有时会弹出警报,但有时不会弹出。我需要先检查警报是否存在,然后我可以接受或关闭它,否则它会说:未找到警报。

【问题讨论】:

    标签: selenium webdriver alert


    【解决方案1】:
    public boolean isAlertPresent() 
    { 
        try 
        { 
            driver.switchTo().alert(); 
            return true; 
        }   // try 
        catch (NoAlertPresentException Ex) 
        { 
            return false; 
        }   // catch 
    }   // isAlertPresent()
    

    在此处查看链接https://groups.google.com/forum/?fromgroups#!topic/webdriver/1GaSXFK76zY

    【讨论】:

    • 在链接中你可以看到主人接受或关闭警报窗口
    • 下面稍有错误的答案和stackoverflow.com/questions/8244723/… 上的答案是一种更好的方法。 try/catch 模型虽然笨重,但会记录一条关于没有警报的消息。
    • ExpectedConditions.alertIsPresent() 为您提供完全相同的东西,但以一种更好的方式,并且只需一行 :)
    • ExpectedConditions 并没有比简单的 try catch 节省更多的代码。
    • 这种方法的一个问题是,在检查警报是否存在时,上下文已切换到警报。如果您没有预料到,这可能会出现问题。
    【解决方案2】:

    以下(C# 实现,但在 Java 中类似)允许您在不创建 WebDriverWait 对象的情况下确定是否存在警报。

    boolean isDialogPresent(WebDriver driver) {
        IAlert alert = ExpectedConditions.AlertIsPresent().Invoke(driver);
        return (alert != null);
    }
    

    【讨论】:

    • 谢谢。这应该是其他解决方案不解决异常的答案。
    【解决方案3】:

    我建议使用ExpectedConditionsalertIsPresent()。 ExpectedConditions 是一个包装类,它实现了ExpectedCondition 接口中定义的有用条件。

    WebDriverWait wait = new WebDriverWait(driver, 300 /*timeout in seconds*/);
    if(wait.until(ExpectedConditions.alertIsPresent())==null)
        System.out.println("alert was not present");
    else
        System.out.println("alert was present");
    

    【讨论】:

    • 在“alertIsPresent()”之后添加一个“.apply(driver)”或正确执行并使用等待
    • 我发现这会引发 TimeoutException。
    • 在给定超时时间内未满足预期条件时出现超时异常。警报是否存在?
    • @nilesh 链接已损坏
    【解决方案4】:

    我发现在Firefox (FF V20 & selenium-java-2.32.0) 中捕获driver.switchTo().alert(); 的异常非常慢。`

    所以我选择了另一种方式:

        private static boolean isDialogPresent(WebDriver driver) {
            try {
                driver.getTitle();
                return false;
            } catch (UnhandledAlertException e) {
                // Modal dialog showed
                return true;
            }
        }
    

    当您的大多数测试用例都不存在对话框时,这是一种更好的方法(抛出异常很昂贵)。

    【讨论】:

    • 当我调用你的函数的 C# 实现时,它会抛出异常,但它也会关闭警报。
    • 尽管它也关闭了alert,但目前我发现这种方法在处理alert检测时是最快的,甚至比ExpectedConditions.alertIsPresent更快
    • 这种方法的主要问题是它吃掉了警报。当警报不存在时 driver.switchTo().alert() 在 FF 62 中大约需要 6-10 毫秒
    【解决方案5】:

    我建议使用ExpectedConditionsalertIsPresent()。 ExpectedConditions 是一个包装类,它实现了ExpectedCondition 接口中定义的有用条件。

    public boolean isAlertPresent(){
        boolean foundAlert = false;
        WebDriverWait wait = new WebDriverWait(driver, 0 /*timeout in seconds*/);
        try {
            wait.until(ExpectedConditions.alertIsPresent());
            foundAlert = true;
        } catch (TimeoutException eTO) {
            foundAlert = false;
        }
        return foundAlert;
    }
    

    注意:这是基于 nilesh 的回答,但适用于捕获由 wait.until() 方法抛出的 TimeoutException。

    【讨论】:

    • 另一个注意事项:在 C# 中,这是 WebDriverTimeoutException。我想那是因为有一个 System.TimeoutException 类很容易与之混淆。
    【解决方案6】:

    ExpectedConditions 已过时,因此:

            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
            wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.AlertIsPresent());
    

    C# Selenium 'ExpectedConditions is obsolete'

    【讨论】:

      【解决方案7】:

      此代码将检查警报是否存在。

      public static void isAlertPresent(){
          try{
          Alert alert = driver.switchTo().alert();
          System.out.println(alert.getText()+" Alert is Displayed"); 
          }
          catch(NoAlertPresentException ex){
          System.out.println("Alert is NOT Displayed");
          }
          }
      

      【讨论】:

        【解决方案8】:
        public static void handleAlert(){
            if(isAlertPresent()){
                Alert alert = driver.switchTo().alert();
                System.out.println(alert.getText());
                alert.accept();
            }
        }
        public static boolean isAlertPresent(){
              try{
                  driver.switchTo().alert();
                  return true;
              }catch(NoAlertPresentException ex){
                  return false;
              }
        }
        

        【讨论】:

          【解决方案9】:

          公共布尔 isAlertPresent() {

          try 
          { 
              driver.switchTo().alert(); 
              system.out.println(" Alert Present");
          }  
          catch (NoAlertPresentException e) 
          { 
              system.out.println("No Alert Present");
          }    
          

          }

          【讨论】:

          • 添加一些更详细的描述。
          猜你喜欢
          • 2011-10-27
          • 1970-01-01
          • 1970-01-01
          • 2013-09-30
          • 1970-01-01
          • 2020-08-27
          • 1970-01-01
          • 1970-01-01
          • 2015-01-20
          相关资源
          最近更新 更多