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

  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

Post a Comment

Popular posts from this blog

QTP - Object Identification - Prerequesties

While starting a new project or proof of concept or after new QTP installation, we would face the issue in identifying objects as QTP built objects like WinList, WebEdit, etc.; instead everything will be identified as WinObject.

Here are some steps to ensure you did the environment setup RIGHT before start automating.
Did you enabled required Add-ins only while launchingDid you selected "Record >> Record and Run Settings" - This should be based on what type of application you are working with
Windows app - Windows >> Record and run test on any windows applications. Web app - Web >> Record and run test on any open browser.
Did you tried to launch UFT/QTP and then AUT.If its web, ensure browser zoom level set to 100%Try disabling the protected mode in IE for web.Launch UFT and AUT using RUN AS ADMINISTRATORAlso see the Object Identification Methods if you are interested.

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 EndFunction 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 Settings
Function ChangeIEZoom Dim intZoomLevel, objIE  intZoo…

Run JavaScript from QTP

Yeah, You can run your pure JavaScript from QTP using RunScript method. Lot of times, we are in need of firing events or simulating actions on web page which is not supported by QTP. At that time, you can use your direct DOM methods and directly execute your script on the web page from QTP like,
Dim MyPage, SearchBox Set MyPage = Browser("title:=Google").Page("title:=Google") Set SearchBox = MyPage.RunScript("document.getElementsByName('q')(0);") SearchBox.Value="testing"
'if objects available in frames, Set SearchBox = MyPage.RunScript("document.frames(4).document.getElementsByName('q')(0);")
'OR
Set objFrame = Browser("title:=Google").Page("title:=Google").Frame("title:=something")
objFrame.RunScript("document.getElementsByName('q')(0);") Also, you can execute by obtaining actual browser window object if you have IE app like, Dim IEApp Set IEApp = CreateObject("Internet…