Sunday, 24 November 2013

QTP Passing Parameters to a Function/Sub - Pass By Value & Pass By Reference



You can pass parameters to a function or sub procedure by value or by reference. Let’s see what both these terms mean.

Passing Parameters by Value (byVal).

 With this way of passing parameters, only a copy of the original parameters is passed. This means that whatever modifications we make to the parameters inside the function, it doesn’t affect the original parameters.


Sample Code 1: Passing parameters by value to a function

Dim val
val=5

'Function Call
fnFunc val
msgbox "Original Value: " & val 'msgbox displays value 5

'Function Definition
Function fnFunc(byVal val)
  val = val + 2
  msgbox "New Value: " & val 'msgbox displays value 7
End Function

In the above example you would see that the new value get changed to 7 but it doesn’t get reflected to the original value which still shows the value as 5 only.


Passing Parameters by Reference (byRef).

In this case, the reference of the original value is passed as the parameter to the function. Therefore, whatever change is done to the parameter inside the function, the same is reflected in the original parameter also. By default, values are passed by reference in a function. i.e., even if you don’t use byRef keyword, the parameters are passed by reference.


Sample Code 2: Passing parameters by reference to a function

Dim val
val=5
'Function Call
fnFunc val
msgbox "Original Value: " & val 'msgbox displays value 7
'Function Definition
Function fnFunc(ByRef val)
  val = val + 2
  msgbox "New Value: " & val 'msgbox displays value 7
End Function

Since the original parameter is passed as reference to the function, both the original and new value has the updated value 7.

Differnce between Sub and Functions in QTP


1. QTP Actions:-

Action is specific to QTP and not the part of vbscript. Every QTP test has at least one Action(default name is Action1).
Action can have an object repository associated with it. Action can return multiple values in form of 'output parameters'.

2. Procedures:
2.1. VBScript Sub Procedures:-

A Sub procedure:

    * is a series of statements, enclosed by the Sub and End Sub statements
    * can perform actions, but does not return a value
    * can take arguments
    * without arguments, it must include an empty set of parentheses ()


Sub mysub()
  Print "my Sub Procedude"
End Sub

or

Sub mysub(argument1,argument2)
  Print "my Sub Procedure"
End Sub

How to call Sub Procedures:

To call a Sub, you will use Call statement by enclosing arguments (if any) in parentheses.


The Call statement is not necessary to call a Sub, but if you want to use Call statement (Recommended), you must enclose arguments (if any) in parentheses.

Call mysub(argument1,argument2)

You can call a Sub without using Call statement as well, but not recommended.

mysub argument1,argument2


2.2. VBScript Function Procedures:-

A Function procedure:

    * is a series of statements, enclosed by the Function and End Function statements
    * can perform operations and can return a value
    * can take arguments that are passed to it by a calling procedure
    * without arguments, must include an empty set of parentheses ()
    * returns a value by assigning a value to function name itself


Function myfunction1()
  Print "my fuction1"
End Function

or

Function myfunction2(a,b)
myfunction2=a+b  'assign value to function name
End Function


How to call Function Procedures:

Call myfunction1() 'calling 1st function, without any return value

abc=myfunction2(argument1,argument2)  'calling 2nd function, with a return value

Here you call a Function called "myfunction2", the Function returns a value that will be stored in the variable "abc".

Hope all your confusions are gone! But if you still have any doubts, please post your comments!


Difference between waitproperty and checkproperty in qtp


Wait Property

As per the name, waitProperty waits till the condition met
or the maximum time specified.

For ex:B().p().webbutton().waitproperty "Enabled",true,30000
So here QTP waits till the button is enabled or waits for
max of 30 Secs and if the button is not enabled, then the
step fails.


Check Property

Where as CheckProperty is to verity whether the value is
matched or not and returns a Boolean value

Ex:
B().p().webbutton().CheckProperty "Enabled",true,30000
Here QTP verifies whether the button is enabled or not and
waits for 30 sec and returns true if the button enable else
return false.

Saturday, 23 November 2013

Error Handling in VB Script


To avoid application may get system failure or Crash, we can use the Error Handling in VB Script



-> On Error Resume Next   (if we want bypass the error, we can use this statement)

-> On Error goto 0     (Deactivate On error Resume Next)

-> Error objects (Err) has

    -> Description Property  (while running we get error description. err.Description())
    -> HelpContext Property
    -> Helpfile Property    (When error occurred we go for help file content using this property)
    -> Number Property   ( Its throw error number)
    -> Source Property   (from where this error occurred)

  Methods are
    -> Clear Method ( Its automatiocally invoked while end of function)
    -> Raise Metod  (used for user defined method)





Programming Script
------------------
1)

On Error Resume Next

Dim a,b

a=12

b 9

msgbox a+b


o/p   12

The above script we have Error (b 9), but "on Error Resume next" bypass the error and move to next statement.


2)

On Error Resume Next

Dim a,b

a=12

b 9

msgbox a+b

on Error goto 0

b 9


o/p  12   and show error too

The above script deactivate the "on error resume next" uding "on error goto 0" statement.



3)

On Error Resume Next

Dim a,b

a=12

b 9

msgbox a+b

msgbox "Error Source = " &err.source
msgbox "Error Description =" &err.Description
msgbox "Error Number = " &err.Number
msgbox "" &err.Clear



o/p
 12 
Error Source = Microsoft VBscript Runtime error
Error Description = Wrong number of arguments or invalid property assignment
Error Number = 450

Sunday, 10 November 2013

GetTOProperty,SetTOProperty,GetROProperty



Test Object - Object value and property present in the Object Repository and

application while recording.

Is an object that QuickTest creates in the test to represent the actual object in the application.QTP stores information
about the object that will help to identify and check the object during the test run.


Runtime Object - Object value and property present in corrent application

while execution.

Is an actual object in the application on which methods are performed during the test run.

GetTOProperty - We can retrieve any property value of the test object.

Ex:


systemutil.Run "iexplore.exe", "http://www.google.com"

Set oWebEdit=Browser("Google").Page("Google").WebEdit("q") 'Create webedit object

Set TOProperties=oWebEdit.GetTOProperties 'Get TO Properties

For i = 0 to TOProperties.count - 1 'Looping through all the properties
    sName=TOProperties(i).Name ' Name of the property
    sValue=TOProperties(i).Value 'Value of the property
    isRegularExpression=TOProperties(i).RegularExpression 'Is it a Regular expression
    Msgbox sName & " -> " & sValue & "->" & isRegularExpression
Next

'##############
'### Result:###
'##############
' type -> ext -> true
' name -> q -> true
'### End of Script ####




SetTOProperty - We can set any property value of the test object.


EX:

oldName = oWebEdit.GetTOProperty("name")
msgbox "Old Name ->" & oldName
oWebEdit.SetTOProperty "name","New Value"
NewName = oWebEdit.GetTOProperty("name")
msgbox "New Name ->" & NewName

'##############
'### Result:###
'##############
' Old Name ->q
' New Name ->New Value
'### End of Script ###



GetROProperty - We can retrieve any property value of the Runtime object.

Ex :

Browser("Page").Page("Page").Navigate "www.google.com"
Browser("Page").Page("Page").WebEdit("q").set "arunrajvdm"
msgbox Browser("Page").Page("Page").WebEdit("q").GetROProperty("value")


We can not set any property value of the Run time object.

QTP object spy



Its used to know object hierarchy, object Property, object operations and

property value. Everything visible in object spy, so that testers easily

know about how developers coded on the perticular object.

Object Hierachy - Show the object parent- child Hierarchy
Properties - The properties are same for all corresponding object.But different property value for all the objects. 

Ex: All the properties are same for all WinButton object. But Property values are different.

That means WinButton class have the same properties for all WinButton object

in application, But have different value.

Operations - The operations are same for all class of the object.  That

means WinButton class have the same operations for all WinButton object in

application.



Standared object

Developer created the all object using Native class,Object class. Win Button

values are

Native Class = Button
Object Class = Button

But Visual basic assign the value for class name to WinButton

Class = WinButton

These kind of objects are called Standared Object.


some common object properties are

Class name = class name of the object 
regexpwndclass = its same as class name assigned by QTP
text = name of the object in application
regexpwndtitle = its same as text assigned by QTP
x,y = The X and Y axis value in the application.
abs_x,abs_y = The X and Y value is the desktop screen absolute location,  if

application location change in window, this value change automatically.
Focused = its focused or not
Enables = ITs enaabled or disabled
Height = this value assigned by desktop screen location, if application

location change in window, this value change automatically.



object spy operation

-> Highlight in application -> its hughlight the object in application

QTP Smart Identification for identify the object in application


-> Whenever the QTP unable to find an object in application because of the

object got changed or the object are not unique, then the script get fails.

-> if we configure the smart identification for the object class , then

there is get chance to QTP able to identify the object.

-> smart identification is more complex, but its flexible.

-> smart identification has two type of property

a) Base filter Properties
b) Optional filter Properties

a) Base filter properties

    These are the fundamental properties of the perticular object class,

that can not be changed.
EX: For link html tag always <A>
    For Edit Html Tag always INPUT
These are base filter properties which are not changed always.

b) Optional Filter Properties

Except Base Filter Properties, rest of the properties are called Optional

Filter Properties.


Smart Identification Process

1) After Mandatory Properties and Assistive properties only QTP move into

Smart Identification. So Smart identification not check the properties from

Mandatory and Assistive properties.

2) Smart identification starts to find the all object in the perticular

page. That means

Browser("Page").Page("Page").WebEdit("q").click

Its take all the object in this page -> Browser("Page").Page("Page")

3) Then its take all object and compare those object with base Filter

properties, so now its reduce the count of the object.

4) then its take that object and compare with optional filter properties to

find out the expected correct objetc.

5) If object unable to find by QTP for above process then its move to

ordinal identifier.