Friday, December 9, 2011

Function in VBScript


Here I have tried to throw light on the functions, arguments and return values which may be helpful for the beginners of QTP and VB Script.
1. What is function in VB Script
Ans: Function is a piece of code with specific keywords like
Function funtionName() 
End Function
Within these two key words we can write any number of lines of code to perform actions. Then we can call entire function with its function name wherever it is necessary. We can pass one or more than one variable/s as argument/s inside the function and can get one or more than one return value/s from it.
2. How to pass two arguments?
Ans: Let's say write a function to add two numbers
Dim num1,num2
num1=10
num2=20
Call addTwoNums(num1,num2)
Function addTwoNums(inNum1,inNum2)
   Dim outResult
   outResult=inNum1+inNum2
   msgbox outResult
End Function
This function works fine and gives result as 30 as perfect as the code is. But if you observe the function, result is still inside the function. If you just want to add two numbers and end up the looking at result, then this function is good enough. But what if you want to use the result of addTwoNums in another function to subtract?
To get the result outside we have following ways:
a. Define variable outResult as global variable. Means define outResult out side of function
Dim num1,num2
Dim outResult
num1=10
num2=20
Call addTwoNums(num1,num2)
Function addTwoNums(inNum1,inNum2)  
   outResult=inNum1+inNum2
End Function
msgbox outResult
b. In some cases we don't want to define outResult as global variable. Say there is another function subtractTwoNums to subtract and result is getting stored in outResult, if we execute first addTwoNums and next subtractTwoNums, outResult will be having result of subtractTwoNums. But because of some issue subtractTwoNums did not get execute even after calling it, then outResult will be having result of addTwoNums. So it misleads.
In such cases we can re-assign outResult to function it self. But while calling the function, we have to use a variable instead of Call keyword. Here is the code:
Dim num1,num2
num1=10
num2=20
rc= addTwoNums(num1,num2)
Function addTwoNums(inNum1,inNum2)  
   Dim outResult
   outResult=inNum1+inNum2
   addTwoNums=outResult
End Function
msgbox rc
c.Is there any other method without caring defining variable as public or private and not assigning result to function?
Ans: In such cases we have to add another argument and assign result to it. Here is the code:

Dim num1,num2
num1=10
num2=20
Call addTwoNums(num1,num2,result)
Function addTwoNums(inNum1,inNum2,outResult)
   outResult=inNum1+inNum2
End Function
msgbox result
NOTE: Don't define outResult as variable in side the function because outResult is argument.
d. Is there any way to make one function perform multiple actions like add,subtract,division and multiply?
Ans: Yes, here is the function:

Dim num1,num2,outResult
num1=10
num2=20
Call addTwoNums(num1,num2,result1,result2,result3,result4)
Function addTwoNums(inNum1,inNum2,outResult1,outResult2,outResult3,outResult4)
   'add
   outResult1=inNum1+inNum2
   'subtract
   outResult2=inNum1-inNum2
   'multiplication
   outResult3=inNum1*inNum2
   'devision
   outResult4=inNum1/inNum2
End Function
msgbox result1
msgbox result2
msgbox result3
msgbox result4
OMG so many arguments, if I want to pass 100 numbers and perform 200 actions how big my function's argument list LOL ;-) ;-) ;-)
Here is the solution to such scenarios. Use array function and make it every thing sweet and short.
Dim num(10),result(10)
num(1)=10
num(2)=20
Call addTwoNums(num,result)
Function addTwoNums(inNum,outResult)
   'add
   outResult(0)=inNum(1)+inNum(2)
   'subtrac
   outResult(1)=inNum(1)-inNum(2)
   'multiplication
   outResult(2)=inNum(1)*inNum(2)
   'devision
   outResult(3)=inNum(1)/inNum(2)
End Function

msgbox result(0)
msgbox result(1)
msgbox result(2)
msgbox result(3)
NOTE: Don't forget to define the variables as array before calling  the function (num(10),result(10)), you can define more space in array not less.

Thursday, November 10, 2011

QTP 11,Windows 7,IE 8 and IE 9


Here is the collected(from http://www.learnqtp.com/forums and
http://www.sqaforums.com)  information about QTP 10.0, 11.0 with OS
Windows 7 and IE 8/9.

Tool: QTP 11.0
OS:  Windows 7
Browser: IE 9
General Issues: Objects are not recognised and IE gets hanged.
Solutions: Install patch QTPWEB_00078

Tool: QTP 11.0
OS:  Windows 7
Browser: IE 8

General Issues: Objects are not recognised for Web Application.
Solutions:
1. Right Click and run QTP as an Administrator and then try recording
or recognizing objects.
2. Open QTP, go to Record and Run Settings and navigate to the Web tab
and change the settings from a specific browser settings to 'Record on
any open browser' setting and then try recording or recognizing
objects.
Reason for not working:
a. QTP needs Admin level privileges to some of its files/registry
options to perform its functionality.
b. Windows 7 has bought in levels of security, your User ID on your
test PC might NOT have had all the admin level privileges.
c. To do one time setting by right clicking on QTP desktop shortcut
and select Properties. Navigate to Compatibility Tab and check the
option Run as Administrator.

Tool: QTP 10.0
OS:  Windows 7
Browser: IE 8
General Issues: Objects are not recognised.
Solutions:
1. Install following two patches
   a. For IE8.0 - qtpweb_00037.exe.
   You can download it from:
   http://support.openview.hp.com/selfsolve/document/KM743912
   Here is the description of the patch:
    ========================================================================
   HP QuickTest Professional 10.00 Patch QTPWEB_00037
   (c) Copyright 2009 Hewlett-Packard Development Company, L.P.
   ========================================================================
   QTPWEB_00037 - Support for Internet Explorer 8
   ========================================================================
   This patch provides official support for testing Web applications
in Internet Explorer 8.
   It includes support for working with Internet Explorer 8 on all
operating systems that QuickTest Professional supports.
   (For the complete list, see the QuickTest Professional 10.00 Readme.)

   b. For Windows 7.0 - qtp_00644.exe.
2. Try the below settings:
   a. IE: Select the “Enable 3rd party extensions” option under Tools
–> Advanced section in IE
   b. Open Control Panel, choose User Accounts and Family Safety >>
System and Security Settings>>Change User Account Control
settings>>Set the control to ‘Never notify’ and restart the computer.

Tool: QTP 9.5
OS:  Windows 7
Browser: IE 8

General Issues: IE 8 gets crash.
Solutions: Uncheck 'ActiveX add-in' when launching QTP 9.5 will work
file with IE 8.
Note: Official Support for IE8 is QTP 10.0 with Patch QTPWEB_00037.

Monday, November 7, 2011

ExecuteFile



Have you used ExecuteFile in your Action/Script to call Lib files?
There is another option in QTP to call functions and Sub Routines via Associate Resources Files tab. What is the difference between both of them. Here is my view.

Say there is a main script which calls sub scripts they cover 10 scenarios. For 6 scenarios the variables, constants, functions and sub routines are required from 'text1.vbs,text2.vbs,text3.vbs,text4.vbs and text5.vbs'  and for other 4 the variables, constants, functions and sub routines written in 'GeneralLib1.vbs,GeneralLib2.vbs,GeneralLib3.vbs,GeneralLib4.vbs and GeneralLib5.vbs' are required. If we use File--> Settings--> Resources to associate all 10 vbs files to main script, it will take time to read all 10 vbs and will have impact on performance of the main script. ExecuteFile statement is very useful in this case. By using conditions like if else statements we can call the required lib files at the required steps.
Here is the example:
If scnNumber=1 Then
   ExecuteFile "C:\Library\text1.vbs"
End if

If your location of lib files getting change offenly then use relative path as
If scnNumber=1 Then
   ExecuteFile "text1.vbs"
End if
But make sure that folder path is associated at Tools--> Options--> General --> Folders as C:\Library.
Disadvantages of ExecuteFile statement over Accosiate Lib file are break point is not going to work and execution marker will not move to the file and function which is getting executed. It will be difficult for debugging.
Here are the advantages and disadvantages of ExecuteFile statement and Associate Lib files

Advantages:
1. It can be called one or more Lib file/s in another lib files.
2. Lib files can be called at the point where it is exactly needed.
Disadvantages of ExecuteFile command
1. Will not display execution marker even after selecting Normal Mode in Tools->Options->Run
2. Will not support break Points.
3. Will not support F11 at middle of execution
4. To use F11 at middle of execution, need to click on Pause then it will support F11. But even this cursor will not display on the line number where it is getting executed for every clicks of F11.
5. It will not list associated function libraries in Resources-> Associated Function Libraries.
6. If there is a break point in a script and the statement where break point is applied is calling a function which is present in other lib files, then if user click on F11, it will move next statement but will not invoke the lib file and will not show the cursor.
7. When you run an ExecuteFile statement within an action, you can call the functions in the file only from the current action.
8. Function Generator will not list the Library Functions in Operation Drop down.

Advantages of Associate Lib files
1. Supports break points and F11 from there.
2. Easy to debug.
3. Execution Marker moves over each statement for every press of F11, we can see what the statement does.

Sunday, July 24, 2011

Send Status in gmail by VBScript


Do you forget sending the status every day after script run?
Why to worry, ask VBScript to do it. Update the status excel file by manually or by QTP, double click on the status.vbs file (copy below script and save as status.vbs) or if your lazy to double click, just create Windows Task Scheduler and set the time.
This script will send status by reading the data from excel and attaché in body of mail as text.
********************************************************************
Function GetData()
Dim x, strTemp, objExcel, objWB

Set objExcel = Wscript.CreateObject("Excel.Application") 
Set objWB = objExcel.Workbooks.Open("D:\Test.xlsx") 
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1) 

' Make Excel visible while debugging 
objExcel.Visible = True

' This is the row of our first cell. 
x = 2

dowhile objExcel.Cells(x, 1).Value <> ""
  strTemp =strTemp & objExcel.Cells(x, 1).Value &_
  Space(25)
  strTemp = strTemp & objExcel.Cells(x, 2).Value  &_
  Space(30)
  strTemp = strTemp & objExcel.Cells(x, 3).Value  & vbCRLF
  x = x + 1
loop

' This will prevent Excel from prompting us to save the workbook. 
objExcel.ActiveWorkbook.Saved = True

' Close the workbook and exit the application. 
objWB.Close
objExcel.Quit

set objWB = Nothing
set objExcel = Nothing

GetData = strTemp
EndFunction
'*******************************************
sendmail()
Sub sendmail()
  Set iMsg = CreateObject("CDO.Message")
  Set iConf = CreateObject("CDO.Configuration")

  iConf.Load -1' CDO Source Defaults
  Set Flds = iConf.Fields

  With Flds
   .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
   'Enable SSL Authentication

   .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
   'Value of 1 enables basic authnetication, 
   '2 enables NTLM Authentication, 
   '0 disables Authentication

    'Enter your gmail address from which you would like to send mail.
   .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "test@gmail.com"
   'Enter Password
   .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") =  ""
   'Enter smtpserver, if it is gmail enter as smtp.gmail.com and if it is hotmail smtp.live.com
   .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"

   .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
   'Value of 2 means send using port
   'value of 1 means send using a local SMTP server
   'value of 3 means send using Exchange Server

    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
   'The SMTP Port which must be enabled in your network by ISP or local Firewall

    .Update
  EndWith

  With iMsg
   Set .Configuration = iConf
   .To = "test1@gmail.com"
   .CC = ""
   .BCC = ""
   .From = "test@gmail.com"
   .Subject = "Status"
   strBody = "Sl.No:" & Space(15) & "Test Case Name" & Space(25) & "Status" & vbCRLF
    ' Here we call the function GetData to populate the body text. 
    strBody = strBody &  GetData

   .textBody = strBody
   '.AddAttachment "D:\Test1.txt"
   'Local path of the file to attached
   'For attaching another file, 
   'repeat the line with new path
   .Send
   MsgBox"Sent Mail"
  EndWith
EndSub
*************************************************************************** 

Send gmail and hotmail by VBscript


We have seen many scripts of sending mail using OUTLOOK by VB script. But what if the system does not have OUTLOOK installed? I thought of sending mail using gmail, but I don’t want to launch browser,login to gmail and send mail from it. Having this in mind I started googling and found nice script to send mail from gmail without much delay.  You can send such mails from hot mail as well. Interesting part is we can send mail with attachment as well. Thanks to google and thanks to learnqtp.info.
Here is the script, you can directly copy to notepad,save as .vbs and double click. Please don’t forget to enter your gmail user name and password. Enjoy QTP and VBScripting

**********************************************************************************
sendmail()
Sub sendmail()
  Set iMsg = CreateObject("CDO.Message")
  Set iConf = CreateObject("CDO.Configuration")

  iConf.Load -1' CDO Source Defaults
  Set Flds = iConf.Fields

  With Flds
   .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
   'Enable SSL Authentication

   .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
   'Value of 1 enables basic authnetication, 
   '2 enables NTLM Authentication, 
   '0 disables Authentication

    'Enter your gmail address from which you would like to send mail.
   .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "test@gmail.com"
   'Enter Password
   .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") =  ""
   'Enter smtpserver, if it is gmail enter as smtp.gmail.com and if it is hotmail smtp.live.com
   .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"

   .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
   'Value of 2 means send using port
   'value of 1 means send using a local SMTP server
   'value of 3 means send using Exchange Server

    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
   'The SMTP Port which must be enabled in your network by ISP or local Firewall

    .Update
  EndWith

  With iMsg
   Set .Configuration = iConf
   .To = "test1@gmail.com"
   .CC = ""
   .BCC = ""
   .From = "test@gmail.com"
   .Subject = "A mail from VBscript"
   .TextBody = "This is a test email sent using Gmail's SMTP Server with port 25."
   .AddAttachment"D:\Test1.txt"
   'Local path of the file to attached
   'For attaching another file, 
   'repeat the line with new path
   .Send
   MsgBox"Sent"
  EndWith
EndSub
**********************************************************************************

Wednesday, May 4, 2011

Test Parameters and Action Parameters

There are two types of parameters
1. Test Parameters.
2. Action Parameters.

Define Parameters:

If you consider actions are as pages of a book, then test is nothing but a cover pages of it. So Test Parameters are the Parameters which applies to all the action of the test. You can use the Test Parameters in any actions but before using them user has to call then in the actions. For example if a TEST has 3 actions (Action1,Action2 and Action3), if you want use Test Parameters in Action1, then you have to call them in Action1.

How to Define Test Parameters: Go to Test Settings File-->Settings--> Test Settings window will open.
Click on Parameters. Click on '+' mark, Enter the name of the Paremeter,Type, default value and description in 'Input Parameters' section.
Example:
Name: UserName_IN
Type: String
Default Value: test1
Description: Enter the User Name.
Click on Apply and OK button.
How to Use: To use Test Parameters in Actions, Action parameters must have defined. Click on 'Keyword View', right click on 'Action1' click on 'Action Properties'. On Action Properties window click on 'Parameters' tab. Define Input and output parameters (same as test parameters, name must be different).
Example:
Name: UName_IN
Type: String
Default Value: 
Description: Enter the User Name.
Click on OK button.
Again click on 'Keyword View', select 'Test Flow' from drop down present at the left top of the test. Right click on Action1, click on 'Action Call Properties', on Window click on 'Parameter Values' tab. The Action parameters will be displayed. Click under 'Value' column. Click on <#> icon. 'Value configuration Options' window will open. Click on Parameter radio button, select test parameters from drop down. Click OK, and OK. Click on Expert View and call the parameter.
msgbox Parameter("UName_IN")
Result: test1.
Note: You can change the parameter values in run time.
Click on RUN, on RUN window click on 'Input Parameters', Change the value and click OK.

You can use the TEST Parameter only in Action1, if you want to use TEST parameters in Action2 and Action3, it is necessary to define action parameters.

If you want to use only Action Parameters then not necessary to select Parameter radio button on 'Value configuration Options' window.