Wednesday 2 September 2015

Selenium WebDriver and Javascripts alerts/ pop-ups

Generally JavaScript pop-ups are generated by web application and hence they can be easily controlled by the browser.         
Built into Selenium is the ability to switch to an alert window and either accept or dismiss it.
Below are the ways to accept / dismiss alert

// Here you can accept the alert and can get text of alert


    public String AcceptAlertAndGetText()  
       {  
              //Switch to alert  
              Alert alert = driver.switchTo().alert();  
               //Get text of the alert  
               String AlertText = alert.getText();  
               //Accept the alert  
               alert.accept();  
          return AlertText;  
      }  




 
//Here you can dismiss the alert and can get text of alert

 public String DismisAlertAndGetText()  
 {  
           //Switch to alert  
           Alert alert = driver.switchTo().alert();  
          //Get text of the alert  
           String AlertText = alert.getText();  
         //Dismiss the alert  
         alert.dismiss();  
      return AlertText;  
 }  


No comments:

Post a Comment