Skip to main content

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

Comments

Popular posts from this blog

Some good Resources / Blogs / Sites for Selenium Users

Here I have listed out some good blogs and sites by extensive selenium automation users. Hope these will help you a lot. http://automationtricks.blogspot.com  - by NirajKumar http://www.theautomatedtester.co.uk/ http://testerinyou.blogspot.com   - by Naga/Mathu http://seleniumready.blogspot.com  - by Farheen Khan http://seleniumdeal.blogspot.com/  - Amit Vibhuti http://seleniumexamples.com/blog Sauce Labs and BrowserMob are companies doing cloud and extensive selenium automation services, products, etc http://saucelabs.com/blog http://blog.browsermob.com http://testingbot.com/ Cedric Beust -  creator of the TestNG Java testing framework. http://beust.com/weblog/ http://blog.reallysimplethoughts.com/  - by Samit Badle, Created many Selenium IDE Plug-Ins Available Colud Testing: 1. SauceLabs 2. Soasta 3. BrowserMob 4. CloudTesting.com  etc. Selenium Testing Products: 1. Twist by ThoughtWorks 2.  TestMaker by  PushToTest 3. Element34 company providi

UFT - Take full page screenshot by scrolling the page

'######################################################################################## 'This is navigate through the full page and taking individual screenshot of visible area '######################################################################################## Function TakeScreenshot Dim intScrolls, intScroll, strScrollPos Set pgApp = Browser ( " " ) .Page ( " " ) intScrolls = Round ( pgApp . RunScript ( " document.documentElement.scrollHeight / (screen.height) " ) , 2 ) If intScrolls < 1 Then intScrolls = - 1 pgApp . RunScript " window.scrollTo(0, 0); " Wait 1 Browser ( " " ) .CaptureBitmap " C:\screenshot0.png " , True For intScroll = 0 To intScrolls If Environment . Value ( " Browser " ) = " CHROME " Then strScrollPos = " scrollY " Else strScrollPos = " document.documentElement.scrollTop " End If If p

Change IE Browser ZOOM settings

Lot of UI automation testers could have faced this problem as we could change the zoom settings while operating manually for our convenience and forgot to reset to 100%. But our QTP and some other related tools would operate the browser perfectly if browser zoom is 100%. So wee need to change the zoom before start to run the scripts. Its better to have a code snippet in our framework to change this zoom setting right? Here we go... 1. We can simply change the Registry values before Invoking IE Function  ChangeRegistry   Dim  objShell   Set  objShell =  CreateObject ( "WScript.Shell" )  objShell.RegWrite  "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Zoom\ZoomFactor" ,  "100000" ,  "REG_DWORD"   Set  objShell =  Nothing End   Function This option is very useful. But in real time, lot of customers could have restricted write access to windows registry. So we can try other options. 2. Use IE COM object and Change