This topic covers the concepts of the Eval
function and Execute
statement in depth. The function Eval is used to evaluate a string and return the result of the string expression. The Execute statement on the other hand, executes one or more statements in a string expression and enables creation of dynamic code. Multiple statements can be executed at once by using colons or line breaks to separate them.
If you have had a chance to use RelevantCodes[1]One and went over the function libraries, you would find that all the events are controlled through Execute
statement – data is retrieved from the Excel Table and parsed by the function libraries into string statements. Once parsed, these string statements are then executed in the same manner as QTP code.
Contents
Eval Syntax
Eval(expression)
Execute Syntax
Execute statement
Difference between Eval & Execute
In the syntax above, both expression
and statement
arguments are string statements. Both expressions can be derived from VBScript or QTP code; the difference lies in the fact that Eval will always return the result of the string evaluation whereas Execute will execute a string statement for execution and will not retrieve the result (but there are workarounds). The following example demonstrates the main difference between the 2 functions:
x = 9 y = 10 bIsEqual = Eval("x = y") Execute "x = y" MsgBox "bIsEqual: " & bIsEqual MsgBox "X is no longer 9. It is: " & x
Note that Eval returned a Boolean value whereas Execute function performed a variable assignment. This article will cover the usage of both techniques in depth through both VBS and QTP code.
You must be wondering why the syntax for Eval and Execute is different. The reason is that, Eval is a function. Execute is not. That means, the following statement for Eval: bIsEqual = Eval("x = y")
, when written for Execute will return a Null value for bResult (but assignment will be performed for x
):
bResult = Execute("x = y") MsgBox bResult MsgBox "x: " & x
With that said, let me share the workaround that can be used with Execute to return the evaluation just like we did with Eval
. Here’s how:
x = 9 y = 10 Execute "z = (x = y)" MsgBox "z: " & z
Return Z (False)
More on this concept will be covered in the section Evaluating Statements.
Creating Variables
Let’s assume a scenario where you need to create the following variables: var_1
, … , var_5
. One way is to manually create the variables:
var_1 = 1 : var_2 = 2 : var_3 = 3 : var_4 = 4: var_5 = 5
A quicker, and more dynamic way to achieve the same is through Execute statement:
For ix = 1 to 5 Execute "var_" & ix & " = " & ix Next
Option Explicit
Using the Execute statement also has a direct impact if the Option Explicit
statement is ON. If a variable is not explicitly declared, an error will be occurred:
Option Explicit iNumber = 9 |
When the same variable is created through Execute, the error will not occur and the variable will be created and value assigned successfully.
Option Explicit Execute “iNumber = 9” |
Evaluating Statements
Instead of equating variables and values, we will evaluate statements using Eval. A quick example using IsObject
is demonstrated below:
Set dicObject = CreateObject("Scripting.Dictionary") If Eval("IsObject(dicObject)") Then MsgBox "Object created." Set dicObject = Nothing End If
Similarly, the above code can be broken into a Select Case
block and variables can be evaluated by their type:
Sub RetType(ByVal var) Select Case Eval("TypeName(var)") Case "Integer" : MsgBox "Integer" Case "Dictionary" : MsgBox "Dictionary" Case "Nothing" : MsgBox "Nothing" Case "String" : MsgBox "String" End Select End Sub Call RetType(Nothing) Call RetType("Test") Call RetType(CreateObject("Scripting.Dictionary")) Call RetType(1)
The same function above can be written through Execute. However, with Execute, we cannot directly retrieve the result (it is not a function, unlike Eval). Therefore, we will create a variable and store the result with the variable; the variable will then be used as the Select Case
condition.
Sub RetType(ByVal var) Dim bResult Execute "bResult = TypeName(var)" Select Case bResult Case "Integer" : MsgBox "Integer" Case "Dictionary" : MsgBox "Dictionary" Case "Nothing" : MsgBox "Nothing" Case "String" : MsgBox "String" End Select End Sub Call RetType(Nothing) Call RetType("Test") Call RetType(CreateObject("Scripting.Dictionary")) Call RetType(1)
In short then, with Execute, bResult is represented within the string statement:
Execute “bResult = TypeName(var)” |
With Eval, bResult is an actual variable that holds the result like below:
bResult = Eval(“TypeName(var)”) |
Creating Functions
I have seen this approach rarely used, but its important to know all the possibilities nonetheless. Yes, we can create Functions (really!) at run-time, as long as everything is a string and new lines are separated by colons or a line feed. This approach is generally fine for small functions, but it can get really confusing and hard to debug as the lines of code increase.
sFunc = "Function ExecuteTest" sFunc = sFunc & vbLf sFunc = sFunc & "MsgBox ""ExecuteTest executed.""" sFunc = sFunc & vbLf sFunc = sFunc & "End Function" Execute sFunc Call ExecuteTest()
We can write the same code above using colons (:
) as well:
Execute "Function ExecuteTest : MsgBox ""ExecuteTest executed."" : End Function" Call ExecuteTest()
Executing QTP Statements
This is where the Execute statement absolutely shines. As stated before, RelevantCodes[1]One executes all keywords through Execute statement. It retrieves keywords from the Excel Table as strings at runtime, creates the relevant hierarchies, and executes the hierarchies with chosen events. The concept here and the concepts covered by previous examples will remain the same: we create action strings and execute them. The last example in this topic will show how different strings can be joined together to implement events on supplied test-objects (as strings).
Let’s start with a quick example to check if our target browser exists:
strBrowser = “Browser(“”title:=Google””)” Execute “MsgBox ” & strBrowser & “.Exist(0)” |
The above code can also be written with Eval as well, enabling us to retrieve the value directly instead of using the workaround approach if we were to use Execute:
strBrowser = “Browser(“”title:=Google””)” bExist = Eval(strBrowser & “.Exist(0)”) |
The above statement when executed does the same operation as it would when you would verify the browser existence using an inline DP statement: Msgbox Browser("title:=Google").Exist(0)
. Let’s consider another example where we create an event on a WebEdit object by creating an object hierarchy through strings at run time.
strBrowser = "Browser(""title:=Google"")" 'Browser("title:=Google") strPage = "Page(""title:=Google"")" 'Page("title:=Google") strText = "WebEdit(""name:=q"")" 'WebEdit("name:=q") strEvent = "Set ""Execute Test""" 'Set "Execute Test" Execute strBrowser & "." & strPage & "." & strText & "." & strEvent
It does look interesting, doesn’t it? The same approach can be used if you are using Object Repository instead of Descriptive Programming; simply replace the Programmatic Descriptions with the Logical Descriptions in your OR, parse everything in a string and Execute it!
The example below shows how QTP code can be executed into succession through strings. The last 4 statements show how all strings are combined together to 1. Enter the search terms, 2. Click Search Button, 3. Synchronize and 4. Navigate back to the Google search page.
strBrowser = "Browser(""title:=.*Google.*"")" 'Browser("title:=Google") strPage = "Page(""title:=.*Google.*"")" 'Page("title:=Google") strText = "WebEdit(""name:=q"")" 'WebEdit("name:=q") strButton = "WebButton(""value:=Google Search"")" 'WebButton("value:=Google Search") strEvent = "Set ""Execute Test""" 'Set "Execute Test" strClick = "Click" 'Click strSync = "Sync" 'Sync strBack = "Back" 'Back Execute strBrowser & "." & strPage & "." & strText & "." & strEvent Execute strBrowser & "." & strPage & "." & strButton & "." & strClick Execute strBrowser & "." & strSync Execute strBrowser & "." & strBack
The above strings are equivalent to the following QTP code:
Browser(“title:=.*Google.*”).Page(“title:=.*Google.*”).WebEdit(“name:=q”).Set “Execute Test” Browser(“title:=.*Google.*”).Page(“title:=.*Google.*”).WebButton(“value:=Google Search”).Click Browser(“title:=.*Google.*”).Sync Browser(“title:=.*Google.*”).Back |
Closing Remarks
I hope this article has covered most of the common (and uncommon) uses of Eval and Execute. I’m sure you’ll find areas in your framework where these 2 techniques will make things cleaner and more dynamic. If there is something I have omitted, please feel free to share it with us. I hope you guys find this article useful 🙂