Nice programing

WebDriver를 사용하여 경고가 있는지 확인하는 방법은 무엇입니까?

nicepro 2020. 11. 20. 09:39
반응형

WebDriver를 사용하여 경고가 있는지 확인하는 방법은 무엇입니까?


WebDriver에서 Alert가 있는지 확인해야합니다.

때로는 경고가 표시되지만 때로는 나타나지 않습니다. 먼저 경고가 있는지 확인해야합니다. 그런 다음이를 수락하거나 무시할 수 있습니다. 그렇지 않으면 경고를 찾을 수 없습니다.


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

여기에서 링크를 확인하세요. https://groups.google.com/forum/?fromgroups#!topic/webdriver/1GaSXFK76zY


다음 (C # 구현이지만 Java에서 유사 함)을 사용하면 WebDriverWait객체 를 생성하지 않고 예외없이 경고가 있는지 확인할 수 있습니다 .

boolean isDialogPresent(WebDriver driver) {
    IAlert alert = ExpectedConditions.AlertIsPresent().Invoke(driver);
    return (alert != null);
}

ExpectedConditionsalertIsPresent () 사용하는 것이 좋습니다 . ExpectedConditions는 ExpectedCondition 인터페이스에 정의 된 유용한 조건을 구현하는 래퍼 클래스입니다 .

WebDriverWait wait = new WebDriverWait(driver, 300 /*timeout in seconds*/);
if(wait.until(ExpectedConditions.alertIsPresent())==null)
    System.out.println("alert was not present");
else
    System.out.println("alert was present");

예외를 잡는 driver.switchTo().alert();것이 너무 느립니다 Firefox(FF V20 & selenium-java-2.32.0) .`

그래서 다른 방법을 선택합니다.

    private static boolean isDialogPresent(WebDriver driver) {
        try {
            driver.getTitle();
            return false;
        } catch (UnhandledAlertException e) {
            // Modal dialog showed
            return true;
        }
    }

그리고 대부분의 테스트 케이스가 대화 상자가없는 경우 더 나은 방법입니다 (예외 발생 비용이 많이 듭니다).


ExpectedConditionsalertIsPresent () 사용하는 것이 좋습니다 . ExpectedConditions는 ExpectedCondition 인터페이스에 정의 된 유용한 조건을 구현하는 래퍼 클래스입니다 .

public boolean isAlertPresent(){
    boolean foundAlert = false;
    WebDriverWait wait = new WebDriverWait(driver, 0 /*timeout in seconds*/);
    try {
        wait.until(ExpectedConditions.alertIsPresent());
        foundAlert = true;
    } catch (TimeoutException eTO) {
        foundAlert = false;
    }
    return foundAlert;
}

참고 : 이것은 nilesh의 답변을 기반으로하지만 wait.until () 메서드에 의해 발생하는 TimeoutException을 포착하도록 조정되었습니다.


이 코드는 경고가 있는지 여부를 확인합니다.

public static void isAlertPresent(){
    try{
    Alert alert = driver.switchTo().alert();
    System.out.println(alert.getText()+" Alert is Displayed"); 
    }
    catch(NoAlertPresentException ex){
    System.out.println("Alert is NOT Displayed");
    }
    }

public boolean isAlertPresent () {

try 
{ 
    driver.switchTo().alert(); 
    system.out.println(" Alert Present");
}  
catch (NoAlertPresentException e) 
{ 
    system.out.println("No Alert Present");
}    

}


ExpectedConditions 더 이상 사용되지 않으므로 :

        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
        wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.AlertIsPresent());

C # Selenium 'ExpectedConditions가 사용되지 않음'


public static void handleAlert(){
    if(isAlertPresent()){
        Alert alert = driver.switchTo().alert();
        System.out.println(alert.getText());
        alert.accept();
    }
}
public static boolean isAlertPresent(){
      try{
          driver.switchTo().alert();
          return true;
      }catch(NoAlertPresentException ex){
          return false;
      }
}

참고URL : https://stackoverflow.com/questions/11467471/how-to-check-if-an-alert-exists-using-webdriver

반응형