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") 

Wednesday, March 6, 2013

Calling External File and Application in QTP



There are various ways through with we can call files and applications in QTP. Below are the four very simple ways:

Method 1. Use "InvokeApplication" command - Invokes an executable application.
Note: In most situations, you should use a SystemUtil.Run statement to run applications or to open files in their default application. The InvokeApplication statement is supported primarily for backward compatibility.
Example: 
The following example uses the InvokeApplication function to open Internet Explorer.

InvokeApplication "E:\Program Files\Plus!\Microsoft Internet\IEXPLORE.EXE".

Method 2. The use of "SystemUtil.Run" command - Runs a file or application.
When specifying a non-executable file, the file opens in the associated application.
Note: A SystemUtil.Run statement is automatically added to your test when you run an application from the Start menu or the Run dialog box while recording a test.
Tip: You can also use this method to perform operations on the specified file, similar to the usage of the Windows ShellExecute command.
You can directly run the windows applications by just giving the application name. No need to give complete path.
Example:
SystemUtil.Run “IEXPLORE.EXE”
Note – The difference between SystemUtil.Run and InvokeApplication is – InSystemUtil.Run No need to give complete path for windows shell applications. But inInvokeApplication you need to give path for each application.
Example - 
Open a Text File in the Default Text Application (Notepad)

Sub CloseDescendentProcesses_Example() 
'The following example uses the Run method to open a file named type.txt 
'in the default text application (Notepad). It then types "happy days", 
'saves the file using shortcut keys, and then closes the application.

SystemUtil.Run "C:\type.txt", "", "", "" 
Window("Text:=type.txt - Notepad").Type "happy days" 
Window("Text:=type.txt - Notepad").Type micAltDwn & "F" & micAltUp 
Window("Text:=type.txt - Notepad").Type micLShiftDwn & "S" & micLShiftUp 
Window("Text:=type.txt - Notepad").Close

End Sub

Method 3. ExecuteFile function - Executing Externally-Defined Functions from Your QTP Test scripts.
If you decide not to associate a function library (any VBScript file) with a test, but do want to be able to call its functions, subroutines, and so forth from an action in your test or from another function library, you can do so by inserting an ExecuteFile statement in your action.
When you run your test, the ExecuteFile statement executes all global code in the function library making all definitions in the file available from the global scope of the action's script.
Note: You cannot debug a file that is called using an ExecuteFile statement, or any of the functions contained in the file. In addition, when debugging a test that contains an ExecuteFile statement, the execution marker may not be correctly displayed.
Tip: If you want to include the same ExecuteFile statement in every action you create, you can add the statement to an action template.
To execute an externally-defined function:
1. Create a VBScript file using standard VBScript syntax. For more information, see the Microsoft VBScript Language Reference (Help > QuickTest Professional Help > VBScript Reference > VBScript).
2. Store the file in any folder that you can access from the computer running your test.
3. Add an ExecuteFile statement to an action in your test using the following syntax: 
ExecuteFile FileName

where FileName is the absolute or relative path of your VBScript file.
4. Use the functions, subroutines, and so forth, from the specified VBScript file as necessary in your action.

Method 4. The use of "WshShell.Exec" method - Runs an application in a child command-shell, providing access to the StdIn/StdOut/StdErr streams.
Remarks 
The Exec method returns a WshScriptExec object, which provides status and error information about a script run with Exec along with access to the StdIn, StdOut, and StdErr channels. The Exec method allows the execution of command line applications only. The Exec method cannot be used to run remote scripts. Do not confuse the Exec method with the Execute method (of the WshRemote object).

Example 
The following example demonstrates the basics of the Exec method.

[VBScript] 
Dim WshShell, oExec 
Set WshShell = CreateObject("WScript.Shell")

Set oExec = WshShell.Exec("calc")
Do While oExec.Status = 0 
WScript.Sleep 100 
Loop

WScript.Echo oExec.Status 
[JScript] 
var WshShell = new ActiveXObject("WScript.Shell"); 
var oExec = WshShell.Exec("calc");

while (oExec.Status == 0) 
{ 
WScript.Sleep(100); 
}

WScript.Echo(oExec.Status);

Method 5


Dim IE
Set IE= CreateObject("InternetExplorer.Application")

IE.Visible = True
IE.Naviagte = "URL"