Sunday, 3 March 2013

Alert Example

Java code 

import java.awt.AWTException;

import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.Iterator;
import java.util.List;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class alert {

    public static void main (String args[])
    {
       
        try
        {
   
            WebDriver driver  = new FirefoxDriver();
            driver.get("http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert");
           
            driver.switchTo().frame("viewIFRAME");
           
            List<WebElement> input_buttons = driver.findElements(By.tagName("input"));
           
            Iterator<WebElement> i = input_buttons.iterator();
           
              while(i.hasNext())
              {
                  WebElement _temp_input_button = i.next();
                 
                 
                      String str_type = _temp_input_button.getAttribute("type");
                    String str_value = _temp_input_button.getAttribute("value");
                    System.out.println("Type is :"+ str_type + " And Value is :"+str_value);
                    if ( str_type.trim().contentEquals("button") && str_value.trim().contentEquals("Show alert box"))
                    {
                        System.out.println("I am in block for clcik");
                       
                        _temp_input_button.click();
           
                        call_Robot();
                       
                    }       
                }       
        }
        catch(Exception e)
        {
            System.out.println(e.fillInStackTrace().toString());
        }
    }   
 

    public static void call_alert(WebDriver driver)
    {
           
        Alert alert = driver.switchTo().alert();
        System.out.println(alert.getText());
        alert.accept();
       
    }
   
}

Note : I have used W3school for example purpose.

1 comment:

  1. Nice blog..very helpful..

    Can you please provide some code for reporting API / Action class / Page factory...

    ReplyDelete