Frequently Asked Coding Questions During Interview on UFT

17
623
Spread the love
  • 15
  • 12
  • 6
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
    34
    Shares

 


Sub Macro1()
Call midexplained("Animesh", 2, 2)
'same as mid("Animesh",2,2)
End Sub
Function midexplained(inputq, strtcut, length)
Dim bText() As Byte
Dim j As Integer
j = 0
Dim k As Integer
k = 0
Dim a(999)
Dim n As String
Dim MyString As String: MyString = inputq
bText = MyString
For i = 0 To UBound(bText) - 1
If (bText(i) = 0) Then
Else
a(j) = Chr(bText(i))
j = j + 1
End If
Next
For k = 0 To UBound(a) - 1
If a(k) = "" Then
ElseIf k + 1 >= strtcut And k < ((strtcut + length) - 1) Then
n = n & a(k)
Else
End If
Next
MsgBox "value of n is " & trim(n)
End Function

The output for both the variable is the same.

How to Swap two number or two input strings?


sinp1=Inputbox("Enter the First string:")
sinp2=Inputbox("Enter the second string:")
scon=sinp1&sinp2
msgbox scon
lensinp1=len(sinp1)
lensinp2=len(sinp2)
lenscon=len(scon)
msgbox lensinp1
msgbox lensinp2
msgbox lenscon
'For i=1 to lenscon
'smid1=mid(scon,i,lensinp1)
'Next
'msgbox smid1
srght=right(scon, lensinp2)
msgbox "The swapped last name: " &srght
sleft=left(scon, lensinp1)
msgbox "The swapped first name: " &sleft
scon2=srght&sleft
msgbox "The swapped string:" & scon2
 

How to find the Dedcimal value of a given hexadecimal and vice varsa?


sinp=Inputbox("Enter the Hexadecimal number::")
slen=len(sinp)
c=0
For i=1 to slen
smid=mid(sinp,i,1)
If isnumeric(smid)=False Then
Select Case smid
Case "A" n=10
Case "B" n=11
Case "C" n=12
Case "D" n=13
Case "E" n=14
Case "F" n=15
End Select
c=c+cInt(n)*(16^(slen-i))
Else
c=c+cInt(smid)*(16^(slen-i))
End If
Next
msgbox ("The Decimal Equivalentof the Hex number:: "&sinp&"=" &c)

How to find out if a string is having numeric values or not?


sinp=Inputbox("Enter the String::")
slen=len(sinp)
For i=1 to slen
smid=mid(sinp,i,1)
If isnumeric(smid) Then
snum=snum & smid
tfchk=True
else
tfchk=false
End If
Next
If tfchk=True Then
msgbox snum
End If
 

Design a basic calculator that takes two input and and an operation as input and provide us the result.


Option explicit
Dim x,y, Operation, Result
x= Inputbox (" Enter x value")
y= Inputbox ("Enter y value")
Operation= Inputbox ("Enter an Operation")
Select Case Operation
  Case "add"
      Result= cdbl (x)+cdbl (y)
      Msgbox "Addition of x,y values is "&Result
  Case "sub"
      Result= x-y
      Msgbox "Substraction of x,y values is "&Result
  Case "mul"
    Result= x*y
      Msgbox "Multiplication of x,y values is "&Result
  Case "div"
      Result= x/y
      Msgbox "Division of x,y values is "&Result
  Case "mod"
      Result= x mod y
      Msgbox "Mod of x,y values is "&Result
  Case "expo"
      Result= x^y
      Msgbox"Exponentation of x,y values is "&Result
  Case Else
      msgbox "Wrong Operation"
End Select
 

How to Resize a window in QTP/UFT?


Syntax:
Max:
Objectname.maximize
min
Objectname.minimize
Restore:
Objectname.Restore
Move: This is not exactly a resizing command
Objectname.move x,y
 

How to extract numbers from String?

For example :

  • If we pass “There are 40 boys “, the function should return us 40.
  • Similarly, if we pass “Ticket is confirmed and the no is 56477” to the function , it should return me 56777.

Function extract_number(msg)
Dim re, matches, item, result
Set re = New RegExp
re.pattern = "[A-Za-z -.]*(d+).*"
Set matches = re.Execute(msg)
If matches.Count > 0 Then
Set item = matches(0)
If item.SubMatches.Count > 0 Then
result = item.SubMatches(0)
Else
result = -1
End If
Else
result = -1
End If
extract_number = result
End Function
MsgBox extract_number("There are 40 boys")
MsgBox extract_number("Ticket is confirmed and the no is 56477")

Another way:


mystring = "The order is confirmed and its no is 567"
myLength = Len(mystring)

For i = 1 To myLength
If Asc(Mid(mystring, i, 1)) <> 32 Then
If Asc(Mid(mystring, i, 1)) >= 48 And Asc(Mid(mystring, i, 1)) <= 57 Then
myNumber = myNumber & Mid(mystring, i, 1)
End If
Else
msgbox(“no numeric”)
End If
Next
msgbox(myNumber)

READ  Get Column Count in Excel

How to Ping Other computer using VBScript?

This is a sample code on how to network with another computer. This will automatically open up the command prompt and ping to the particular machine. For combating the Network latency and delay I have used some delay in the programming too.
This is mostly applicable to the networking team and those who use com/dcom.


Set oWshShell = CreateObject("wscript.Shell")
Cmd = "ping 58.2.122.76"
Set oExec = oWshShell.Exec(Cmd)
Do While oExec.Status = 0
WScript.Sleep 60
Loop
0utput= oExec.StdOut.ReadAll
error_code = oExec.StdErr.ReadAll
MsgBox "Output: " & output & vbNewLine & vbNewLine & "Errors: " & error_code

Find out a number of occurrence of Substring within a string?

Syntax–


Call OSS(str,substring)
Function OSS(str,sbstring)
occ=split(str,sbstring)
oss=ubound(occ)
End Function
Exp--msgbox OSS("anianianianiani","ani")

how we can measure the time required for each component to execute in QTP?

This “TimeIt” will give us the time required to execute the particular component.This is applicable to any script along with components.


'//In the head of the script...
Dim StartTime, EndTime
StartTime = Timer
....your code
EndTime = Timer
'//In the last line of the script component...
TimeIt = EndTime - StartTime

Write a code in QTP which will close a particular opened window in Desktop. Say I said only opened Notepads needs to be closed.


Set winobj=description.Create()
Set objdesk=desktop.ChildObjects(winobj)
cntobj=objdesk.count()
msgbox cntobj
sdt=Datatable.GetSheet("Global").GetParameter("OpenedApp")
cntrow=DataTable.GetSheet("Global").GetRowcount()
msgbox cntrow
For i=1 to cntobj
winapp1=objdesk(i).GetROProperty("text")
If Winapp1=sdt Then
.....
End If
Next

Write code on calling an action say the first action will take some input and will do some operation. Now the output of Action 1 will pass to Action 2. The output of Action-2 will go to Action-3. After finishing it will come back to Action1 and give the final output.


Action-1
a=inputbox("Enter a number:")
b=inputbox("Enter a number:") 
isum=cint(a)+cint(b) 
RunAction "Action2", oneIteration,isum,output 
msgbox output 
Action-2 
imul=Inputbox("Enter a number to multiply:") 
pmul=Parameter("C")*imul 
Parameter("OUT")=pmul 
RunAction "Action3", oneIteration, pmul, out
Parameter("OUT")=out 
Action-3 
Parameter("LAST")=Parameter("IN3")+5 
//Split a string and find the length 
sinp=inputbox("Enter the Path:") 
strsplit=split(sinp,"") 
slen=len(sinp) 
cnt=0 For i=1 to slen 
sms=mid(sinp,i,1) 
If sms="" Then 
cnt=cnt+1 
End If 
Next 
msgbox strsplit(cnt)