Thursday, April 25, 2013

Difference between Action and Function


Execute( ) Statement in QTP


Execute( ) Statement in QTP

Executes one or more specified statements.

Execute statement
The required statement argument is a string expression containing one or more statements for execution. Include multiple statements in the statement argument, using colons or embedded line breaks to separate them.
 

*********
In VBScript, x = y can be interpreted two ways. The first is as an assignment statement, where the value of y is assigned to x. The second interpretation is as an expression that tests if x and y have the same value. If they do, result is True; if they are not, result is False. The Execute statement always uses the first interpretation, whereas the Eval method always uses the second.


Note In Microsoft® JScript™, no confusion exists between assignment and comparison, because the assignment operator (=) is different from the comparison operator(==).
The context in which the Execute statement is invoked determines what objects and variables are available to the code being run. In-scope objects and variables are available to code running in an Execute statement. However, it is important to understand that if you execute code that creates a procedure, that procedure does not inherit the scope of the procedure in which it occurred.

Like any procedure, the new procedure's scope is global, and it inherits everything in the global scope. Unlike any other procedure, its context is not global scope, so it can only be executed in the context of the procedure where the Execute statement occurred. However, if the same Execute statement is invoked outside of a procedure (i.e., in global scope), not only does it inherit everything in global scope, but it can also be called from anywhere, since its context is global. The following example illustrates this behavior:

Dim X ' Declare X in global scope.
X = "Global" ' Assign global X a value.
Sub Proc1 ' Declare procedure.
Dim X ' Declare X in local scope.
X = "Local" ' Assign local X a value.
' The Execute statement here creates a
' procedure that, when invoked, prints X.
' It print the global X because Proc2
' inherits everything in global scope.
Execute "Sub Proc2: Print X: End Sub"
Print Eval("X") ' Print local X.
Proc2 ' Invoke Proc2 in Proc1's scope.
End Sub
Proc2 ' This line causes an error since
 
' Proc2 is unavailable outside Proc1.
Proc1 ' Invoke Proc1.
Execute "Sub Proc2: Print X: End Sub"
Proc2 ' This invocation succeeds because Proc2
' is now available globally.
The following example shows how the Execute statement can be rewritten so you don't have to enclose the entire procedure in the quotation marks:

S = "Sub Proc2" & vbCrLf
S = S & " Print X" & vbCrLf
 
S = S & "End Sub"
Execute S

------------------example-------------------------------------------

var1="func1"
execute("func1()") 'output is Hi func2
execute(var1) 'output is Hi func2
execute("Call (" & var1 & ")" ) ''output is Hi func2
eval("func1()") 'output is Hi func2.but always prefer execute statement if there is no return value

assume function library will be like mentioned below..
function func1()
msgbox "Hi func1"
end function
function func1()
msgbox "Hi func2"
end function

Difference Between Execute() statement and Eval() statement in QTP ?

Eval()
Evaluates an expression and returns the result.

syntax:[result = ]Eval(expression)

result :
Optional. Variable to which return value assignment is made. If result is not specified, consider using the Execute statement instead.
 

expression :
Required. String containing any legal VBScript expression.
 


In VBScript, x = y can be interpreted two ways. The first is as an assignment statement, where the value of y is assigned to x. The second interpretation is as an expression that tests if x and y have the same value. If they do, result is True; if they are not, result is False. The Eval method always uses the second interpretation, whereas the Execute statement always uses the first.

Sub GuessANumber

Dim Guess, RndNum
RndNum = Int((100) * Rnd(1) + 1)
Guess = CInt(InputBox("Enter your guess:",,0))
Do
If Eval("Guess = RndNum") Then
MsgBox "Congratulations! You guessed it!"
Exit Sub
Else
Guess = CInt(InputBox("Sorry! Try again.",,0))
End If
Loop Until Guess = 0

End Sub

Thursday, April 11, 2013

How to call a function in one action from another action

By passing the function name with parameters as Input parameters


Following is the script in my "Action1"
RunAction "LibraryAction",0,"displayMsg(3)"
FuncReturnVal = RunAction ("LibraryAction", oneIteration, "sum 5,6,7,8")
msgbox FuncReturnVal

Create a New action called "LibraryAction", can created an input parameter call "function" of type string
Following is the script in my second action "LibraryAction"

func = Parameter("function")
Execute func
Public function displayMsg(str)
msgbox(str)
End Function

Function sum(a,b,c,d)
temp=a+b+c+d
exitAction(temp)
End Function

Saturday, April 6, 2013

QTP - Exist Property

QTP - Exist Property

SystemUtil.Run "iexplore.exe", "https://www.google.com"

Var=Browser("name:=Google").Page("title:=Google").WebEdit("name:=q").Exist(5)
Msgbox var ' Returns True if page if WebEdit is found within given time i.e. 5 seconds


'".Exists" retsurns boolean value i.e. True or False

Environment Variables

Environment variables in QTP are variables whose value is available for all actions in a test. These values are available globally for any actions, libraries, recovery scenarios etc.

There are mainly two types of environment variables:
Built-In and User-Defined.

Built-in variable - Built-in variables are QTP defined variables that contains information of Test Properties, computer’s information, Information related to operating system and Action’s property.

User defined variable - Other than environment variable defined in-built in QTP, We can define user defined environment variables. The main purpose of defining user defined variables is to define global variables that can be used throughout test execution irrespective of working with different Actions. So scope of user defined variable is throughout the test execution. This can be of type internal or external if loaded from a file.
User-Defined internal environment variable: Variables defined in the test and saved with the test. You can change these environment variables during run time.



Environment.Value("URL")= "http:\\www.google.com"
URL=Environment.Value("URL")
Systemutil.Run "iexplore.exe",URL

  •  User-defined external environment variable: Variables declared in an external environment file. You cannot change these environment variables during run time.
Steps to follow:
File --> settings --> Environment --> select variable type as user defined --> click add icon (+) --> enter variable name & Value --> click OK --> click export --> browse path and enter file name --> save with xml extension --> click OK.
2. Associate environment variable file(xml)
File --> settings -->Environment --> select variable type as user defined --> check “load variables and values from the external file” --> browse path of the xml file --> click apply --> Click OK.
XML file with variable name and value should be mentioned
 Loading of variables from external file as shown above can be done through code:
'Check if an External Environment file is loaded and if not, load it.
fileName = Environment.ExternalFileName
if (fileName = "") Then
    Environment.LoadFromFile("C:\Env.xml")
End If


Use of Environment variable 
  • We can define parameters in QTP using environment variables.
  • Environment variables can act as global variables which can be used across different  tests/actions.
  • We can dynamically load environment variables during start up of QTP execution and can store url, environment, login details in a xml file,which will be loaded each time test is executed.
  • Built- in Environment variables gives us useful information about the test environment and extracting the information can be useful.

1. Built-In Environment Variables: These are the variables defined by Quick Test Professional. This includes many useful variables like OS, OSVersion, UserName, VUSerId etc.

  • ActionIteration - Indicates which action iteration is currently running.
  • ActionName - Indicates which action is currently running
  • ControllerHostName - the name of the computer which serves as a controller
  • GroupName - The scenario identification number
  • LocalHostName - Local Host Name
  • OS - Operation System
  • OSVersion - Operating system version
  • ProductDir - folder path where the product is installed
  • ProductName - Product Name
  • ProductVer - Product Version
  • ResultDir - Folder path where the results are saved
  • Scenarioid - The scenario identification number
  • SystemTempDir - System Temporary Directory
  • TestDir - Path of the Test
  • TestIteration - Indicates which test iteration is currently running
  • TestName - The name of the test
  • UpdatingActiveScreen -
  • UpdatingCheckpoints -
  • UpdatingTODescriptions -
  • UserName - Windows Login User Name
  • VUserId -
There are two types of User-Defined environment variables namely, internal and external.
'User can create their own environment variables like "URL"

User defined External: are the variables that we predefine in the active external environment variables file.These can be created using a list of variable-value pairs in an external file in .xml format.

1. Create External user defined variable

Loading Environment File Using ExternalFileName Property and LoadfromFile Method -
Using environment variables in QTP
Environment variables have many uses while scripting with QTP. Consider a scenario where we are designing an automation framework. While running a framework we have to load many files, for instance an excel sheet containing object description. Path to this excel file my change when you move the framework from one computer to another. TestDir environment variable can come in handy in such a scenario. Environment.Value (“TestDir”) will return the path of the folder in which the test is located. By manipulating this value we can find the path to the excel file.
Consider another scenario where you want to open the test result folder after a test is completed. To do this we can make use of the environment variable ResultDir that returns the path where test results are stored.
Basic syntax to get an environment value is
Enviornment.Value (variable name)
To get test "OS" you have to use
Enviornment.Value (OS)
Default property or Environment is value so we can get the same value with
Environment (OS)