2016-09-22 50 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