Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

Alert & Popup Handling Using Selenium Java

Alert & Popup Handling Using Selenium Java
// Get a handle to the open alert, prompt or confirmation
Alert alert = driver.switchTo().alert();
Alert is an interface. There below are the methods that are used
//Will Click on OK button.
alert.accept();
// Will click on Cancel button.
alert.dismiss()
//will get the text which is present on th Alert.
alert.getText();
//Will pass the text to the prompt popup
alert.sendkeys();
//Is used to Authenticate by passing the credentials
alert.authenticateUsing(Credentials credentials)

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SeleniumAlertsHandling {
public String baseUrl="http://www.javascriptkit.com/javatutors/alert2.shtml";
WebDriver driver;
public void LaunchingBrowser(){
System.setProperty("webdriver.chrome.driver", 
"C:\\Users\\Sravya\\AppiumTraniningProject\\Driverexes\\chromedriver_win32\\chromedriver.exe");
driver=new ChromeDriver(); 
driver.get(baseUrl);
}
   public void alertTest() throws InterruptedException{

   driver.findElement(By.xpath("//input[@name='B2']")).click();
   Alert alert=   driver.switchTo().alert();
      if(true==isAlertPresent( )){
       Thread.sleep(5000);
  alert.accept();
  
      }
      else {       
      System.out.println("No alerts");
      }
   }  
   
   public boolean isAlertPresent( ){
   
   try{
   
Alert alert=   driver.switchTo().alert();
   return true;
   
   }catch(Exception e){
   
   return false;
   }
   }

public static void main(String[] args) throws InterruptedException {

SeleniumAlertsHandling sah=new SeleniumAlertsHandling();
         sah.LaunchingBrowser();
         sah.alertTest();
}

}

Post a Comment

0 Comments