web-dev-qa-db-fra.com

org.openqa.Selenium.UnhandledAlertException: alerte inattendue ouverte

J'utilise un pilote Chrome et j'essaie de tester une page Web. 

Normalement, tout se passe bien, mais il arrive parfois que des exceptions…

 org.openqa.Selenium.UnhandledAlertException: unexpected alert open
 (Session info: chrome=38.0.2125.111)
 (Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 x86) (WARNING: The server did not  provide any stacktrace information)
 Command duration or timeout: 16 milliseconds: null
 Build info: version: '2.42.2', revision: '6a6995d', time: '2014-06-03 17:42:30'
 System info: Host: 'Casper-PC', ip: '10.0.0.4', os.name: 'Windows 7', os.Arch: 'x86', os.version:  '6.1', Java.version: '1.8.0_25'
 Driver info: org.openqa.Selenium.chrome.ChromeDriver

Puis j'ai essayé de gérer l'alerte -

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

Mais cette fois j'ai reçu -- org.openqa.Selenium.NoAlertPresentException 

Je joins les captures d'écran de l'alerte -First Alert and by using esc or enter i gets the second alertSecond Alert

Je ne suis pas capable de savoir quoi faire maintenant. Le problème est que je ne reçois pas toujours cette exception. Et quand cela se produit, le test échoue.

12
Shantanu Nandan

J'ai eu ce problème également. Cela était dû au comportement par défaut du pilote lorsqu'il rencontrait une alerte. Le comportement par défaut a été défini sur "ACCEPT", l'alerte a donc été fermée automatiquement et switchTo (). Alert () n'a pas pu la trouver.

La solution consiste à modifier le comportement par défaut du pilote ("IGNORE") afin qu'il ne ferme pas l'alerte: 

DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
d = new FirefoxDriver(dc);

Ensuite, vous pouvez le gérer:

try {
    click(myButton);
} catch (UnhandledAlertException f) {
    try {
        Alert alert = driver.switchTo().alert();
        String alertText = alert.getText();
        System.out.println("Alert data: " + alertText);
        alert.accept();
    } catch (NoAlertPresentException e) {
        e.printStackTrace();
    }
}
21
RotS

Vous pouvez utiliser la fonctionnalité Wait dans Selenium WebDriver pour attendre une alerte et l'accepter une fois qu'elle est disponible.

En C # - 

public static void HandleAlert(IWebDriver driver, WebDriverWait wait)
{
    if (wait == null)
    {
        wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
    }

    try
    {
        IAlert alert = wait.Until(drv => {
            try
            {
                return drv.SwitchTo().Alert();
            }
            catch (NoAlertPresentException)
            {
                return null;
            }
        });
        alert.Accept();
    }
    catch (WebDriverTimeoutException) { /* Ignore */ }
}

Son équivalent en Java -

public static void HandleAlert(WebDriver driver, WebDriverWait wait) {
    if (wait == null) {
        wait = new WebDriverWait(driver, 5);
    }

    try {
        Alert alert = wait.Until(new ExpectedCondition<Alert>{
            return new ExpectedCondition<Alert>() {
              @Override
              public Alert apply(WebDriver driver) {
                try {
                  return driver.switchTo().alert();
                } catch (NoAlertPresentException e) {
                  return null;
                }
              }
            }
        });
        alert.Accept();
    } catch (WebDriverTimeoutException) { /* Ignore */ }
}

Il attendra 5 secondes jusqu'à ce qu'une alerte soit présente. Vous pouvez intercepter l'exception et la gérer si l'alerte attendue n'est pas disponible.

2
Sudara

Votre commutateur doit-il alerter dans un bloc try/catch? Vous pouvez également ajouter un délai d’attente pour voir si l’alerte s’affiche après un certain délai.

try {
    // Add a wait timeout before this statement to make 
    // sure you are not checking for the alert too soon.
    Alert alt = driver.switchTo().alert();
    alt.accept();
} catch(NoAlertPresentException noe) {
    // No alert found on page, proceed with test.
}
1
shri046

Après avoir cliqué sur l'événement, ajoutez le code ci-dessous à gérer.

    try{
         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
       }
 catch (org.openqa.Selenium.UnhandledAlertException e) {                
         Alert alert = driver.switchTo().alert(); 
         String alertText = alert.getText().trim();
         System.out.println("Alert data: "+ alertText);
         alert.dismiss();}

... faire d'autres choses driver.close ();

0
Kanchari Srikanth
DesiredCapabilities firefox = DesiredCapabilities.firefox();
firefox.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);

Vous pouvez utiliser UnexpectedAlertBehaviour.ACCEPT ou UnexpectedAlertBehaviour.DISMISS

0
user5495209

J'étais confronté au même problème et j'ai apporté les modifications ci-dessous.

try {
    click(myButton);
} catch (UnhandledAlertException f) {
    try {
        Alert alert = driver.switchTo().alert();
        String alertText = alert.getText();
        System.out.println("Alert data: " + alertText);
        alert.accept();
    } catch (NoAlertPresentException e) {
        e.printStackTrace();
    }
}

Cela a fonctionné étonnamment.

0
Sobhit Sharma