Thursday, 6 December 2012

Running Automation Script when the system is locked

Problem Statement

There can be situations when you need to execute your QTP scripts so that they can run when you are not present in front of your PC. But the problem is if the script execution takes more than 10 minutes or longer, the machine gets locked automatically due to security reasons. There are various options available for the system to not to get locked by making some changes in Windows Environment (registry,disable screen saver). This is again a security violation and also one should never leave the system unlocked.

In this situation, it has been observed that QTP does not work properly, however scripts can be started in locked mode but there is no guarantee that QTP will be able to interact GUI application. In most of the cases it is the Window message pop-up causes the issue. whenever QTP has to interact with such a pop-up it stuck at the place and this impacts in the script execution, in other cases it works fine.

Reason why QTP scripts does not work while system is locked

When the machine is locked, Windows disables windows messages, which is required to perform a window operation.There are some methods in QTP which uses these system messages to communicate and interact with the application. On a locked system, these QuickTest Professional methods are unable to replay. This is a limitation of the O/S that is preventing the QuickTest Professional script to replay in a locked mode. If we can send the messages that are required to perform a particular operation through QTP, then this problem can be sorted out and script execution can be done while the system is locked.

Solution for the above problem

To overcome the above problem we need to use Windows API in QTP.
Below code help us to work with the QTP objects while the screen is locked.

Function HandleDialog
'Include Win API
Extern.Declare micHwnd, "FindWindowEx", "user32.dll", "FindWindowExA", micHwnd, micHwnd, micString, micString
Extern.Declare micLong, "GetWindowText", "user32.dll", "GetWindowTextA", micHwnd, micString + micByRef, micLong
Extern.Declare micLong, "GetWindow", "user32.dll", "GetWindow", micLong, micLong
Extern.Declare micLong, "PostMessage", "user32.dll", "PostMessageA", micHwnd, micLong, micLong, micLong

Const GW_HWNDNEXT = 2 'Retrieve Text Constant
Const WM_COMMAND = &H111& 'Send command

pHwnd = extern.FindWindowEx(0,0, vbNullString, "Windows Internet Explorer") 'Get Window Handle
Hwnd = extern.FindWindowEx(pHwnd,0,"Static" ,vbNullString) 'Get Text Handle
'Find All the Text that are there in the window
Do until Hwnd = 0
staticText=Space(256)
Extern.GetWindowText Hwnd,staticText, 256
Result = Result + staticText
Hwnd = extern.GetWindow(Hwnd, GW_HWNDNEXT)
Loop
'Send click message to click on 'Yes' button
lRes = extern.PostMessage(pHwnd, WM_COMMAND, 6, 0)

Print lRes
HandleDialog = True
End Function

Just Open QTP and Paste the above function and the below statement in a function library -
RegisterUserFunc "Dialog","Exist","HandleDialog"

Associate the function library to the Test and its ready.
The function can be broken into parts or extended to handle as per the requirement, like retriving the window text, click on a particular button etc

Sunday, 9 September 2012

Copying Excel data from one excel file to another file using QTP Automation

Step wise approach for copying excel data from one excel file to another(when the destination workbook is already created):-
  1. Create the excel object
  2. Open the source workbook(excel whose data need to be copied)
  3. Open the source worksheet(sheet whose data need to be copied)
  4. Now,open the excel workbook in which we need to copy the data(say destination workbook)
  5. Open the destination worksheet
  6. Copy the data from source workbook and paste into destination worksheet.
  7. Save the destination workbook.
  8. Set excel object to nothing to free the allocated memory to object.
Function CopyData(SourceExcel,DestnExcel,SourceSheetName,DestnSheetName)
    Set ExcelObj=CreateObject("Excel.Application")
    Set SourceWorkBook=ExcelObj.WorkBooks.Open(SourceExcel)
    Set SourceWorkSheet=SourceWorkBook.WorkSheets(SourceSheetName)
    Set DestinationWorkBook=ExcelObj.WorkBooks.Open(DestinationExcel)
    Set DestinationWorkSheet=SourceWorkBook.WorkSheets(DestinationSheetName)
    SourceWorkSheet.UsedRange.Copy
    DestinationWorkSheet.Range("").PasteSpecial Paste=xlValues
    DestinationWorkBook.Save
    DestinationWorkBook.Close
    SourceWorkBook.Close
    Set ExcelObj=Nothing  
End Function

In the  function ,I have assumed that the destination excel file is already created.    

Introduction to Quick Test Professional

Quick Test Professional (QTP) is one of the renowned automation testing tools used for performing the functional and regression testing.

It was originally developed by Mercury Interactive but later Mercury Interactive was acquired by Hewlett Packard (HP) in 2006.

ALM (Application Life Cycle Management) or HP Functional Testing 11.0 commonly known as QTP 11.0 is the latest version of QTP.

Features:
  • Uses VBScript language to specify a test procedure, and for the manipulation the objects and controls of the application under test (AUT).
  • Supports two interfaces i.e. keyword and expert view.
  • Mainly used for automation testing  of User Interface(UI) based Application but also used for automation testing of Non UI based application like Database testing, Web Services Testing and File System operations.
  • All the AUT objects are stored in the object repositories.
  • It’s licensed software.    
Apart from these there are many other features which we will discuss in the coming articles.


Step to Step guide for installation of the QTP 11.0:- 

1.   Go to QTP download location(Click Here) 

2.   Click on Trial Software Section and click on the HP Functional Testing 11.00 Evaluation.

                3.   Filled in the required details and proceed to Terms of Service by clicking on Next.


               4.  Click on the “I AGREE” button to proceed to download Evaluation software.

              5.  Download the evaluation pack by clicking on the download link. The software is about of 3 GB so it    will take about 3-4 hrs depending on your net speed.

         6.  Once downloaded, you will get a zip file. Extract this zip file.
         7.  Once extracted, go inside the folder HP_QTP_11.00_Eval_Package_Eng_SW_E-Media_T6508-15000\QTP 11 Evaluation\QTP 11 Installation Files and double click on Setup.exe file.
         8.  Click on the QuickTest Professional Setup link as shown.


         9.  Click on Next button.


          10.  Check “I AGREE” radio button and click on Next.


          11.  Enter required details and click on Next button.


          12.  If you are beginner don’t do anything click on Next else you can select the Addin required and click on next.

          13.  Select the path where you want to install QTP and click Next


          14.  Now installer is ready to install QTP window will come, simply click Next


          15.  QTP will begin to install, once it is installed click on Next button.



          16.  Click on Finish Button and your QTP is installed.


          17.  When you will open QTP for the first time, it will ask for additional requirements click on Run.
          18.  License Window will pop up, if you want to use trial version simply click Cancel and 30 days trial version will be installed.
          19. Time to start using QTP now.