WebDriver(Selenium2) 處理可能存在的JS彈出框


 

http://uniquepig.iteye.com/blog/1703103

在自動化測試過程中,有些情況下我們會遇到一些潛在的Javascript彈出框。(即某些條件下才會出現,不是固定出現),然后如果當這種彈出框出現,我們沒有加以處理,WebDriver將無法進行下一步的操作,並且會拋出NoAlertPresentException的異常(從2.16版本開始)。所以,使用以下方法可以處理潛在的1個alert(javascript彈出框)。 

public boolean dealPotentialAlert(WebDriver driver,boolean option) {
boolean flag = false;
try {
Alert alert = driver.switchTo().alert();
if (null == alert)
throw new NoAlertPresentException();
try {
if (option) {
alert.accept();
System.out.println("Accept the alert: " + alert.getText());
} else {
alert.dismiss();
System.out.println("Dismiss the alert: " + alert.getText());
}
flag = true;
} catch (WebDriverException ex) {
if (ex.getMessage().startsWith("Could not find"))
System.out.println("There is no alert appear!");
else
throw ex;
}
} catch (NoAlertPresentException e) {
System.out.println("There is no alert appear!");
}
return flag;
}

  方法返回值為,調用出是否出現了alert。 
參數列表中第二個參數option為期望對alert的處理方式(true:ok/false:cancle) 
在selenium2.20及以上版本中,增加了alertIsPresent方法。 也可以將這個方法替換上面的內容。用於捕獲alert。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM