Sunday, March 17, 2013

How to compare two images using QTP

We know by using Bitmap checkpoint, we can compare two images.

But is there a other way we can compare?

Yes, we can use "IsEqualBin" method from Mercury.FileCompare

sImgPath1="C:\Users\user\Downloads\SAI 01461_exposure.JPG"
'sImgPath2="C:\Users\user\Downloads\SAI 01462_exposure.JPG"
sImgPath2="C:\Users\user\Downloads\SAI 0175_exposure.JPG"
Set obj=createobject("Mercury.FileCompare")
retVal=obj.IsEqualBin(sImgPath1,sImgPath2,1,1)
print retVal
Set obj=nothing

The retVal=1 if both the images are same
else retVal=0

Monday, March 4, 2013

How to add an image at the end of the Word document

Here i divided the task into 2 steps.
1. Capture the desktop image/application.
2. Insert the captured image at the end of the word document.

sImageFilePath="D:\TempImage.png"
sWordFilePath="D:\Programming Samples\QTP\SampWord.docx"

CapturePrintScreen sImageFilePath
AddImageToWord sWordFilePath,sImageFilePath

Sub CapturePrintScreen(sFilePath)
   Desktop.CaptureBitmap sFilePath,true
End Sub

Sub AddImageToWord(sWrdFilePath,sImgFilePath)
   Const END_OF_STORY = 6
    Const MOVE_SELECTION = 0
   Set oWord=createobject("Word.Application")
    oWord.Visible=true

    set oDoc=oWord.Documents.Open(sWrdFilePath)

    Set oSelection=oWord.Selection
    oSelection.EndKey END_OF_STORY,MOVE_SELECTION

    oSelection.InlineShapes.AddPicture(sImgFilePath)

    oDoc.Save

    oWord.Application.Quit
   
    Set oWord=nothing
End Sub

Thursday, February 7, 2013

QTP unables to identify pop up window


There are couple of scenarios where we perform some operations in a application, where a pop up window will appear like:
1. When you delete a user/emp, it will display a pop up saying "Do you want to Delete the user?"
2. You choose some pop up menu option, then a pop up window may be displayed with the selected items functionality.

For sometimes, even though you have the correct recorded script, it may throw you an error saying the object does not exist.

Ex: B("XYZ").Dialog("Delete User").WinButton("Ok").click    may throw error saying "Object not visible".

How to overcome this error?

Cause: The "visible" property of the Parent object may not enabled.

Solution: Goto Tools -> Object Identification -> Choose Environment type -> Choose the object class(in our case Browser is the parent to the Dialog box) -> Click on Add/Remove property -> select "visible" property.
After visibility property is configured, now re-record the steps.

Now QTP should be able to identify the pop up window.

Tuesday, February 5, 2013

How to get QTP Results in a HTML file

Once we ran our Regression suite, it is handy if the results are displayed in HTML File, it is easy for us to understand at the same time also easy for the management(Lead/Manager/Customer) to look at it.

They will not show any interest if you zip your QTP Result folders and send it to them.

And for many reasons it is handy if the results are displayed in HTML File, right? How can we get the results in HTML File.

But how can we view results in HTML File?

By changing one registry setting, we have get QTP results in a HTML File.

Open windows registry by entering regedit and clicking enter in Windows run.

HKLM\Software\MercuryInteractive\QuickTestProfessional\Logger\Media\Log

Double click on "Active"

Change the value from 0 to 1.

Restart QTP.

Now run your QTP Test and see results in the QTP Test folder, where you see a "Log" folder.

In this folder you will see a file called LogFile.html which is the HTML Report.

Friday, February 1, 2013

How to check the website you are testing is up and running

We can check the website up and running by just pinging the website.

You can do that in couple of ways.

Here is the simplest way to  ping a website.

Appraoch1:

strWebSiteName="www.yahoo.com"
strQuery = "SELECT * FROM Win32_PingStatus WHERE Address = '" & strWebSiteName & "'"
bFlag = False

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set objItems = objWMIService.ExecQuery( strQuery )

For Each objItem In objItems
       If objItem.StatusCode = 0 Then
            bFlag = True
            Exit For
        Else
            bFlag = False
        End If
Next

If bFlag = true Then
    print "Website is avilable"
else
    print "Website not avilable"
End If

Set objItems = Nothing
Set objWMIService = Nothing

Appraoch2:
Set oNetwork = DotNetFactory( "Microsoft.VisualBasic.Devices.Network" ,"Microsoft.VisualBasic")
bFlag=oNetwork.ping(strWebSiteName)
If bFlag Then
  print  "Website is avilable"
Else
   print  "Website not avilable"
End If
Set oNetwork=nothing

Saturday, January 19, 2013

How to check an Excel file is already open

There are no direct methods using Exel/Workbook object to find the functionality.

Work around is, we need to find out all open tasks, and from that we will see whether any task with name "Microsoft Excel"

Here in the below code, i have a sample Excel file with name "SampleXL".

Set Word = CreateObject("Word.Application")
Set Tasks = Word.Tasks
i=0
For Each Task in Tasks
    If instr(Task.Name,"Microsoft Excel - SampleXL")>0  Then
            i=1
    end if
Next
If i=1 Then
    print "Excel file is opened"
else
    print "No excel file opened with the name specified"
End If

Word.Quit

How to retrieve data from Excel file using ADODB

The below code retrieves data from Sheet1 from an Excel file using ADODB.

Both the connection strings specified here works.

Set oCmd=createobject("ADODB.Command")
Set oRS=createobject("ADODB.RecordSet")

sCon="Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=D:\Programming Samples\QTP\SampleXL.xls;"
'sCon="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Programming Samples\QTP\SampleXL.xls;Extended Properties=""Excel 8.0;"""
sQry="select * from [Sheet1$]"

oCmd.ActiveConnection = sCon
oCmd.CommandText=sQry
Set oRS=oCmd.Execute
While not oRS.EOF
    print oRS.Fields(1).Value
    oRS.MoveNext
Wend

oRS.Close

Set oCmd=nothing
Set oRS=nothing

How to verify the existance of an environmental variable

The below code illustrate the existence of an environmental variables:

function checkEnvironmentalVariableExists(sEnvVarName)
    Err.clear
    On error resume next
    envValue=Environment.Value(sEnvVarName) 'Env exist
    If Err.number<>0 Then
        checkEnvironmentalVariableExists=False
    else
        checkEnvironmentalVariableExists=true
    End If
    On error goto 0
end function

Environment.Value("name")="Uday"
retVal=checkEnvironmentalVariableExists("name123")
If retVal=true Then
    print "Environmental Variable exists"
else
    print "Environmenta Variable does not exist"
End If

Tuesday, January 1, 2013

How to check a browser window is minimized

Extern.Declare micLong, "GetMainWindow", "user32" ,"GetAncestor",micLong, micLong 'This is the declaration for the referencing "GetMainWindow" with the GetAncestor method in user32.dll.
GA_ROOT=2

Just opened Gmail and checked whether the browser is minimized or maximized.

We cannot directly use Browser().GetROProperty("minimized") here.


Set oBrowser=description.Create
oBrowser("micclass").value="Browser"
oBrowser("name").value="Gmail.*"

hwnd=Browser(oBrowser).GetROProperty("hwnd")

hwnd = Extern.GetMainWindow(hwnd , GA_ROOT)
msgbox Window("hwnd:=" & hwnd ).GetROProperty("minimized")

The above code returns False if the Tab/browser is maximized else
returns True if the Tab/browser is minimized

The above code worked well with QTP and IE 7.

How to display occurances of a string in a Excel file

Following code helps find the occurrences of a string in a Excel file.

It will return the count as 0, if the string is not found
else returns the number of occurrences of the string

Dim oXLObj,oXLWBObj,olXLWSObj

Function FindStringOccuranceCount(sFileName,iSheetId,sSearchString)
    iCount=0
    set oXLObj=createobject("Excel.Application")
    Set oXLWBObj=oXLObj.workbooks.open(sFileName)
    Set olXLWSObj=oXLWBObj.worksheets(1)

    set cell=olXLWSObj.Range("A:Z").find(sSearchString)
   

    If cell is nothing Then
        CloseExcel()
        FindStringOccuranceCount=iCount
        Exit Function
    End If

    sFirstAddress=cell.address

    Do
        set cell=olXLWSObj.Range("A:Z").FindNext(cell)
        'set CurCell=olXLWSObj.UsedRange.FindNext(sSearchString)
        sCurrentAddress=cell.address
        'sCurrentAddress
        iCount=iCount+1
    loop while not cell is nothing and sCurrentAddress<>sFirstAddress

    CloseExcel()
    FindStringOccuranceCount=iCount

End Function

Function CloseExcel()
    oXLWBObj.close
    oXLObj.application.quit
    Set oXLWBObj=nothing
    Set oXLObj=nothing
End Function

x=FindStringOccuranceCount("C:\Test1.xls",1,"Uday")
msgbox x

Sample Excel file is here: