Friday, March 15, 2013

Working with WebTable

QTP – Web Tables

A Table containing a number of rows and columns

Keywords for Web Table are:
·         Rows
·         Columns
·         Cell
·         Cell Data
·         RowCount
·         ColumnCount(r)
·         GetCellData
·         GetRowWithCellText
·         CellText
·         CellTextByContent
·         TextCellExists


'**************************************************************************
'Purpose: Get  Column headers of a table & write it to a excel sheet
'**************************************************************************

DataTable.AddSheet "MySheet"
Set objWebTable=Browser("Gmail: Email from Google").Page("Gmail - Inbox").WebTable(“t")
vrRC=objWebTable.RowCount 'To get the number of rows of a table from a page
vrCC=objWebTable.ColumnCount(1) 'To get the number of columns of a table from a page
MsgBox "Row Count is   " & vrRC
MsgBox "Column Count is   " &  vrCC
For i=1 to C
      vrColName=objWebTable.GetCellData(1,i)
      MsgBox "Name of column is   "& vrColName
      DataTable.GetSheet("MySheet").AddParameter vrColName,""
Next

'***********************************************************************
'Getting Cell Data from a Web Table
'Method used is GetCellData(Row#,Col#)
'***********************************************************************
Set vrText = Browser("Gmail: Email from Google").Page("Gmail - Inbox").WebTable("t").GetCellData(1, 1)
 MsgBox "Text contains: " & vrText

Example:

Set objWebTable= Browser("Gmail: Email from Google").Page("Gmail - Inbox").WebTable("t")
For i=1 to vrRC
      vrCC=objWebTable.ColumnCount(i)
      For j=1 to vrCC
            vrCellData=objWebTable.GetCellData(i,j)
            MsgBox "Data Contains in Cell:  " & vrCellData
      Next
Next

'**************************************************************
'Getting Cell Data From Web Tables and writing to an excel
'**************************************************************

Set objWebTable= Browser("Gmail: Email from Google").Page("Gmail - Inbox").WebTable("t")
For i=1 to vrRC
      Datatable.GetSheet("MySheet").SetCurrentRow(i)
      vrCC=objWebTable.ColumnCount(i)
      For j=1 to CC
            vrCellData=objWebTable.GetCellData(i,j)
            DataTable.GetSheet("MySheet").GetParameter(j).Value=vrCellData
            MsgBox "Data Contains in Cell:  " & vrCellData
      Next
Next

'****************************************************************
'Working Gmail Web Table
'*****************************************************************

Set objWebTable=Browser("Gmail: Email from Google").Page("Gmail - Inbox").WebTable("t")
vrRC=objWebTable.RowCount 'To get the number of rows of a table from a page
vrCC=objWebTable.ColumnCount(1) 'To get the number of columns of a table from a page
  MsgBox "Row Count is   " & vrRC
  MsgBox "Column Count is   " &  vrCC

DataTable.AddSheet("MySheet")

For i=1 to vrCC
      vrColName=oTable.GetCellData(1,i)
         MsgBox "Column Name is:    "& vrColName
      DataTable.GetSheet("MySheet").AddParameter vrColName,""
Next

For i=1 to vrRC
      Datatable.GetSheet("MySheet").SetCurrentRow(i)
      vrCC=oTable.ColumnCount(i)
      For j=1 to vrCC
            vrColData=oTable.GetCellData(i,j)
            DataTable.GetSheet("MySheet").GetParameter(j).Value=vrColData
            MsgBox "Column Data     " & vrColData
      Next
Next

'**********************************************************
'Reading an excel sheet and passing cell data as a link
''**********************************************************

vrParamCount = DataTable.GetSheet("Sheet1").GetParameterCount
vrRowCount = DataTable.GetSheet("Sheet1").GetRowCount

      For j=1 to vrRowCount
            For i=1 to vrParamCount
                                                                                       vrColValue=DataTable.GetSheet("Sheet1").GetParameter(i).ValueByRow(j)
                MsgBox "Column Value:  " & vrColValue 
            Next
 Next

      For j=1 to vrRowCount
           vrRCValue=DataTable.GetSheet("MySheet").GetParameter(3).ValueByRow(j)
            MsgBox "Row Column Value    "& vrRCValue
       Next

''********************************************************************
'To get the no# of web tables existing on page
'********************************************************************
Set objWebTable=Description.Create
objWebTable("micClass").Value="WebTable"
objWebTable("html tag").Value="TABLE"
objWebTable("text").Value=".*"

Set objWebTableChild=Browser("Gmail: Email from Google").Page("Gmail - Inbox").ChildObjects(objWebTable)
vrNumOfTables=objWebTableChild.count

MsgBox "Total Number of Tables: " & vrNumOfTables
For i=0 to vrNumOfTables-1
      vrTabName=objTableChild(i).GetROProperty("name")
      vrTabCols=objTableChild(i).GetROProperty("cols")
      vrTabRows=objTableChild(i).GetROProperty("rows")
      vrTabText=objTableChild(i).GetROProperty("text")

      MsgBox "Table Name:    "& vrTabName
      MsgBox "Table Column:     "& vrTabCols
      MsgBox "Table Rows:    "& vrTabRows
      MsgBox "Tabl eText:    "& vrTabText

Next

'************************************************************
'Get Row Number with CellText
'************************************************************

'Scenario : When we want to search for Row Number for any specific text in a WebTable.

vrRowNumber = Browser("Gmail: Email from Google").Page("Gmail - Inbox").WebTable("html tag:=TABLE").GetRowWithCellText("Jackiechen")
MsgBox vrRowNumber


'************************************************************
'Miscellaneous: Undocumented QTP Web-table Methods
'************************************************************

Method 1:  CellText
This method works as same as GetCellData method.
MsgBox Browser("Gmail: Email from Google").Page("Gmail - Inbox").WebTable("t").CellText(1,1) 

Method 2:  CellTextByContext
This method returns the text within a cell delimited by a character. We can retrieve the text before (or) after the delimiter using this method.
Eg.: Consider a cell having the text “Jackie;Chen”. Here “;’ is the delimiter

Code to retrieve the text before the delimiter
MsgBox Browser("Gmail: Email from Google").Page("Gmail - Inbox").WebTable("t").CellTextByContext(1,1,"",";") 

Code to retrieve the text after the delimiter
MsgBox Browser("Gmail: Email from Google").Page("Gmail - Inbox").WebTable("t").CellTextByContext(1,1,";") 

Method 3:  TextCellExist

This method checks whether a text exists in a cell or not.
 It returns true, if the input text exists in the cell, else it returns false.
 Rather than using GetCellData method and then comparing the result with the input text, we can achieve it using this simple method.

MsgBox Browser("Gmail: Email from Google").Page("Gmail - Inbox").WebTable("t").TextCellExist(1,1,"jackie") 

1 comment: