07 November 2017

Write a If condition without using End If in UFT

A = 20 : B = 10

If A > B Then: MsgBox "A is greater than B"


...

How To Set Function name as a String Value In UFT


Function GreetMe(a)
  MsgBox a 
Endfunction

Greet = "GreetMe ""Hello"""


Execute Greet    'Returns Hello



...

Difference between Array and Dictionary Object in UFT

1. Dictionary cannot be multidimensional, while an array can be multidimensional
2. QTP does not have any concept like dynamic Dictionary, but Array can be Dynamic
3. When we delete one particular item from the dictionary, all subsequent item automatically shift up.
4. Array can have index only as numeric (0,1,2..) while we can  use keys to identify dictionary items and Keys can be of any data subtype(String, integer)
5. No need to set the size of the dictionary object in QTP, while initializing an Array is must. And If we want to increase the array size in script we can use Redim and Redim Preserve statement to increase the array.
6. Various methods available for dictionary object like Exists, Items, Keys, Remove, RemoveAll
7. QTP does not have any method to release the memory in case any element is not required, while in Dictionary object we can delete any element if it is not required

Example:

'Declaring array 
Dim arr()
'Resizing Array
ReDim arr(2)
arr(0) = "UFT14"
arr(1) = "QTP11"
arr(2) = "QTP10"

msgbox arr(0)   'Returns UFT14


'Creating Dictionary Object
Set obj = createobject("scripting.dictionary")
'Using Add methods to add values to dictionary object
obj.Add"UFT14","Version 14"
obj.Add"QTP11","Version 11"
obj.Add"QTP10","Version 10"

'Locating items in Dictonary
MsgBox obj.item("UFT14")   'Returns Version 14


....

Add Parameter to a Function without affecting othere scripts using the same Function

x = add(10,15,Array(10,15))
MsgBox x

Function add(a,b,c)

    IfIsArray(c) Then
        t = 0
        For i = 0Toubound(c)
            t = t + c(i)
        Next
        Else
        t = c
    EndIf
    r = a+b+t
    add = r


EndFunction



...

06 November 2017

Error handling in UFT / VBScript

'Will Returns the Error.no and corresponding Error description


OnErrorResumeNext

For i = 1To10

    Err.Raise i
    MsgBox"Err no: = " & Err.Number & "  Desc: = " & Err.Description


Next


...

Count No.of Word in a Sentence without using Split function in UFT

Using Split Function:

str = "simple is sample str"

b = Split(str)
MsgBoxUBound(b)+1   'Returns 4



Without using Split Function:

str = "simple is sample str"
count = 1

For i = 1ToLen(str)
    
    b = Mid(str, i, 1)
    
    IfNot b = " "Then
        t = t &b
        Else
        count = count+1
    EndIf
    
Next

MsgBox count    'Returns 4



...

Find Total no.of Files present inside any Folder using UFT

FPath = InputBox("Enter the Folder Path") 'Get Folder Path

Set obj = Createobject("Scripting.FileSystemObject")
Set fol = obj.GetFolder(FPath ) 'Folder Path
Set fis = fol.Files

For each fs in fis
    count = count + 1
Next

Msgbox count ' returns the total no.of files present inside Folder



...

Finding the Highest And Lowest value from an Array using UFT

Finding the Highest value:

a = array(4,5,8,9,1,7)

For i = 0Toubound(a)
    If a(i) > a(0) Then
        a(0) = a(i)
    EndIf
Next

MsgBox a(0)   'Returns 9



Finding the Lowest value:

a = array(4,5,8,9,1,7)

For i = 0Toubound(a)
    If a(i) < a(0) Then
        a(0) = a(i)
    EndIf
Next

MsgBox a(0)   'Returns 1



...

Difference between ByVal And ByRef In UFT

Function test(ByVal var)
    
    info = "test"
    msgbox var
    msgbox info
    
EndFunction

info = "test1"
Call test(info)  'byval - test1, test  


Function test(ByRef var)
    
    info = "test"
    msgbox var
    msgbox info
    
EndFunction

info = "test1"
Call test(info)  'byref - test,test


...

Get Length of the String without using Len Function ( 2 different ways )

Option 1:

str = "ABCDEFG"

var = InStrRev(str, Right(str,1))


MsgBox var 'Return 7 - position of G


Option 2:

str = "ABCDEFG"
i = 0
DoUntil str = ""
    i = i+1
    b = Left(str, 1)
    str = Replace(str, b, "")
Loop


MsgBox i   'Return 7


...

Difference between InStr And InStrRev function ..

str = "abcdeabcde"

MsgBoxInStr(str, "e") ' search from start, will return 5
MsgBoxInStrrev(str, "e")   ' search from end, , will return 10

...

Filter Array value using UFT

a= array("Red","Blue","Yellow")

b = Filter(a,"B")
c = Filter(a,"e")
d = Filter(a,"Y")

Foreach x in b
  MsgBox"Filter 1: " & x 'Return Blue
Next

Foreach y in c
  MsgBox"Filter 2: " & y 'Return Red,Blue,Yellow
Next

Foreach z in d
  MsgBox"Filter 3: " & z 'Return yellow
Next


....

Creating XML Document using UFT ( 2 different ways )

Option 1:


Set xmlDoc = CreateObject("Microsoft.XMLDOM")

Set objRoot = xmlDoc.createElement("ParentNode")  
xmlDoc.appendChild objRoot  

Set objRecord = xmlDoc.createElement("ChildNode") 
objRoot.appendChild objRecord 

Set objDate = xmlDoc.createElement("AddChildData")  
objDate.Text = Date  
objRecord.appendChild objDate  

Set objIntro = xmlDoc.createProcessingInstruction("xml","version='1.0'")  
xmlDoc.insertBefore objIntro,xmlDoc.childNodes(0)  

xmlDoc.Save "C:\Data\Rec\Audits.xml"



Option 2:


set xmlDoc = createObject("MSXML2.DOMDocument.3.0")

Set objRoot = xmlDoc.createElement("ParentNode")  
xmlDoc.appendChild objRoot  

Set objRecord = xmlDoc.createElement("ChildNode") 
objRoot.appendChild objRecord 

Set objDate = xmlDoc.createElement("AddChildData")  
objDate.Text = Date  
objRecord.appendChild objDate  

Set objIntro = xmlDoc.createProcessingInstruction("xml","version='1.0'")  
xmlDoc.insertBefore objIntro,xmlDoc.childNodes(0)  

xmlDoc.Save "C:\Data\Rec\Audits.xml"



.....

Class Implementation in UFT

Class Calculate
    Function Add(a,b)
        Add = a + b
    EndFunction

    Function Multiply(a,b)
        Multiply = a * b
    EndFunction

EndClass

Set calc = New Calculate

val1 = calc.Add(20,30)   'Return 50
MsgBox"Add = "&val1

val2 = calc.Multiply(5,4)   'Return 20
msgbox"Multiply = "&val2



...

Multi demension Array in UFT ( different ways of initializing and accessing )

Option 1:

Dim arr(2,2)   
arr(0,0) = "1"
arr(0,1) = "2"
arr(0,2) = "3"
arr(1,0) = "4"
arr(1,1) = "5"
arr(1,2) = "6"
arr(2,0) = "7"
arr(2,1) = "8"
arr(2,2) = "9"

MsgBox arr(1,0)   'Return 4
MsgBoxUBound(arr,1)   'Return 2



Option 2:

a1 = Array(Array(1, 2, 3), Array(4, 5), Array(6, 7, 8, 9))

MsgBox a1(2)(0) 'Return 6
MsgBoxUBound(a1,1) 'Return 2



Option 3:

a  = array(2,1,4)
a(0)= Array(1,2,3)
a(1) = Array(4,5)
a(2) = Array(6,7,8,9)

MsgBox a(2)(0)   'Return 6
MsgBoxUBound(a,1)   'Return 2




Option 4:

Dim arr(2,4,8)   
arr(0,0,0) = "1"
arr(0,0,1) = "2"
arr(0,0,2) = "3"
arr(1,0,0) = "4"
arr(1,0,1) = "5"
arr(1,0,2) = "6"
arr(2,0,0) = "7"
arr(2,0,1) = "8"
arr(2,0,2) = "9"

MsgBox arr(1,0,1)   'Return 5
MsgBoxUBound(arr,1)   'Return 2
MsgBox arr(2,0,0)   'Return 7
MsgBoxUBound(arr,2)   'Return 4



...

Implementing recursion function using UFT ( calling a function from inside )

i = 0

Function num(no)
i = i+1
b = i & vbNewLine


MsgBox b
    
    If i<no Then
        Call num(no)
    End If
        
End Function

Call num(10) ' Print 1,2,3 ... 10



...

Print 1 To 10 without using any Looping concept ( ascending and descending order )

Ascending  Order:

i = 0
Function num(no)
i = i+1
b = i & vbNewLine
    MsgBox b
    If i<no Then
        Call num(no)
    End If
    
End Function

Call num(10) ' Print 1,2,3 .... 10



Descending Order:

i = 0
Function num(no)
i = i+1
b = i & vbNewLine
    
    If i<no Then
        Call num(no)
    End If
    
    MsgBox b
    
End Function

Call num(10) ' Print 10,9,8 ...1


...

Accessing Web Table using UFT

Set brw = Browser("micclass:=Browser").Page("micclass:=Page"

Row = brw.WebTable("micclass:=WebTable""index:=0").RowCount
col = brw.WebTable("micclass:=WebTable""index:=0").ColumnCount(1)

Msgbox "Row : " & Row & " Col : " & col 

brw.WebTable("micclass:=WebTable""index:=0").ChildItem(2,1,"WebButton",0).click
brw.WebTable("micclass:=WebTable""index:=0").ChildItem(2,2,"WebCheckBox",0).set "ON"
brw.WebTable("micclass:=WebTable""index:=0").ChildItem(2,3,"WebEdit",0).set "Test1"
brw.WebTable("micclass:=WebTable""index:=0").ChildItem(2,2,"WebCheckBox",0).set "OFF"
b = brw.WebTable("micclass:=WebTable""index:=0").ChildItem(2,5,"WebList",0).GetROProperty("items count")
b1 = brw.WebTable("micclass:=WebTable""index:=0").ChildItem(2,5,"WebList",0).GetROProperty("all items")

Msgbox "Total Items : "&b&" Items are: "&b1
brw.WebTable("micclass:=WebTable""index:=0").ChildItem(2,5,"WebList",0).Select "Mercedes"
brw.WebTable("micclass:=WebTable""index:=0").ChildItem(2,5,"WebList",0).Select "Audi"
Browser("micclass:=Browser").Page("micclass:=Page").WebEdit("index:=1").Set "Test2"

Browser("micclass:=Browser").Page("micclass:=Page").WebRadioGroup("index:=1").Select "choice2"

Browser("micclass:=Browser").Page("micclass:=Page").WebCheckBox("index:=1").Set "ON"
Browser("micclass:=Browser").Page("micclass:=Page").WebList("index:=1").Select "D"

l1 = Browser("micclass:=Browser").Page("micclass:=Page").WebList("index:=1").GetROProperty("items count")   'Get total number of items present in a List

l2 = Browser("micclass:=Browser").Page("micclass:=Page").WebList("index:=1").GetROProperty("all items")   'Get All the Items name present in a List

Msgbox "Total Items : "&l1&" Items are: "&l2
Msgbox "Total Items : "&l1&" Items are: "&l2



... Use this HTML page to test the above code ...

Save this with .html and then perform the above operations - 

<!DOCTYPE html>
<html>
<body>
<h1>First Heading</h1>
<p>First paragraph ... </p>

<table class="dynatable">
        <thead>
            <tr>
                <th><button class="add">Add</button></th>
                <th>ID</th>
                <th>Name</th>
                <th>Col 3</th>
                <th>Col 4</th>
                <th><button style="width: 100px; height: 25px" class="addColumn">Add Column</button></th>
            </tr>
        </thead>
        <tbody>
            <tr class="prototype">
                <td><button class="remove">Remove</button>
                <td><input type="checkbox" name="id[]" value="" class="id" /></td>
                <td><input type="text" name="name[]" value="" /></td>
                <td><input type="text" name="col4[]" value="" /></td>
<TD class = "select">Online Password
<select>        
                <option value="volvo">Volvo</option>
                <option value="saab">Saab</option>
                <option value="mercedes">Mercedes</option>
                <option value="audi">Audi</option>
        </select>
           </tr>
    </table>

<p>Second paragraph ... </p>

<p>
<input type="text" name="mail1" size="25">
<input type="text" name="mail2" size="25">
<input type="text" name="mail3" size="25">
</p>
<p>
<input type="radio" name="choices1" value="choice1">
<input type="radio" name="choices2" value="choice2">
<input type="radio" name="choices3" value="choice3">
</p>
<p>
<input type="checkbox" name="checkbox1">
<input type="checkbox" name="checkbox2">
<input type="checkbox" name="checkbox3">
</p>
<p>
<select name="continent" size="1">
  <option value="A">A</option>
  <option value="B">B</option>
  <option value="C">C</option>
  <option value="D">D</option>
  <option value="E">E</option>
</select>
</p>

</body>
</html>


05 November 2017

Generate Random Character using UFT

Const str = "abcdefghijklmnopqrstuvwxyz"

Num = cint(Inputbox("How many random char you want?"))

Randomize()

For i = 1 To Num
    strName = ""
    For j = 1 To Num
        k = Fix(26 * Rnd())
        strChar = Mid(str, k + 11)
        strName = strName & strChar
    Next
Next

Msgbox strName

Multiply 2 numbers without using (*) Operatior

 n1 = 10 : n2 = 20

For i = 1 To n1
    For j = 1 To n2
        b = b & "A"
    Next
Next

Msgbox Len(b)     'Returns 200

AOM - Automation Object Model

'Creating QuicTest Object Set  qtapp =  createobject ( "QuickTest.Application" ) 'Launching the Application If  qtapp...