Working with function libraries can become quite tedious, as each test must have them associated. Deployment issues may arise when tests are copied to other machines. For instance, resources may be missing due to misconfiguration (for example, undefined search paths).
If we could store the code on a server, so that associating function libraries would not be necessary, we would gain three main benefits:
In this recipe, we will examine a clever way to accomplish this.
From the File menu, navigate to New | Test, or use the Ctrl + N shortcut. Create a new file named CROWrapper.wsc
(C
stands for class and RO
for remote object).
Write the following code in the component (wsc
) file:
<?xml version="1.0"?> -<component id="CROWrapper"> <?component error="true" debug="true"?> <registration classid="{D11841E9-B794-4627-AEE6-CA552DFF11C8}" version="1" progid="CTOWrapper" description="Wraps methods"/> -<public> -<method name="PrintMe"> <parameter description="text" name="str"/> </method> </public> -<script language="VBScript"> <![CDATA[ Function PrintMe(str) msgbox "In WSC file!!! - " & str End Function ]]> </script> </component>
Store the file on a remote machine (upload to a server). For the purpose of this demonstration, the file can be found at:
http://www.advancedqtp.com/COM/CROWrapper.wsc
Now, write the following code in your test:
Const rootURL = "script:http://www.advancedqtp.com/COM/" Dim oROWrapper Set oROWrapper = GetObject(rootURL&"CROWrapper.wsc") Call oROWrapper.PrintMe("Hello") Set oROWrapper = Nothing
Run the test. A message box like the following should appear:
Basically, a component defined in a wsc
file is equivalent to a class. The statement Set oROWrapper = GetObject(rootURL&""CROWrapper.wsc"")
actually instantiates the component, so we now have an object of type CROWrapper
. As such, we can call its public methods, as we would with any object. Note that, as mentioned previously, this means that we can actually use code stored in a server without having to associate it with the test.