Showing posts with label Coding. Show all posts
Showing posts with label Coding. Show all posts

Tuesday, 31 March 2015

Variables in QTP scripting

There are 3 types of variable declarations in QTP /

1. Dim:
Variables declared by dim statement are available for the current context only (Script/Procedure)

2. Public :
Variables declared by public statement are available to all the scripts and the procedures

3. Private
Variable declared by the private statement are available only to the script/file in which it is declared.

Thursday, 5 September 2013

Excel Handling in QTP

Hi Folks,
Handling Excel file is very impotant aspect of QTP coding .It is used in parameterization.
I am describing the process in below steps


1. Reference Object:     

 First step of excel handling is to create a reference object, as per below


Set myexcelObj=CreateObject("excel.application")

Now the myexcelObj is not actully holding anything , it is even not pointing to any excel file yet


2.Linking a workbooks(Excel File):

Second Step is to tell your excel reference object that it is gonna point to an excel file(Workbook, both is same thing).so our code will be as below 
.Please note that myexcelObj can point(Handle) more than 1 excel file.



Set myexcelObj=CreateObject("excel.application")
Set myexcelWorkBook =myexcelObj.workbooks.open(FilePATH)

3. Pointing to Worksheet:

Now the myexcelWorkbook object will point to the actual excel file , so the next step is to point the worksheet in that particular file.We can point the worksheet by index or by the sheetname as below

Set myexcelObj=CreateObject("excel.application")
Set myexcelWorkBook =myexcelObj.workbooks.open(FilePATH)
Set myexcelWorksheet=myexcelworkBook.worksheets(SheeetName)

4. Getting the Data:

Once we have reference of the workSheet , getting the data is quite easy .

Set myexcelObj=CreateObject("excel.application")
Set myexcelWorkBook =myexcelObj.workbooks.open(FilePATH)
Set myexcelWorksheet=myexcelworkBook.worksheets(SheeetName)

exceldata=myexcelWorksheet.cells(1,"A").value
             Or
exceldata=myexcelWorksheet.cells(1,1).value
 
 
Now we have received the data as "exceldata" .so the final code is as shown above
Hope this helped.






Monday, 26 August 2013

How to use Increment/Decrement Operator feature in QTP using VBScript

Hi friends, I have been involved with Software Testing and QTP automation for almost 3 years , and I have noticed that in Vb scripting we don't use increment and decrement operator.

We don't have any language support form VB Script itself for increment/decrement operator.It means we cant use below

i=5 
j=i++
or 
msgbox i++

we need to do i=i+1 and sometime when writing long chunk of modules this lack of feature is quite trouble some.
For this we can implement feature by using OOPS architecture.

Step1.
1. Create a new .qfl file (A New function Library) .you can use the existing qfl too, but i recommend to use a new qfl for all CLASS related code.(We say the file name as modifiedOperator.qfl)

2. Now We will create a new Class which will support the this feature, add the below code in your .QFL file.


Class GenericNumber

 Private numberValue

 //Below function will handle the code for PreIncrement function
 Public function [++]()
    numberValue=numberValue+1
    [++]=numberValue
 End Function


 //Below function will handle the code for Predecrement function
 Public function [--]()
    numberValue=numberValue-1
    [--]=numberValue
 End Function


 Public default property get result()
  result=numberValue
 End Property

 Public property let result(n)
  numberValue=n
 End Property

 Sub Class_Initialize()
  numberValue=0
 End Sub

End Class

Now the Above mentioned code will handle the operation for PreIncrement/Predecrement Operations. now we need to implement a constructor like operations so that we can actually use this class

Function [customOperation](x)
   Set [customOperation]=New GenericNumber
   If isnumeric(n) Then
    [customOperation].result=x
   End If
End Function
The Function [customOperation] actually enable the user to use the Class we used now store both class and Function customOperation in modifiedOperator.qfl file Step 2. Now we are goin to use the code we written into our tests/programes.Please remember that we need to include the qfl file into your test ,Application area to use this feature now instead of using a normal integer variable we use a GenericNumber type variable.(We make an object of class GenericNumber for which we use the function customOperation . ) this functions returns the object of the class GenericNumber
// the variable num will hold the a object of type GenericNumber  with value as 5
Set  num=[As Num](5)


msgbox num.[++]
//We use .[++] instead of ++ as was in c/c++ languages.

//now run this code and the answer will be 6 
msgbox num.[--]
//now run this code and the answer will be 5

I hope this helped.

Sunday, 25 August 2013

Function Calling In VB Scripting, use of parenthesis

In VB scripting we notice two type function call syntex as per below

syntex1
Object.func1 parm1,param2

syntex2
Object.func2(param1,param2)

Now the question is what is difference between these two syntax.The answer is quite simple , it depends on the context . NO matter what the function is , if you are going to assign the return value to some variable or the result of the function call is used as input for some other function then the function should have the parameters in parenthesis.

example
msgbox "Hi"
msgbox trim(" Hi ")

Tuesday, 11 June 2013

How to Get Column No for any field/ColumnName in WebTable

Below is a piece of code which should be part of ur .QFL file.this will return the column no for specfic cell text(in specfic row).
this is very essential function as by defect VBScript/QTP lib provide only row no not column for cell data


Function gettingColNo( tbleObj,ColName,RowNo) 
 colcount= tbleObj.ColumnCount(row)
  For colNum =1 To colcount
   ColHeader = tbleObj.GetCellData(RowNo,colNum)
   If inStr(Lcase(ColHeader),Lcase(ColName)) > 0 Then
    blnResult = True
    gettingColNo= colNum
    Exit For
   End If
  Next        
  If  colNum > colcount  Then
    blnResult = False
    'your custom code to hadle false condtion, like reporting to parent function or qc
  End If
End Function