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.
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.
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.
- Dim dic
- Set dic = CreateObject("Scripting.Dictionary")
- dic.Add "Name", "Shanmugavel"
- dic.Add "Age", "26"
- dic.Add "City", "CBE"
- dic.Add "Height", "5.4"
- 'To Check a particular key exists or not
- If dic.Exists("Weight") Then
- msgbox "Exists"
- Else
- msgbox "Not Exists"
- End If
- 'To loop through all keys present in dic object
- For Each key in dic
- msgbox dic.Item(key) 'Here we are using property "Item" to get a key value
- Next
- 'To change a key name without altering the value associated to that key
- dic.Key("Age") = "MyAge"
- msgbox "Value for MyAge: " + dic.Item("MyAge")
- msgbox "Check for Age Exists: " + CStr( dic.Exists("Age") )
- 'To get count of available Key-Value pairs
- msgbox dic.Count
- 'To get all key names only
- Dim KeyNames
- KeyNames = dic.Keys 'Returns an array
- For Each key in KeyNames
- msgbox key
- Next
- 'To get all key values only
- Dim KeyValues
- KeyValues = dic.Items 'Returns an array
- For Each value in KeyValues
- msgbox value
- Next
- 'To Remove a key-Value pair
- msgbox "Before Removing a key-Value pair, Count: " + CStr( dic.Count )
- dic.Remove("Height")
- msgbox "After Removing a key-Value pair, Count: " + CStr( dic.Count )
- 'To Remove all key-Value pairs
- dic.RemoveAll
- msgbox "After Removing all key-Value pairs, Count: " + CStr( dic.Count )
- 'To make key names Case-Sensitive
- ' -- By default, VBScript considers everything as Case-Insensitive because its a Case-Insensitive scripting language.
- ' -- 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
- ' -- VBBinaryCompare - 0; VBTextCompare - 1; VBDataBaseCompare - 2 (compares information inside)
- dic.CompareMode = VBBinaryCompare 'or assign value 0
- dic.Add "Name", "shanmugavel c"
- dic.Add "NAME", "SHANMUGAVEL C"
- msgbox "Name: " + dic.Item("Name")
- 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.
- var WS = new ActiveXObject("WScript.Shell");
- //Declaring and Assigning values with key-value pairs
- var obj = { Name: "ShanmugavelC", Age: "26",
- City: "CBE", Height: "5.4" };
- //To get all key-value pairs
- for (key in obj)
- WS.Popup("Value for " + key + ": " + obj[key]);
- //To add a Key-value pair
- obj["Weight"] = "69";
- //To check a key exists or not
- if( obj.hasOwnProperty("State") )
- WS.Popup("Key State Exists");
- else
- WS.Popup("Key State not Exists");
- //By default, JavaScript is Case-Sensitive
- obj["NAME"] = "SHANMUGAVEL C"
- WS.Popup( "Value for NAME: " + obj["NAME"] );
- WS.Popup( "Value for Name: " + obj["Name"] );
Comments
Post a Comment