Facebook

Course Name Start Date Time Duration Registration Link
No Training Programs Scheduled ClickHere to Contact
Please mail To sudhakar@qtpsudhakar.com to Register for any training

Tuesday, April 27, 2010

Using one Library Functions / Variables in other Library

As we all know that we can automate the Web Applications by using only DOM + VBScript without having any automation tool. In this process we have to write the global functions to handle webobjects like links, edit boxes, radio buttons …etc.
I have got a requirement to automate an application only by using DOM + VBscript. For this process I have created all the global functions to handle every web object and stored all the functions in a library.
This is the plan I thought to Automate the Testcases Using DOM
  • Develop Global Functions to handle Web Objects
  • Write scripts by calling global functions (scripts will be stored as VBS files)
I thought of using filesystem object and execute statements to call functions in . But those are not fully capable for some of the situations. And the next one immediately came in my mind is Microsoft Script Control.
Using Microsoft Script Control you can call one library functions in another library. For this you have to follow below steps.
  1. Read and store all the text from source library file using filesystem object
  2. Add the source library code as vbscript in MSScriptControl
  3. Call the functions using MSScriptControl methods
Source Library Code

Dim a
a=20
 
Function demo ()
MsgBox "QTP Sudhakar"
End Function
 
Function demo_add (x,y)
    demo_add = x+y
End Function

Calling the Functions in other libraries

This is how we can use one Library functions / Variables in other libraries.

'Create file system object
Set fso=CreateObject("scripting.filesystemobject")
'Open library file
Set sFile=fso.OpenTextFile("C:\Documents and Settings\sudhakar kakunuri\Desktop\Script Control\source.vbs")
'Read the complete data from library file
SourceLibraryData=sFile.ReadAll
'close the file
sFile.Close
 
'Create Script Control Object
Set scrControl=CreateObject("msscriptcontrol.scriptcontrol")
'Specify the language to be controlled
scrControl.Language = "VBScript"
'Add the source library code
scrControl.AddCode(SourceLibraryData)
 
'Diplay the number of procedures in th source library
MsgBox scrControl.Procedures.Count,,"Total Procedures Count"
'Display value of a variable in source library
Msgbox scrControl.Eval("a"),,"Value of Variable 'a'"
'Executing the function source library function
scrControl.Run "demo"
'Executing the function source library function with parameters
msgbox scrControl.Run("demo_add",10,20),,"demo_add function Result"

1 comment :

  1. Thank you so much, You've helped me a lot!!

    ReplyDelete