Showing posts with label VBScript. Show all posts
Showing posts with label VBScript. Show all posts

Saturday, December 10, 2011

For...Each Statement in HP QTP

For...Each Statement
A For...Each loop instructs QuickTest to perform one or more statements for each element in an array or an object collection. It has the following syntax:

For Each item In array
    statement
Next

Item  Description
item  A variable representing the element in the array. 
array  The name of the array. 

statement  A statement, or series of statements, to be performed during the loop. 
The following example uses a For...Each loop to display each of the values in an array:

MyArray = Array("one","two","three","four","five")
For Each element In MyArray
    msgbox element
Next


Note: During a run session, if a For Each statement iterates on the Parameter Definitions collection, the run may fail if the collection was retrieved directly before using the For Each statement. To prevent this, use other VBScript loop statements, such as For or While.

Do...Loop Statement in VB Script

Do...Loop Statement
The Do...Loop statement instructs QuickTest to perform a statement or series of statements while a condition is true or until a condition becomes true. It has the following syntax:

Do [{while} {until} condition]
    statement
Loop


Item           Description  
Condition     A condition to be fulfilled.  
Statement     A statement or series of statements to be performed during the loop. 

In the following example, QuickTest calculates the factorial value of the number of passengers using the Do...Loop:

passengers = Browser("Mercury Tours").Page("Find Flights").WebEdit("numPassengers").GetROProperty("value")
total = 1
i = 1
Do while i <= passengers
     total = total * i
     i = i + 1
Loop
MsgBox "!" & passengers & "=" & total  

Variables in VBScript

Variables in VBScript

You can specify variables to store test objects or simple values in your test or function library. When using a variable for a test object, you can use the variable instead of the entire object hierarchy in other statements. Using variables in this way makes your statements easier to read and to maintain.
To specify a variable to store an object, use the Set statement, with the following syntax:
Set ObjectVar = ObjectHierarchy
In the example below, the Set statement specifies the variable UserEditBox to store the full Browser.Page.WebEdit object hierarchy for the username edit box. The Set method then enters the value John into the username edit box, using the UserEditBox variable:
Set UserEditBox = Browser("Mercury Tours").Page("Mercury Tours").WebEdit("username")
UserEditBox.Set "John"
Note: Do not use the Set statement to specify a variable containing a simple value (such as a string or a number). The example below shows how to define a variable for a simple value:

MyVar = Browser("Mercury Tours").Page("Mercury Tours").WebEdit("username").GetTOProperty("type")
You can also use the Dim statement to declare variables of other types, including strings, integers, and arrays. This statement is not mandatory, but you can use it to improve the structure of your test or function library. In the following example, the Dim statement is used to declare the passengers variable, which can then be used in different statements within the current action or function library:
Dim passengers
passengers = Browser("Mercury Tours").Page("Find Flights").WebEdit("numpassengers").GetROProperty("value")

Saturday, December 3, 2011

How to export data from database table to Excel sheet and Datatable

How to export data from database table to Excel sheet and Datatable

‘  Adding NewDataSheet in Run Time
DataTable.AddSheet “MySheet”

‘ ‘  Adding Column name in Run time
DataTable.GetSheet(“MySheet”).AddParameter “bname”,” “
‘ Create DataBase Connection
Set objCon = CreateObject(“ADODB.Connection”)
objCon.Open”Provider=SqlOledb.1;Server=sys;uid=sa;pwd=;database=db1″
‘ (OR)  Open DataBase Connection

objCon.Open”Provider=Microsoft.Jet.Oledb.4.0;Data Source=D:\NewDB.mdb”

‘  Creating Record set for DataSet
Set objRs=CreateObject(“adodb.recordset”)

‘ Opening RecodSet form DataBase( storing the values in recordsetb )
objRs.open”select * from Emp”,objCon

‘ Loop for Getting DataBase Column name
For intCount = 1 to objRs.Fields.Count-1
i = 1

‘ column name in run time data sheet using add parameter method
DataTable.GetSheet(“MySheet”).AddParameter objRs.Fields(intCount).Name,” “
‘ Loop for Checking end of the Recod
While objRs.eof<>true
‘  Seting the current row
DataTable.SetCurrentRow(i)
‘   inc’ents for data table row
i=i+1
‘  Assigning the DataBase Values into DataTable
DataTable.Value(objRs.Fields(intCount).Name,”MySheet”) = objRs.Fields(intCount).Value
‘’ ——->  moving the record  pointed to next record
objRs.MoveNext
Wend
‘  moving the record  pointed to First record
objRs.MoveFirst
Next
‘ Exporting the results in local
DataTable.ExportSheet”D:\HPQTPForum.xls”,3

Tuesday, November 22, 2011

QTP Script for Application Launch

QTP Script for Application Launch
Ways to launch your application

Four different ways to launch your application are mentioned below:

1) SystemUtil.Run

SystemUtil.Run ( FileName, Parameters, Path, Operation )

FileName – The name of the file you want to run.

Parameters – If the specified FileName is an executable file, use the Parameters argument to specify any parameters to be passed to the application.

Path – The default directory of the application or file.

Operation – The action to be performed. If this argument is blank (“”), the open operation is performed.

The following operations can be specified for the operation argument of the SystemUtil.Run method:

open – Opens the file specified by the FileName parameter. The file can be an executable file, a document file, or a folder. Non-executable files are open in the associated application.

edit – Launches an editor and opens the document for editing. If the FileName argument does not specify an editable document file, the statement fails.

explore – Explores the folder specified by the FileName argument.

find – Initiates a search starting from the specified folder path.

print – Prints the document file specified by the FileName argument. If the specified file is not a printable document file, the statement fails.

Example:SystemUtil.Run “D:\My Music\Breathe.mp3?,”",”D:\My Music\Details”,”open”

2) InvokeApplication

This command is now mainly used for the backward compatability ie to use with the lower versions(below QTP 6.0) of QTP.

InvokeApplication(“Full URL as Parameter”)

Example:InvokeApplication “C:\Program Files\Internet Explorer\IEXPLORE.EXE http://www.yahoo.com”

3) VBscript to invoke application

1. Create a “WScript.shell” object.

2. Use the “run” object to launch the application. If the path to your executable contains spaces, use Chr(34) to ensure the path is contained within double quotes.

3. When done, set the shell object to nothing to release it.

Example:

Dim oShellSet oShell = CreateObject (“Wscript.shell”)’

Example 1 – run a batch file:oShell.run “F:\jdk1.3.1\demo\jfc\SwingSet2.bat”‘

Example 2 – run a Java jar file:oShell.run “java -jar F:\jdk1.3.1\demo\jfc\SwingSet2\SwingSet2.jar”‘

Example 3 – launch Internet Explorer:oShell.Run Chr(34) & “C:\Program Files\Internet Explorer\IEXPLORE.EXE” & Chr(34)Set oShell = Nothing


4) Trivial but useful method

If nothing works out you might try this

You can use the Start -> Run dialog of Windows.

1. Add the Windows Start button to the Object Repository using the “Add Objects” button in Object Repository dialog.

2. Open the Run dialog (Start -> Run), and learn the “Open” edit field and the “OK” button into the Object Repository.

3. Switch to the Expert View, and manually add the lines to open the Run dialog.

Example:Window(“Window”).WinButton(“Button”).ClickWindow(“Window”).Type(“R”)

4. Manually enter the lines to enter the information to launch the application, and click the “OK” button of the Run dialog.

Example:

Dialog(“Run”).WinEdit(“Open:”).Type “c:\WINNT\system32\notepad.exe”

Dialog(“Run”).WinButton(“OK”).Click

MsgBox Function in VB Scripts

MsgBox Function
Description
Displays a dialog box containing a message, buttons, and optional icon to the user. The action taken by the user is returned by the function as an integer value.

Syntax
MsgBox(prompt, buttons, title, helpfile, context)

Parameter  Description

prompt         The text of the message to display in the message box dialog.
buttons     The sum of the Button, Icon, Default Button, and Modality constant values.
title          The title displayed in the Title-bar of the message box dialog.
helpfile     An expression specifying the name of the help file to provide help functionality for the dialog.
context      An expression specifying a context ID within helpfile.

Notes
  prompt can contain approximately 1,000 characters, including carriage return characters such as the built-in vbCrLf or vbNewLine constants.
  In order to divide prompt onto multiple lines, you can use any of the vbCr, vbLf, vbCrLf, or vbNewLine constants. For example:
sMsg = "This is line 1" & vbNewLine & "This is line 2"
  If the helpfile parameter is provided, the context parameter must also be provided, and vice versa. 
  When both helpfile and context are passed to the MsgBox function, a Help button is automatically placed on the MsgBox dialog, allowing the user to
click and obtain context-sensitive help. 
  If you omit the buttons argument, the default value is 0; VBScript opens an application modal dialog containing only an OK button. 
  The following intrinsic constants can be added together to form a complete buttons argument

InputBox Function VB Script

InputBox Function
Description
The InputBox function displays a dialog box containing a label, which prompts the user about the data you expect them to input, a text box for entering the data, an OK button, a Cancel button, and optionally, a Help button. When the user clicks OK, the function returns the contents of the text box

Syntax
InputBox(prompt,title,default,xpos, ypos, helpfile, context)
Arguments
Constant  Description
prompt The message in the dialog box.
title  The title-bar of the dialog box.
default  String to be displayed in the text box on loading.
xpos  The distance from the left side of the screen to the left side of the dialog.
ypos  The distance from the top of the screen to the top of the dialog box.
helpfile  The Help file to use if the user clicks the Help button on the dialog box.
context  The context number to use within the Help file specified in helpfile.

Notes
1.  InputBox returns a string containing the contents of the text box from the
Input-box dialog.
2.  If the user clicks Cancel, a zero-length string (" ") is returned.
3.  prompt can contain approximately 1,000 characters, including nonprinting
characters like the intrinsic vbCrLf constant. 
4.  If you don't use the default parameter to specify a default entry for the text
box, the text box is shown empty; a zero-length string is returned if the
user doesn't enter anything in the text box prior to clicking OK. 
5.  xpos and ypos are specified in twips. A twip is a device-independent unit of
measurement that equals 1/20 of a point or 1/1440 of an inch. 
6.  If the xpos parameter is omitted, the dialog box is centered horizontally. 
7.  If the ypos parameter is omitted, the top of the dialog box is positioned
approximately one-third of the way down the screen. 
8.  If the helpfile parameter is provided, the context parameter must also be
provided, and vice versa. 
9.  In VBScript, when both helpfile and context are passed to the InputBox
function, a Help button is automatically placed on the InputBox dialog,
allowing the user to click and obtain context-sensitive help.

sString = InputBox("Enter it now", , "Something", ,  , "help.hlp", 321321)

Example
The following example uses the InputBox function to display an input box and
assign the string to the variable Input:

Option Explicit
Dim sPrompt, sTitle, sHelpFile, sRes
Dim vDef
Dim nYPos, nXPos, nContext
sPrompt = "Where do you live?" : sTitle = "Personal Data"
vDef = "Israel"
nXPos = 100 : nYPos = 100 : nContext = 1001
sHelpFile = "C:\WINDOWS\system32\winhelp.hlp"
sRes = InputBox(sPrompt, sTitle, vDef, nXPos, nYPos, sHelpFile, nContext)