Welcome to Automation Fraternity!

Interview Q & A Part 2

7. How on error resume will work for a IF condition if it has error?

On error Resume Next
If (1/0) Then
Msgbox "In True, If part"
Else
Msgbox "In False, Else part"
End If
Output: "In True. If part"

If On error resume next is not used, there will be a vb script run time error,
"Division By Zero"
If error handling is done using, On error resume Next, Control will go to the next statement
i.e. the True part of the If Condition.

8. Ways to return values from a function?
'####################################
'Ways to Return values from Function
'####################################

'a. Single return value

Function Temp()
	Dim x
	X=5
	
	Temp= x
End Function
   
'b.Multiple values return:
Concatenate variables
Function Temp()
	Dim x,y
	X=5
	Y=6
	
	Temp= x & "~"y
End Function

'c. Save Variables in array
Function Temp()
	Dim x,y, aResult(1)
	x=5
	y=6
	aResult (0) = x
	aResult(1) = y
	Temp=aResult
End Function

'd. Return Object
Set ret = Temp()
X = ret.Item(1)
Y= ret.item(2)

Function Temp()
	Dim oDict,x,y
	X=5
	Y=8
	Set oDict = CreateObject("Scripting.Dictionary")
	oDict.Add 1,x
	oDict.Add 2,y
Set Temp= oDict
End Function

'e. Return Multiple values of different Types:

ret = Temp()
ret_1 = ret(0)
ret_2= ret(1)
ret_3 = ret(2)
Set ret_4 = ret(3)
msgbox ret_1 
msgbox ret_2
msgbox ret_3
msgbox  ret_4.FileExists("C:\QueryRandD.txt") 

'Output: 
'5, 8, "AutomationFraternity", 
'True or False dependening on the file availability.
Function Temp()
	Dim fso,x,y,str
	Dim aResult(3)
	X=5
	Y=8
	Str = "AutomationFraternity"
	
	'Object can be of any type of Object:
	' Fso, Dictionary, XML, or QTP 'hierarchy object
	'Set oBrowser = Browser("title:=.*").Page("name:=.*)
	Set fso = CreateObject("Scripting.FileSystemObject")
	aResult(0) = x
	aResult(1) = y
	aResult(2) = str
	Set aResult(3) = fso
	
	Temp= aResult 
End Function

9. What is the use of ReDim and Preserve statement?
'######################################################
'Static and Dynamic Array, Use of Preserve Keyword
'#######################################################

'ReDim is used to Redimensioning the existing 
'"Dynamic Array".
'Case 1: ReSizing Static Array is not Possible
Dim Arr(1)
Arr(0)= "My"
Arr(1) = "Name"

ReDim Arr(2) 'Will give Error
'Above Statement Will give error as Arr is not aDynamic 
'array since its size has already been decided during its 
'declaration

'Case 2: ReSizing Dynamic Array
Dim Arr()’ Dynamic Array
ReDim Arr(1)
Arr(0)= "My"
Arr(1)= "Name"

'Again Resizing
ReDim Arr(2)

'This time array will resize but it will not 
'retain the previous information
'So Preserve Keyword is to be used

'Case 3: ReSizing Dynamic Array using Preserve Keyword
Dim Arr() 'Dynamic Array
ReDim Arr(1)
Arr(0)= "My"
Arr(1) = "Name"

'Again Resizing
ReDim Preserve Arr(2)
Arr(2)="Akash"

'Preserve keyword will retain previous information

10. What is difference between dictionary object and array?
a.Dictionary object has keys for every item, so it is very easy to add or remove any item from the dictionary object. With array adding and removing a cell in between is difficult.
b.Array is indexed while object dictionary is not. So while using "loops" manipulation is quite easy as position of all the elements has predefined indices.
c.Arrays are multi dimension while object dictionary are not. We can have multi dimension object dictionary as well i.e. object dictionary inside another objet dictionary but it is very complicated to handle.
Example of multi dimension Object Dictionary is here:
Set objDict = CreateObject("Scripting.Dictionary")

objDict.Add "Math",CreateObject("Scripting.Dictionary")
objDict.Add "Science",CreateObject("Scripting.Dictionary")
objDict.Add "Social",CreateObject("Scripting.Dictionary")
objDict.Add "Hindi",CreateObject("Scripting.Dictionary")

objDict("Math").Add "Akash",20
objDict("Math").Add "Sumit",30
objDict("Math").Add "Amit",50

objDict("Science").Add "Akash",50
objDict("Science").Add "Sumit",60
objDict("Science").Add "Amit",90

msgbox objDict.Item("Math").Item("Sumit")
'Answer is 30

11. How many ways to write descriptive programming?
Static and Dynamic.

Static description done in action statement itself with help of ":="
Dynamic description by creating object description Object in the script directly.
In static Description OR object can no tbe used if the parent object description is set in the stamen itself.
For example mentioned below:

- Browser("google").Page("google").WebEdit("search").Set "USA" - Correct
- Browser("google").Page("name:=google").WebEdit("search").Set "USA" - Incorrect
- Browser("name:=google").Page("google").WebEdit("search").Set "USA" -Incorrect
- Browser("name:=google").Page("title:=google","html tag=google").WebEdit("name:=search").Set "USA" - Correct

Dynamic Description: See below Code.
Set oDesc = Description.Create
oDesc("micclass").Value = "Weblist"

'Below will take care if any disabled drop down exists on the page
oDesc("disabled").Value = 0

Set oWebListCollection = Browser("Creationtime:=0").Page("title:=.*").ChildObjects(oDesc)
Print "Number of Active Drop downs present on the page are : " & oWebListCollection.Count

For i = 0 to oWebListCollection.Count - 1
        oWebListCollection(i).Select 0
Next
'There are 8 tables on the page. 
'Each table have 10 similar links. Each link 
'in all the tables have same html id as "xyz_link_new". 
'That is, there are 80 links with absolutely similar
'properties. (8 Table* 10 links = 80).
'Each table also has the same html id as "xyz_table_new".
'Write a QTP script to click on 4 th link in 4th table.

'I call this double cascade Description since 
'two descriptions are to be created and one 
'description is used inside another.

Dim oTableChild,i,oChildObj,odescTable,odescLink

'Create Desc for table as all tables has
'the same description
Set odescTable = description.Create

'Create Desc for Link as all links has
'the same description
Set odescLink = description.Create
odescTable("html tag").Value = "Table"
odesctable("htmltag").value = "xyz_table_new"

odescLink("micclass").Value = "Link"
odesclink("htmltag").value = "xyz_link_new"

'Getting all the Table Child Object
Set oTableChild = browser("name:=.*").page("title:=.*").ChildObjects(odescTable)

'Getting all the link child object
'but only which are present under
'specified child table object i.e. 4
set oChildObj = oTableChild(3).ChildObjects(odescLink)

'Clickin on the 4th Link Contained in 4th Table
oChildObj(3).click

File Link:
download file.
it will be saved in txt format.
open the file in notepad.
Clik on save as .
Select "all files" in the drop down.
rename the file and with.html extension 
i.e
temp.html 
a new file will be created.
Open the File in IE to run the Code from QTP.
http://www.automationfraternity.com/forum/index.php?action=dlattach;topic=34.0;attach=1

12. What is Synchronization and How to handle it?
Synchronization refers to sync between QTP and AUT.
After QTP performs an operation it has to wait for application to respond. This is called sync between QTP and Application. This usually happens during navigation. After QTP clicks over a link, and if page or window navigates to the next page, QTP has to wait for the next page or window to appear in order to do next operation.

There are many ways and methods to achieve it:
1.Wait : wait statement waits for specific amount of time. Not recommended as it will keep on waiting even after AUT is ready for next action.
2.Sync :Browser().Page().Sync: Wait till the Browser or Page stops Loading the content of the page. Default time out for this is 60 seconds. If Browser get loaded in 30 secs, QTP will start next operation and will not wait as was happening with wait statement.
Time Out can be altered from File- Settings-Run-Browser navigation Time Out.
This setting is at the test Level.
3.Exist : used against any object for checking the Existence for the same.
4.WaitProperty : Waits till object gets the specified property value.
5.Inbuilt Synchronization point : Only available in Recording Mode, so not much used.

Sometimes above mentioned methods does not work. In such cases custom sync function can also be created to handle sync issues. Cases like, mouse hovering pop up, loading spinning wheel display or table refresh.
Typical example is to wait till the table value refreshes. All the above mentioned sync methods will fail to work here. Custom function is then to be created, which checks the table every 5 seconds if its text property is getting changed or not. If not changed than keep on waiting.

Similarly, for spinning wheel, wait till the wheel exists and its style property is none(style property is to be checked sometimes using DOM in cases where spinning wheel exists in the page all the time but its display is set as None)

13. Can we work on excel files using fso?
Yes. Excel files can be manipulated using FSO as a normal files.
But changing the content of those files is not possible using FSO.
Operations like deleting, Moving, renaming of the xl file can be done by fso
object but changing or reading a cell value cannot be done using fso.
Excel COM object has to be created in order to do it.