Friday, 3 June 2016

How to handle JavaScript alerts or window based popups using Robot keys and selenium

package rough;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.chainsaw.Main;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

import com.mysql.jdbc.Driver;

public class WindowAlertPopup_Handling {
static WebDriver driver;
public static void main(String[] args) throws AWTException, InterruptedException {
    int n = 9;
    //driver=new FirefoxDriver();
    System.setProperty("webdriver.chrome.driver","D:\\selenium_NEW_PRACTICE\\softwares\\chromedriver_win32\\chromedriver.exe");
    driver = new ChromeDriver();
    driver.get("http://www.jtable.org/");   
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    driver.findElement(By.xpath("//*[contains(text(),'Export to Excel')]")).click();
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    Thread.sleep(1000);
    /*
    //working fine using Robot keys
    Robot robot=new Robot();
    robot.keyPress(KeyEvent.VK_ALT);
     robot.keyPress(KeyEvent.VK_S);
     robot.keyRelease(KeyEvent.VK_S);
     robot.keyRelease(KeyEvent.VK_ALT);
     robot.keyPress(KeyEvent.VK_ENTER);
      System.out.println("File is downloaded");
      */
   
    //working fine using selenium
    Alert al=driver.switchTo().alert();
    String aleartText=al.getText();
    al.accept();
    System.out.println("clicking alert popup");
   

}
}

1 comment: