My Blog List

Saturday, May 19, 2012

Download File - VBScript function

Here is the function to download any file over internet using XMLHTTP COM objects in VBScript. 

  1. Function Download(sFileURL, sLocation)
  2.   'create xmlhttp object
  3.   Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
  4.  
  5.   'get the remote file
  6.   objXMLHTTP.open "GET", sFileURL, false
  7.  
  8.   'send the request
  9.   objXMLHTTP.send()
  10.  
  11.   'wait until the data has downloaded successfully
  12.   do until objXMLHTTP.Status = 200 :  wscript.sleep(1000) :  loop
  13.  
  14.   'if the data has downloaded sucessfully
  15.   If objXMLHTTP.Status = 200 Then
  16.  
  17.         'create binary stream object
  18.     Set objADOStream = CreateObject("ADODB.Stream")
  19.     objADOStream.Open
  20.  
  21.           'adTypeBinary
  22.     objADOStream.Type = 1
  23.     objADOStream.Write objXMLHTTP.ResponseBody
  24.  
  25.           'Set the stream position to the start
  26.     objADOStream.Position = 0    
  27.  
  28.           'create file system object to allow the script to check for an existing file
  29.           Set objFSO = Createobject("Scripting.FileSystemObject")
  30.  
  31.           'check if the file exists, if it exists then delete it
  32.     If objFSO.Fileexists(sLocation) Then objFSO.DeleteFile sLocation
  33.  
  34.           'destroy file system object
  35.     Set objFSO = Nothing
  36.  
  37.           'save the ado stream to a file
  38.     objADOStream.SaveToFile sLocation
  39.  
  40.           'close the ado stream
  41.     objADOStream.Close
  42.  
  43.     'destroy the ado stream object
  44.     Set objADOStream = Nothing
  45.  
  46.   'end object downloaded successfully
  47.   End if
  48.  
  49.   'destroy xml http object
  50.   Set objXMLHTTP = Nothing
  51. End Function
  52.  
  53. 'Call like this
  54. Download "http://tamilmp3world.com/123.mp3""C:\test1.mp3"

I got this script from internet and thanks for the author as I forgot where I took this.

ExecuteGlobal in VBScript

Most of guys will face this problem like,
- Having a class in a .vbs file say class1.vbs
- In another .vbs file, trying to create object for that class
Here it will say class is undefined because your class should be in global namespace so that your second script should know. To achieve this, use ExecuteGlobal command in VBScript.
Here I am putting sample function to include your class or other .vbs files which you are going to refer.
  1. 'To include a file which are referenced here.
  2. Sub Include(file)
  3.   Dim fso, f
  4.   Set fso = CreateObject("Scripting.FileSystemObject")
  5.   Set f = fso.OpenTextFile(file, 1)
  6.   str = f.ReadAll
  7.   f.Close
  8.   ExecuteGlobal str
  9. End Sub
  10. 'Just call this function like this in your scripts
  11. Call Include("C:\class1.vbs")
  12. 'Then try to create object for the above class
  13. Dim obj
  14. Set obj = New Class1
  15. 'continue...
Hope this give you ideas !!!

Saturday, May 12, 2012

Start learning VBScript or JavaScript

Lot of guys who are interested to move to automation testing from manual have dilemmas on how to start learning the scripting language, which one I have to install, where to study, etc.
For those who are covered under the above case, this article is for you.
Both VBScript and JavaScript will run on windows based script host. So follow the simple steps to learn.


1. Just create a text file in your system and save it as .vbs(VBScript) or .js(JavaScript) extension.
2. Open the .vbs or .js file using notepad or other your favorite notepad editor like wordpad, notepad++, etc.
3. Just do your code and save.
4. Just double-click the .vbs or .js file. It will execute and give you results.
5. For your understanding like what value it returns, use msgbox which will show the value in dialog while executing for VBScript. For JavaScript, just create an object for "WScript.Shell" and use Popup.
6. If errors in your code, the it will give you error dialog by specifying error line and other info.
For technical resources, browse google. You will get lot of sites which will teach you VBscript and JavaScript. Start from W3Schools as a beginner.
Here is another good link for VBScript here.
Just refer the sample code here.


Environment Setup:
Basically VBScript will support by default in all windows versions. First right-click the file, Properties and change the open with as "Cwindows\systems32\WScript.exe".
If you have problems in executing VBScript just follow the steps mentioned here.
Use this registry file to merge. Download here
For JavaScript, just assign open with as "Windows based script host" which mentioned above. If still facing problems, do merging these attached registry entries. 
Download for Windows 7
Download for XP

VBScript Dictionary Object vs JavaScript Object

In test automation world, always we have situations to return more than one value from a function call. For this we have some simple ways but yet always we searching for better ways. Here I am listing some ways we can 
consider.
1. Array
We have simple array concept in all languages but this not effective while retrieving values for a particular field because array is related to index. We can't know at which index which value is there. We have to implement our own methods to find them.
2. Class and Objects
This is an another effective method but the user need some information about classes and objects. You can create a class, then class variables for all fields you are going to return values. So just create an instance for this class i.e. object and assign the values for all properties associated with this object and return this object.
Here you can directly get your particular field value by passing the field name. No concept of indexes like array. So simple to access a value and assign a value.
3. Dictionary Object
This is mostly used format of returning multiple values in VBScript. This is an object which contains values in  key-value pairs format. Very simple to access, assign, remove, etc.
Here I am going to give some demo about VBScript Dictionary object.
  1. Dim dic
  2. Set dic = CreateObject("Scripting.Dictionary")
  3. dic.Add "Name""Shanmugavel"
  4. dic.Add "Age""26"
  5. dic.Add "City""CBE"
  6. dic.Add "Height""5.4"
  7. 'To Check a particular key exists or not
  8. If dic.Exists("Weight") Then
  9.    msgbox "Exists"
  10. Else
  11.    msgbox "Not Exists"
  12. End If
  13. 'To loop through all keys present in dic object
  14. For Each key in dic
  15.    msgbox dic.Item(key)  'Here we are using property "Item" to get a key value
  16. Next
  17. 'To change a key name without altering the value associated to that key
  18. dic.Key("Age") = "MyAge"
  19. msgbox "Value for MyAge: " + dic.Item("MyAge")
  20. msgbox "Check for Age Exists: " + CStr( dic.Exists("Age") )
  21. 'To get count of available Key-Value pairs
  22. msgbox dic.Count
  23. 'To get all key names only
  24. Dim KeyNames
  25. KeyNames = dic.Keys   'Returns an array
  26. For Each key in KeyNames
  27.    msgbox key
  28. Next
  29. 'To get all key values only
  30. Dim KeyValues
  31. KeyValues = dic.Items   'Returns an array
  32. For Each value in KeyValues
  33.    msgbox value
  34. Next
  35. 'To Remove a key-Value pair
  36. msgbox "Before Removing a key-Value pair, Count: " + CStr( dic.Count )
  37. dic.Remove("Height")
  38. msgbox "After Removing a key-Value pair, Count: " + CStr( dic.Count )
  39. 'To Remove all key-Value pairs
  40. dic.RemoveAll
  41. msgbox "After Removing all key-Value pairs, Count: " + CStr( dic.Count )
  42. 'To make key names Case-Sensitive
  43. ' -- By default, VBScript considers everything as Case-Insensitive because its a Case-Insensitive scripting language.
  44. ' -- We can make Case-Sensitive keys to have 2 keys with same key name i.e. One with lower case and another with upper case
  45. ' -- VBBinaryCompare - 0; VBTextCompare - 1; VBDataBaseCompare - 2 (compares information inside)
  46. dic.CompareMode = VBBinaryCompare  'or assign value 0
  47. dic.Add "Name""shanmugavel c"
  48. dic.Add "NAME""SHANMUGAVEL C"
  49. msgbox "Name: " + dic.Item("Name")
  50. msgbox "NAME: " + dic.Item("NAME")

JavaScript:
In JavaScript also we have first 2 options as in VBScript. But JavaScript have a concept called associative array or Object i.e. equivalent to Dictionary Object in VBScript. Here I am giving simple demo code.
  1. var WS = new ActiveXObject("WScript.Shell");
  2.   //Declaring and Assigning values with key-value pairs
  3.   var obj = { Name: "ShanmugavelC", Age: "26",
  4.               City: "CBE", Height: "5.4"       };
  5.   //To get all key-value pairs
  6.   for (key in obj)    
  7.     WS.Popup("Value for " + key + ": " + obj[key]);
  8.    
  9.   //To add a Key-value pair
  10.   obj["Weight"] = "69";
  11.   //To check a key exists or not
  12.   if( obj.hasOwnProperty("State") )
  13.     WS.Popup("Key State Exists");
  14.   else
  15.     WS.Popup("Key State not Exists");
  16.   //By default, JavaScript is Case-Sensitive
  17.   obj["NAME"] = "SHANMUGAVEL C"
  18.   WS.Popup( "Value for NAME: " + obj["NAME"] );
  19.   WS.Popup( "Value for Name: " + obj["Name"] );
Hope this will give you some ideas !!!

Thursday, May 3, 2012

QTP - File Formats

Lot of QTP users are very interested to know all file formats related to QTP. Here are list as of my knowledge. Kindly do correct me if I am wrong.


Inside each Action:
-->  .BDB - Berkeley  Data Base - This is the Local Object Repository
-->  .MTR - Mercury Test Resource - This file contains Resources i.e. External Libraries, ORs and other files associated to each action.
-->  .MTS - Mercury Test Script - This is the script belongs to particular action.
-->  Folder Snapshots - Used to store the Active Screen images for the action.


Inside each Test:
-->  Actions folder will be there.
-->  default.CFG - Contains full details about run configurations
-->  default.USP - Contains user info with run logics. (related to LoadRunner execution)
-->  test.TSP - Used to store test settings info.
-->  Parameters.MTR - Contains Input, Output parameters info belongs to the test.
-->  TestName.USR - This file contains info about add-ins, files associated, environment info and other files info belongs to the test. (related to LoadRunner execution)
-->  test.lck - Lock file for the test if anyone is using the test.
-->  default.xls - Test Data file i.e. default DataTable associated for the action.


Others:
--> .TSR - Test Shared Repository
--> .QFL - QTP Function Library. Just custom QTP format instead to use common .VBS
--> .QRS - QTP Recovery Scenario

Option Explicit


Option Explicit: Why we need this statement while writing VBScript?

-- By default, VBScript will allow use of variables without declaration which leads to lot of mis-spell, type and wrong variable name errors.
-- This will force the VBScript Engine not to accept the variables without declaration before its first use.
-- Normally, Used to avoid typo errors, giving wrong variable names, redefining variables by mistake, etc.

Consider the below script:
  1. Dim var1
  2. var1 = 10
  3. 'This line will not throw any error like "Variable undefined"
  4. var2 = 20
  5. 'This line will not throw any error like "Variable redefined"
  6. Dim var1
After inserting Option Explicit Statement, it will throw error.
  1. Option Explicit
  2. Dim var1
  3. var1 = 10
  4. var2 = 20     'This line will throw error "Variable undefined"
Also,
  1. Option Explicit
  2. Dim var1
  3. var1 = 10
  4. Dim var1      'This line will throw error "Variable redefined"

The same functionality is available in JavaScript by typing "use strict" like below;
  1. "use strict";
  2. var var1 = 10;
  3. alert(var1);