2016-09-22 51 views
0

我有以下警报。问题在于wait.until(ExpectedConditions.alertIsPresent())线路上的返回错误是UnhandledAlertException,即使我可以看到在屏幕上触发的警报。如果我删除UnhandledAlertException,那么我得到错误,我有一个UnhandledAlertException。你能否提供一个解决方案,以及一些解释,让我明白发生了什么? `在处理警报时需要帮助 - 等待警报出现并未处理警报异常

//Alert 
    Alert getAlert() { 
     try{ 
     Alert alert = driver.switchTo().alert(); 
     String alertCheck = alert.getText(); 
     this.bcase = true; 
     System.out.println(alertCheck); 
     if(alertCheck.equals("Define higher than 0 quantity for all products")){ 
      m.print("Correct error message appeared! User cannot advance further!"); 
     } else if(alertCheck.equals("Invalid Payment Document Number")){ 
      m.print("Correct error message appeared! User cannot advance further!"); 
     }else if (alertCheck.equals("Define rates for all products")){ 
      m.print("Correct error message appeared! User cannot advance further!"); 
     } else if (alertCheck.equals("Define delivery dates for all products")){ 
      m.print("Correct error message appeared! User cannot advance further!"); 
     } else if(alertCheck.equals("NOT IMPLEMENTED YET - WAITING DELIVERY DOC INPUT")){ 
      m.print("Correct error message appeared! User cannot advance further!"); 
     }else if (alertCheck.equals("You must upload an invoice file!")){ 
      m.print("Correct error message appeared! User cannot advance further!"); 
     } else if (alertCheck.equals("You must upload a payment order file!")){ 
      m.print("Correct error message appeared! User cannot advance further!"); 
     } else if (alertCheck.equals("Invalid Payment Document Date")){ 
      m.print("Correct error message appeared! User cannot advance further!"); 
     } else if (alertCheck.equals("Invalid Payment Document Number")){ 
      m.print("Correct error message appeared! User cannot advance further!");} 
     else if (alertCheck.equals("Invalid Payment Document Date")){ 
      m.print("Correct error message appeared! User cannot advance further!");} 
     else if(driver.findElement(By.xpath("//*[@class='btn pull-left btn-default input-sm'][text()='Open Order Request']")).isDisplayed()){ 
      this.bcase = false; 
      m.print("(FAIL!!!) The user can advance with unselected/wrongly completed fields, even though an error message is present! (FAIL!!!)"); 
     } else {m.print("(FAIL!!!) The user cannot advance, but there is no pop-up message informing him of the problem! (FAIL!!!)");} 
     driver.switchTo().alert().accept(); 
     return alert; 
     } catch (NoAlertPresentException e){ 
      m.print("(FAIL!!!) The user can advance with wrongly completed fields, without any error popup to inform them! (FAIL!!!)"); 
      this.bcase = false; 
      return null; 
     } catch (UnhandledAlertException f){ 
      wait.until(ExpectedConditions.alertIsPresent()); 
      Alert alert = driver.switchTo().alert(); 
      String alertCheck = alert.getText(); 
      if (alertCheck.equals("You must upload an invoice file! You must upload a payment order file!")){ 
       m.print("Correct error message appeared! User cannot advance further!"); 
      } else if(driver.findElement(By.xpath("//*[@class='btn pull-left btn-default input-sm'][text()='Open Order Request']")).isDisplayed()){ 
       this.bcase = false; 
       m.print("(FAIL!!!) The user can advance with unselected/wrongly completed fields, even though an error message is present! (FAIL!!!)"); 
      }driver.switchTo().alert().accept();return null; 
     } 
    } 

`

+0

考虑使用'之开关,而不是大'的if/else if'块。这将使代码更具可读性和可维护性。另外,这是很多样板代码'm.print(“出现正确的错误消息!用户不能进一步提升!”);'尝试在语句之前创建一个'String消息'对象并执行'm.print(message );'。 – JDelorean

+0

这个测试会发生什么'else if(driver.findElement(By.xpath(“// * [@ class ='btn pull-left btn-default input-sm'] [text()='Open Order Request' ]“))。isDisplayed()){ this.bcase = false; m.print(“(FAIL !!!)即使出现错误信息,用户也可以选择未完成/错误完成的字段!(失败!!!)”);'?因为我在alertCheck之后切换并且不是alertCheck语句。另外,我如何将异常集成到'case'场景? – Tudor

+0

您可以将非alertCheck部分放在'switch'语句的默认部分下。例外情况很好,它们应该像现在一样使用'switch'完全相同。 – JDelorean

回答

0

我认为,一个更好的方法来处理警报首先等待出现警报,而且比处理它。你可以做到这一点,与此两种方法:

public boolean checkNativeAlert() { 
    boolean exist = false; 

    try { 
     WebDriverWait wait = new WebDriverWait(webDriver, 3); 
     if(!(wait.until(ExpectedConditions.alertIsPresent()) == null)) { 
      exist = true; 
     } 
    } 
    catch (Exception ignore) {} 

    return exist; 
} 

public String getNativeTextAlert(boolean close) { 
    String text = null; 
    try { 
     Alert alert = webDriver.switchTo().alert(); 
     text = alert.getText(); 
     if (close) { 
      alert.accept(); 
     } 
    } 
    catch (Exception ignore) {} 

    return text; 
} 

处理警报与硒是有点棘手,从我的角度来看是更好地利用这个其他两个选项:

  • 更改警报模式对话框(至少在调试/测试模式)
  • 使用了AutoIt处理警报在Selenium测试
+0

在上面的例子中,我认为close有一个空值,因为我看不到它在任何地方初始化,只声明。 – Tudor

+0

close是函数的参数 – vbail