2011-11-23 14 views
34

Desidero rilevare se un avviso è spuntato o meno. Attualmente sto usando il seguente codice:Gestione degli avvisi in Selenium WebDriver (selenium 2) con Java

try { 
     Alert alert = webDriver.switchTo().alert(); 

     // check if alert exists 
     // TODO find better way 
     alert.getText(); 

     // alert handling 
     log().info("Alert detected: {}" + alert.getText()); 
     alert.accept(); 
    } catch (Exception e) { 
    } 

Il problema è che se non v'è alcun allarme sullo stato attuale della pagina web, attende per un determinato periodo di tempo fino a quando viene raggiunto il timeout, poi getta un eccezione e quindi le prestazioni sono davvero pessime.

C'è un modo migliore, forse un gestore di eventi di avviso che posso utilizzare per gli avvisi che si verificano dinamicamente?

risposta

69

Questo è ciò che ha funzionato per me usando Wait esplicita da qui WebDriver: Advanced Usage

public void checkAlert() { 
    try { 
     WebDriverWait wait = new WebDriverWait(driver, 2); 
     wait.until(ExpectedConditions.alertIsPresent()); 
     Alert alert = driver.switchTo().alert(); 
     alert.accept(); 
    } catch (Exception e) { 
     //exception handling 
    } 
} 
+1

Questo è interessante e potrebbe fare il trucco. È possibile impostare il timeout del metodo di attesa a zero? – Alp

0

Si potrebbe provare

try{ 
     if(webDriver.switchTo().alert() != null){ 
      Alert alert = webDriver.switchTo().alert(); 
      alert.getText(); 
      //etc. 
     } 
    }catch(Exception e){} 

Se questo non dovesse funzionare, si potrebbe provare scorrendo tutte le maniglie delle finestre e vedere se esiste l'avviso. Non sono sicuro che l'avviso si apra come una nuova finestra utilizzando il selenio.

for(String s: webDriver.getWindowHandles()){ 
//see if alert exists here. 
} 
+0

Purtroppo, entrambe le soluzioni non funzionano. Il primo è lento quanto il mio codice perché crea già un'eccezione durante il timeout. Il secondo non riconosce gli avvisi. – Alp

+0

Scusa, valeva la pena un colpo lol. –

+0

@Reid Mac: la tua risposta ha funzionato per me. – djangofan

15

Scrivere il seguente metodo:

public boolean isAlertPresent() { 
    try { 
     driver.switchTo().alert(); 
     return true; 
    } // try 
    catch (Exception e) { 
     return false; 
    } // catch 
} 

Ora, è possibile controllare se la segnalazione è presente o meno da utilizzando il metodo scritto sopra come di seguito:

if (isAlertPresent()) { 
    driver.switchTo().alert(); 
    driver.switchTo().alert().accept(); 
    driver.switchTo().defaultContent(); 
} 
3
 
Alert alert = driver.switchTo().alert(); 

alert.accept(); 

Si può anche rifiutare la finestra di avviso:

 

Alert alert = driver.switchTo().alert(); 

alert().dismiss(); 
2
try 
    { 
     //Handle the alert pop-up using seithTO alert statement 
     Alert alert = driver.switchTo().alert(); 

     //Print alert is present 
     System.out.println("Alert is present"); 

     //get the message which is present on pop-up 
     String message = alert.getText(); 

     //print the pop-up message 
     System.out.println(message); 

     alert.sendKeys(""); 
     //Click on OK button on pop-up 
     alert.accept(); 
    } 
    catch (NoAlertPresentException e) 
    { 
     //if alert is not present print message 
     System.out.println("alert is not present"); 
    } 
Problemi correlati