Monday, September 10, 2012

Selenium RC using Java Post 3 of 10

HTML Editor Sample Page

 

Selenium RC with java Post 3 of 10

In this post I’ll discuss about How to handle drop downs, Text fields ,Date pickers,  Data Grids and Paging using Selenium.

 

How to get the and set values from Text fields

 

Sel.type("id=body_body_shortNameTextBox", "manu11");

Get value from Text box

Sel.getValue("id=body_body_shortNameTextBox");

 

How to get and set values from Drop down

Can select values from 3 methods

From name of the item , ID and Index

Sel.select("body_body_countryDropDownList", "label=Singapore");

Sel.select("body_body_countryDropDownList", "ld=10");

Sel.select("body_body_countryDropDownList", "lndex=118");

 

 

Get the selected value in drop down

Sel.getSelectedLabel("body_body_countryDropDownList");

 

Sel.getSelectedId("body_body_countryDropDownList");

 

Sel.getSelectedIndex("body_body_countryDropDownList");

 

 

Data Grids

 

Following is my datagrid

Now I need to read the data grid (which has paging ) and then Click the edit button on particular row which I need to edit.

 

Now things I need to consider are

Unique name and it’s column id  which I can read the datagrid

Control name of the icon which I need to click to go to the edit page

No of paging which I need to traverse through to find the name (or element ) which I am search for

 

Now to start up I find the column name of the field I am looking for  -  it’s column 2  (I can find the column index using selenium IDE ) here it’s 1.

Element which I need to click is Body_body_activityGridView_selectImageButton (plus it has a unique id at the end and it’s increments with each row )

 

If I create a script to find the given name’s row  and click the edit image as follows

for(int row=1;row<11;row++){       

        activityName = Sel.getTable("//table[@id='body_body_activityGridView']/tbody."+ row +".1");      

       if (activityName.equals("ManujayaAutomation")) {

                  

                   Sel.click("id=body_body_activityGridView_selectImageButton_" + (row-1));

                   Sel.waitForPageToLoad("30000");

                   break;

       }

  

From this method I can find the find my activity name

Sel.getTable("//table[@id='body_body_activityGridView']/tbody."+ row +".1");

 

For getTable mehod I am passing the table id, row id and column id .

Now if my acivity name is in another page of this grid then I should handle the paging as well .

String noOfPagSentence = Sel.getText("css=span.gridPagerInfo");

    int length = noOfPagSentence.length();

    noOfPagSentence = noOfPagSentence.substring(noOfPagSentence.indexOf("of ")+3, length);

    int noOfPage = Integer.parseInt(noOfPagSentence);

    String activityName="";   

    int paging;   

    for(paging=1; paging<(noOfPage+1); paging++){

                if (activityName.equals("ManujayaAutomation")){                     

                                break;

                }

               

                if (paging>1){                             

                                  Sel.click("link=" + paging);

                                  Sel.waitForPageToLoad("30000");

                }               

        for(int row=1;row<11;row++){      

                                activityName = Sel.getTable("//table[@id='body_body_activityGridView']/tbody."+ row +".1");

                 if (activityName.equals("YoosufAutomation")) {

                                Sel.click("id=body_body_activityGridView_selectImageButton_" + (row-1));

                                 Sel.waitForPageToLoad("30000");

                                  break;

                  }

        }

        if (paging == noOfPage+1){

                break;

        }

From the first 4 lines I am finding the no of pages which has in this grid

Using the first for loop I am handling the paging related things , I am traverse through each page in the grid until I find the wanted element.

 

 


HTML Code

No comments:

Post a Comment