Sunday, 3 March 2013

Robot 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_Robot()   
    {
       
        try {
            Robot robot;
            robot = new Robot();
            robot.keyPress(KeyEvent.VK_ENTER);
        } catch (AWTException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
       
               
    }
    

}

Note : I have used W3schools website for example purpose.

No comments:

Post a Comment