My Blog List

Saturday, May 12, 2012

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 !!!

1 comment:

  1. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium

    ReplyDelete