Showing posts with label QTP. Show all posts
Showing posts with label QTP. 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.






Friday, 30 August 2013

Reversing a String: How to reverse a string without using a reverse function

Hi Folks,
I am certain the most of you have come accross the situation like this, This is one of the most common interview question of any programing interview(No matter whether it is Scripting Lang or Prog Lang, you are gonna get this question).

No  in my question I have below example

sampleString="softwareTestingPerfection"
 
Notice that the data is used in String and not in Array and this is where it gets intresting
Now I want the output as

outputString="noitcefrePgnitseTerawtfos"

Now in conventional way we would have the stirng stored in variable array but that is the main feature in this example.We will index the each element of the string without using the actual array.

sampleString="softwareTestingPerfection"

strlen= len(samplestring)
outputString=""
For i=1 to strlen
   outputString=mid(sampleString,i,1)&outputString
Next
msgbox outputString


Hope this will help!!!!!!

for any query/comment please write on the section below


Monday, 26 August 2013

Understanding Descriptive Programming: Part 2


Continuing from... Understanding Descriptive Programming: Part 1
Now till this moment I expect reader are aware of the Objects and how the object are stored in OR(Object Repositories). Now it is time to come to real point.
Till now we were referring the Object by indicating the QTP that the definition of the particular object is stored in OR  
Idea Behind DP: The basic idea behind DP is to access Object without referring to OR.

How to implement: To implement such feature we describe the properties with which the object can be defined within the code instead of storing those in ORs.

Where to use DP/Why not Object Repositories: It can be used to perform operation on the objects which aren't stored in OR or to perform operation on the object whose properties matches a set of description properties which we can determine at run time.

Ways to implement DP  : DP can be implemented by two way. Static and Dynamic Descriptive Programing.

Static Descriptive Programing: In this type of DP, we mention the description properties within
 the Vb Script statement.

sampleObject("Property1:=Value1",.......,"PropertyN:=Value")

Please notice we have used ":=" instead of usual "=".
This is the General Syntax.Please notice that each "property:=value" must be separated by comma.

Below is a sample Vb Script statement which uses static DP, and a practical use of the static DP

MyVar="Submit"
Browser("BrowserName").Page("PageName").Webtable("TableNAme").Webedit("name:="&MyVar).Set "SampleText"

Now please notice that in above statement we have used a variable Myvar for sting. *a very imporatant thing in static DP is that u cant have static DP object in middle of OR based statment.Means
 
Browser("BrowserName").Page("PageName").Webtable("TableNAme").Webedit("name:=Submit").Set "SampleText"
is prefectly valid syntax , but we cant use
 
Browser("BrowserName").Page("PageName").Webtable("htmlid:=inventroyform").Webedit("Submit").Set "SampleText"
is an invalid syntax, as we cant get reference for the Submit button from DP based object as it is expecting OR based reference.
I will be discussing on dynamic DP in later posts

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

Understanding Descriptive Programming: Part 1

HI Folks..
In This Post I will be describing what is descriptive Programming in using VBScript(QTP), and define the various types of descriptive programming.

What is Descriptive Programing(DP):   Before understanding the DP, we must first know what is Object.
In QTP , Object is anything with a set of properties and methods(functions).
It can be any visual entity displayed on the screen as WebPage,Button,Dropdown(Weblist), all of these are type of Objects.(Although it is not necessary that each object should be visual too, this is done with a specific property .)
Now every object has a set of property with which it is defined, such as a WeButton named as "Submit" will have a set of propertys such as 'name', 'htmlid','innertext','outertext',etc
QTP understanding the entire given script in terms of Objects and the operation performed upon them .
Such as click on the submit button, then within the code we specfiy the object and perform the operation.


now the code might look as

Set buttonObj=Browser("sampleBrowser").page("samplePage").WebButton("Submit")
buttonObj.click

OR
Browser("sampleBrowser").page("samplePage").WebButton("Submit")

both of the statments are virtually same.

now if we are not using the DP methods then the properties of the submit button are  stored in Object repositories.and we can directly specify the name and the location of the object.The properties of the submit button are specified within the OR itself and user does not require to provide the information regarding submit button in code .(As shown in the code above).

But When we use the DP the properties through which object is described in the application(Description properties) will be specified within the code.(Without using the OR)


We will be looking deep into the DP in later parts of the Post.

How to use ByRef parameter calling in Vb Scripting.

Most of the time automation personnel don't use the ByRef parameter functionality.
They use normal function calling with which is by default byVal.

Now actually most of the time tester don't know these two feature.so first I m gonna explain there two one by one.

1.ByVal Function calling.

Syntax:

var1=ADD(parameter1,parameter2)




Function ADD(val1,val2)

 val1=val1+1
  ADD=val1+val2


End Function


Function ADD(ByVal val1,ByVal val2)

 val1=val1+1
  ADD=val1+val2


End Function


now in this case the parameter calling is ByVal.In fact both of the statements are identical.

when we byval prefix , it means only a copy of the parameter is going within the function.Any Changes made within  the parameter wont reflect the in the actual called value.
but if we use below syntax


var1=ADD(parameter1,parameter2)
Function ADD(ByRef val1,val2)

 val1=val1+1
  ADD=val1+val2


End Function


in this case any changes made in val1 will reflect in parameter1 too.



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


How to Get a unique row in a webTable Object with Defined column Names

Recently I came across the situation where I need to insert a row in a table and then the validate the
same in the table, whether the row is successfully added or not.
I have written a piece of code which i think will be usefull for u guys in your projects as well.
This is a very common feature of PRPC framework based applications.In which almost everything is composed of Tables
Now there are the few thing we need to make sure that the row is added .

1. We need to check the row count of the table before the row is inserted.Which we can later
    compare  to the row count after the insertion.
2. Once we are sure that the row is added. we need to make sure we have the exact row added which
     we were supposed to .

now for this step 2 we can write a func. which we can use in projects.

I believe it will be usefull for u guys.






 Function Fn_UniqueFirstRow(tblObj,strColNames,strValues)

strRowNum="-1"
aryColNames=Split( strColNames,";")
aryValues=Split(strValues,";")
rowFound=False
rowCount=tblObj.GetROProperty("rows")
If rowCount>0 Then
 If UBound(aryColNames)=UBound(aryValues) Then
  colCount=UBound(aryColNames)
  For rowCounter=1 to rowCount
   '---compile the actual row Value for each row and compare
   MsgSTR=""
   For colCounter=0 to  colCount
    colName=Trim(aryColNames(colCounter))
    '----Now here I m expecting that you have some function to get column   
    '---number, 'if not i will be posting that as well later
    colNumber=Fn_ColumnNumber( colName,tblObj,1)
    colData=Trim(tblObj.GetCellData(rowCounter,colNumber))
      If colCounter=0 Then
        MsgSTR=colData
      Else
        MsgSTR=MsgSTR&";"&colData
     End If
    Next
    If StrComp(Trim(MsgSTR),Trim(strValues)) = 0 Then
     rowFound=True
     strRowNum=rowCounter&";"&" Unique is row found with given values"
     Exit For
    End If
  Next
   If  Not rowFound Then
    strRowNum="-1"&";"&"Unique row doesnt exist"
   End If    
   Else
    strRowNum="-3"&";"&"Col Names and col Values arent in same number"
   End If
  Else
   stRowNum="-2"&";"&"Table has no rows"
  End If
   Fn_UniqueFirstRow=strRowNum
 End Function 

Thursday, 25 April 2013

QTP: Handling WebEdit in WebTable Object.

Using web table in conventional WebApps Testing is quite uncommon.\
As we know all the WebButton, Webedits ,WebLists are ususallu linked with a webtable in webpage.
If we direct object validation for WebEdit as it may look like as below
UserName:  [     ]
Here [] represents the webedit.
Normally what people do is they add this webedit obj in there rep and do their validations. But in my opinion this is not a smart work.

We should access them from User Name field because this is how a Manual SME will see the Username name webedit. Because in approve stated approch webedit can be any where in page but next to username field and it will still be passed.

SO what should we do?????? Hmmm

right a function which handle the webtable it will get the value /Child tiem next to given cell data (In this case UserName)
Now when u pass the value "User Name" to func it you can access anything which is next to this field. By the use of functions u can get the falue /fill the value etc..
yes yes .. i know it will take alot of time to right the func for all cases.
but once it is written it is quite easy to use those in daily scripts.. and belive me your Test Case will req very low maintainence, as it wont be tackling webedit directly it is hadling the webtable, so it wont matter if the prop of Webedit chnages in future.