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.

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.

AutoIT Example : File Upload

Java Code : 

import java.io.IOException;

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


public class fileUpload2 {
   
    public static void main (String args[])
    {
      
        try {
          

//Run AutoIT script through Java code 

 Runtime.getRuntime().exec("C:\\Users\\Desktop\\Senenium\\Fileupload.exe");


        } 

catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      
       //Note : Used http://cgi-lib.berkeley.edu/ex/fup.html just for example 

        WebDriver driver=new FirefoxDriver();
        driver.get("http://cgi-lib.berkeley.edu/ex/fup.html");
      
        WebElement upload_btn = driver.findElement(By.name("upfile"));
        upload_btn.click();
    }


 AutoIt v3 Script

Fileupload.au3

WinWaitActive("File Upload")
Send("C:\Users\Downloads\4.jpg")
Send("{Enter}");

Tuesday, 19 February 2013

How to Identify Web Elements through WebDriver Selenium

How to Identify Web Elements through WebDriver Selenium

It is basic things for any automation tool - How to identify Web Elements in web page....

Take some Examples / Actions / Scenarios in any Automation script :

1) Enter some value in Textbox (Input field). 
2) Click on submit button. 
3) Selection something like radio button / checkbox.
4) Click on Link .
5) Fetch some data and store in Database / file.

For any actions to follow through Automation script First You identify Web Element and in Web driver - selenium - it offers various ways to access / identify Web Element.

Consider Example....

Open Google Page > Search for string "Hello World 2013" > Click on Search Button...

Steps for Automation script to make this successful...
1. Open Google Page.
2. Identify Search Textbox.
3. Enter "Hello World 2013" in it.
4. Click on Search Button.

Let's Do it through Selenium - Web Driver -

Step 1: Open Google Page. 

( Its mentioned in my previous blog...still you can find code below) 


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;

public class allInOne {

public static void main(String args[])
{

               System.out.println("Java is Running properly if this string display on Console"); //Initiate "Test Profile of Firefox" 
ProfilesIni pi = new ProfilesIni();
FirefoxProfile fprofile = pi.getProfile("Test Profile");

//Open Google web page
WebDriver driver = new FirefoxDriver(fprofile);
driver.get("http://google.com");

}

}

Result : It will open Firefox Instance with Google page.....

Step 2: Identify Search Textbox of Google Web Page.


Please install FireBug addon in Firefox - for identify HTML source and identify HTML tag for the search Text Box.














Input HTML Tag is :

<input 
type="text" 
value="" 
autocomplete="off" 
name="q" 
class="gbqfif" 
id="gbqfq" 
style="border: medium none; padding: 0px; margin: 0px; height: auto; width: 100%;  repeat scroll 0% 0% transparent; position: absolute; z-index: 6; left: 0px; outline: medium none;" 
dir="ltr" 
spellcheck="false">

We have identify this HTML tag by Firebug.. Now its turn for Selenium - Web Driver.
Add below code...


WebElement we_Search_box = driver.findElement(By.name("q"));

Here we have used method FindElement() for identify Web Element.

Let's Prepare our Java code.

import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxProfile; import org.openqa.selenium.firefox.internal.ProfilesIni; public class allInOne { public static void main(String args[]) { System.out.println("Java is Running properly if this string display on Console"); //Initiate "Test Profile of Firefox" ProfilesIni pi = new ProfilesIni(); FirefoxProfile fprofile = pi.getProfile("Test Profile"); //Open Google web page WebDriver driver = new FirefoxDriver(fprofile); driver.get("http://google.com"); WebElement we_Search_box = driver.findElement(By.name("q")); } }

3. Enter "Hello World 2013" in it.




import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;


public class allInOne {


public static void main(String args[])
{
System.out.println("Java is Running properly if this string display on Console");


//Initiate "Test Profile of Firefox"
ProfilesIni pi = new ProfilesIni();
FirefoxProfile fprofile = pi.getProfile("Test Profile");


//Open Google web page 
WebDriver driver = new FirefoxDriver(fprofile);
driver.get("http://google.com");

WebElement we_Search_box = driver.findElement(By.name("q"));
we_Search_box.sendKeys("Hello World 2013");

}

}


Now we have identified Web Element and Entered text into it. Its turn to click on Search Button.

4. Click on Search Button.


This step is devide into 2 sub steps 4a and 4b as 

4a : Identify Submit Button.
4b : Click on Submit Button.

4a : Identify Submit Button.
Same way first identify its HTML tag of Submit button through FireBug as described above.

<button 
class="gbqfba gbqfba-hvr" 
name="btnK" 
aria-label="Google Search" 
id="gbqfba">






Thursday, 7 February 2013

First day in Selenium World............

First day in Selenium World............

Step 1: Create Java file.


public class allInOne {

}


Step 2 :  Enter below code...


public class allInOne {


public static void main(String args[])
{
System.out.println("This is my first project");
}

}


Step 3: Run this file to verify PATH variable is set properly and its working properly..















Step 4 : Result...


















Step 5: Add below code for Open Firefox instance...Save File and Run it.....


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;


public class allInOne {


public static void main(String args[])
{
System.out.println("This is my first project");

ProfilesIni pi = new ProfilesIni();
FirefoxProfile fprofile = pi.getProfile("Test Profile");


WebDriver driver = new FirefoxDriver(fprofile);
driver.get("http://google.com");

}

}

Result : It will open Firefox Instance with Google page.....

Thursday, 24 January 2013

Create New Firefox Profiles in Windows 7.

Step 1: 
Go to the Windows Start Menu and then select 'Run'

Step 2:
Enter firefox.exe -P














Step 3 : Click on the 'Create Profile...' button on the 'Firefox - Choose User Profile' window.




















Step 4: Click on 'Next >' in the 'Create Profile Wizard' window.




















Step 5: Enter new profile name in text box  'Enter new profile name' box and click on 'Finish' button.



















Step 6 : Click on Finish Button and Profile is Created.




















Step 7 : Click on Start FireFox.

It will launch Firefox instance of newly created Profile & whatever the profile which is selected would be default opened Profile.