Search This Blog

Sunday 11 November 2012

Basic Concepts in QTP

 

What is QTP?

This is the topic you should ideally start with. You should have an idea of what QTP is all about, what type of testing you can do with QTP, different types of applications that it supports & other general stuff like what is the latest QTP version, from where you can download QTP, what are the different license modes etc.

What sort of Applications / Test Cases should be considered for Automation using QTP.

Now this is a very important concept that must be looked at before starting with any automation project using QTP. You should be able to analyze your manual test cases and your application to see if it is can be automated or not. If it can be automated, you should be able to figure out if you’d really derive any benefit by automating the test cases.

Get familiar with the QTP tool.

Before starting to create test scripts in QTP, you should be familiar with the QTP tool. You should be aware of the different panes and controls in the QTP window. You should know what they are and for what purpose they are used.

Test Objects and Object Repositories.

You should know what are test objects, what is object hierarchy, how you can identify objects using QTP and how you can identify unique properties for your objects. You should also know what object repositories are, why they are used and how you can use them to add objects.

Creating Test Scripts / Actions.

 Now comes the real scripting part. You should be able to use the record & playback method to create & run test cases. Together with record & playback method, you should be able to ‘write’ your code after associating object repositories to your actions.

Analyzing your Test Run Results.

Once you run your test scripts, you should be able to analyze the test run results. You should be able to find out which steps have passed & which have failed. You should also be able to identify the test case flow using the run results.

Creating Functions & Using Function Libraries.

 You should be able to identify reusable flows & functionality in your test cases. You should be able to write user defined functions for the reusable flows. You should also be able to create new function libraries, add some reusable functions to these function libraries and use these functions in your script by associating the function libraries to your test script.

Working with Data Tables.

You should know how you can use data table to pass on data values in your test script. You should also be aware of how you can take data from excel sheets and use it in your scripts.

Basics of Debugging.

You should have a clear understanding of the basic debugging techniques in QTP. You should know how to use breakpoints in your code. You should also know how you can make use of the debug viewer pane while running your test scripts.

QTP File Handling

Many a times you may need to interact with text files using QTP. Interaction can be(but not limited to) in the form of reading input from a file, writing output to a file.  This post describe in detail "File handling using QTP".
We use FSO object to do this.
What is FSO?
FSO stands for File System Object. This is used to support text file creation and manipulation through the TextStream object and is contained in the Scripting type library (Scrrun.dll)
The FSO Object Model has a rich set of properties, methods and events to process folders and files.
How to create a file?
We first create a FSO object using CreateObject and then create a text file using CreateTextFile.
For Example: Suppose you want to create a file called "test.txt" located in C:

Dim fso, file, file_location
file_location = "C:\file_location"
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set file = fso.CreateTextFile(file_location, True) // True--> file is to be overwritten if it already exists else false
We would use the same example for the rest of this post.
How to open a file?
Set file= fso.OpenTextFile("C:\file_location", ForWriting, True)
//2nd argument can be ForReading, ForWriting, ForAppending
//3rd argument is "True" if new file has to be created if the specified file doesn’t exist else false, blank signify false.
How to read content from a file?
Use ReadLine() method
For example:
Set file= fso.OpenTextFile("C:\file_location", ForReading, True) //2nd argument should always be "ForReading" in order to read contents from a file
Do while file.AtEndofStream <> True
      data = file.ReadLine()
      msgbox data
Loop
How to write content to a file?
You can use  Write() or WriteLine() Methods to write text into a file. The difference between the Write() and WriteLine() Method is that the latter automatically inserts a new line character while the former doesn’t insert a new line character.
For example:
Set file= fso.OpenTextFile("C:\file_location", ForWriting, True) //2nd argument should always be "ForWriting" in order to write contents to a file
file.Write("This is a place to get all your qtp")
file.Write("questions and answers solved.")
//Output will be:
This is a place to get all your qtp questions and answers solved.
while
file.WriteLine("This is a place to get all your qtp")
file.Write("questions and answers solved.")
//Output will be:
This is a place to get all your qtp
questions and answers solved.
How to delete content?
Use DeleteFile() method to delete a file from a particular location
Foe Example:
file_location = "C:\file_location"
Set fso = CreateObject(“Scripting.FileSystemObject”)
fso.DeleteFile(file_location)

Tuesday 6 November 2012

QTP Excel Methods & Sample Programs

Create excel file and enter some data save it


  1. '###############################################  
  2. 'Create excel file and enter some data save it  
  3. '###############################################  
  4.   
  5. 'Create Excel Object    
  6. Set excel=createobject("excel.application")    
  7.     
  8. 'Make it Visible    
  9. excel.Visible=True    
  10.     
  11. 'Add New Workbook    
  12. Set workbooks=excel.Workbooks.Add()    
  13.     
  14. 'Set the value in First row first column    
  15. excel.Cells(1,1).value="testing"    
  16.     
  17. 'Save Work Book    
  18. workbooks.saveas"D:\excel.xls"    
  19.     
  20. 'Close Work Book    
  21. workbooks.Close    
  22.     
  23. 'Quit from Excel Application    
  24. excel.Quit    
  25.     
  26. 'Release Variables    
  27. Set workbooks=Nothing    
  28. Set excel=Nothing    


Reading Values from a Specific excel Sheet

  1. '###############################################  
  2. ' Reading Values from a Specific excel Sheet  
  3. '###############################################  
  4.   
  5. 'Create Excel Object    
  6. Set excel=createobject("excel.application")    
  7.     
  8. 'Make it Visible    
  9. excel.Visible=True    
  10.   
  11. 'Open the Excel File  
  12. Set workbook=excel.Workbooks.Open("D:\excel.xls")  
  13.   
  14. 'Get the Control on Specific Sheet  
  15. Set worksheet1=excel.Worksheets.Item("Sheet1")  
  16.   
  17. ' Display the Values  
  18. Msgbox  worksheet1.cells(1,1).value  
  19.   
  20. 'Close Work Book    
  21. workbook.Close    
  22.     
  23. 'Quit from Excel Application    
  24. excel.Quit    
  25.     
  26. 'Release Variables    
  27. Set worksheet1=Nothing  
  28. Set workbook=Nothing  
  29. Set excel=Nothing    



Deleting Rows from Excel Sheet

  1. '###############################################  
  2. ' Deleting Rows from Excel Sheet  
  3. '###############################################  
  4.   
  5. 'Create Excel Object    
  6. Set excel=createobject("excel.application")    
  7.     
  8. 'Make it Visible    
  9. excel.Visible=True    
  10.   
  11. 'Open the Excel File  
  12. Set workbook=excel.Workbooks.Open("D:\excel.xls")  
  13.   
  14. 'Get the Control on Specific Sheet  
  15. Set worksheet1=excel.Worksheets.Item("Sheet1")  
  16.   
  17. 'Delete Row1  
  18. worksheet1.Rows("1:1").delete  
  19.   
  20. 'Save Excel  
  21. workbook.SaveAs("D:\excel.xls")  
  22.   
  23. 'Close Work Book    
  24. workbook.Close    
  25.     
  26. 'Quit from Excel Application    
  27. excel.Quit    
  28.     
  29. 'Release Variables    
  30. Set worksheet1=Nothing  
  31. Set workbook=Nothing  
  32. Set excel=Nothing    

Add and Delete ExcelSheet

  1. ############################################  
  2. ' Add and Delete ExcelSheet  
  3. '###############################################  
  4.   
  5. 'Create Excel Object    
  6. Set excel=createobject("excel.application")    
  7.     
  8. 'Make it Visible    
  9. excel.Visible=True    
  10.   
  11. 'Open Existing Excel File  
  12. Set workbook=excel.Workbooks.Open("D:\excel.xls")  
  13.   
  14. 'Add New Sheet  
  15. Set newsheet=workbook.sheets.Add  
  16.   
  17. 'Assign a Name  
  18. newsheet.name="raj"  
  19.   
  20. 'Delete Sheet  
  21. Set delsheet=workbook.Sheets("raj")  
  22. delsheet.delete  
  23.   
  24. 'Close Work Book    
  25. workbook.Close    
  26.   
  27. 'Quit from Excel Application    
  28. excel.Quit    
  29.     
  30. 'Release Variables    
  31. Set newsheet=Nothing  
  32. Set delsheet=Nothing  
  33. Set workbook=Nothing  
  34. Set excel=Nothing  

Copy an Excel Sheet of one Excel File to another Excel File

  1. '###############################################  
  2. ' Copy an Excel Sheet of one Excel File to another Excel File  
  3. '###############################################  
  4.   
  5. 'Create Excel Object   
  6. Set excel=createobject("excel.application")  
  7.   
  8. 'Make it Visible    
  9. excel.Visible=True    
  10.   
  11. 'Open First Excel File  
  12. Set workbook1=excel.Workbooks.Open("D:\excel1.xls")  
  13.   
  14. 'Open Second Excel File  
  15. Set workbook2=excel.Workbooks.Open("D:\excel2.xls")  
  16.   
  17. 'Copy data from first excel file sheet  
  18. workbook1.Worksheets("raj").usedrange.copy  
  19.   
  20. 'Paste Data to Second Excel File Sheet  
  21. workbook2.Worksheets("Sheet1").pastespecial  
  22.   
  23. 'Save Workbooks  
  24. workbook1.Save  
  25. workbook2.Save  
  26.   
  27. 'Close Workbooks  
  28. workbook1.Close  
  29. workbook2.Close  
  30.   
  31. 'Quit from Excel Application    
  32. excel.Quit    
  33.   
  34. 'Release Variables    
  35. Set workbook1=Nothing  
  36. Set workbook2=Nothing  
  37. Set excel=Nothing  

Comapre Two Excel Sheets Cell By Cell for a specific Range

  1. ###############################################  
  2. ' Comapre Two Excel Sheets Cell By Cell for a specific Range  
  3. '###############################################  
  4.   
  5. 'Create Excel Object   
  6. Set excel=createobject("excel.application")  
  7.   
  8. 'Make it Visible    
  9. excel.Visible=True    
  10.   
  11. 'Open Excel File  
  12. Set workbook=excel.Workbooks.Open("D:\excel.xls")  
  13.   
  14. 'Get Control on First Sheet  
  15. Set sheet1=excel.Worksheets.Item("Sheet1")  
  16.   
  17. 'Get Control on Second Sheet  
  18. Set sheet2=excel.Worksheets.Item("Sheet2")  
  19.   
  20. 'Give the specific range for Comparision  
  21. CompareRangeStartRow=1  
  22. NoofRows2Compare=4  
  23. CompareRangeStartColumn=1  
  24. NoofColumns2Compare=4  
  25.   
  26. 'Loop through Rows  
  27. For r=CompareRangeStartRow to(CompareRangeStartRow+(NoofRows2Compare-1))  
  28.   
  29. 'Loop through columns  
  30.  For c=CompareRangeStartColumn to(CompareRangeStartColumn+(NoofColumns2Compare-1))  
  31.     
  32.   'Get Value from the First Sheet  
  33.   value1=Trim(sheet1.cells(r,c))  
  34.   'Get Value from the Second Sheet  
  35.   value2=Trim(sheet2.cells(r,c))  
  36.     
  37.   'Compare Values  
  38.   If value1<>value2 Then  
  39.     
  40.    ' If Values are not matched make the text with Red color  
  41.    sheet2.cells(r,c).font.color=vbred  
  42.      
  43.   End If  
  44.     
  45.  Next  
  46.    
  47. Next  
  48.   
  49. 'Save workbook  
  50. workbook.Save  
  51.   
  52. 'Close Work Book    
  53. workbook.Close    
  54.   
  55. 'Quit from Excel Application    
  56. excel.Quit    
  57.     
  58. 'Release Variables    
  59. Set sheet1=Nothing  
  60. Set sheet2=Nothing  
  61. Set workbook=Nothing  
  62. Set excel=Nothing  

Reading complete data from excel file

  1. ###############################################  
  2. ' Reading complete data from excel file  
  3. '###############################################  
  4.   
  5. 'Create Excel Object   
  6. Set excel=createobject("excel.application")  
  7.   
  8. 'Make it Visible    
  9. excel.Visible=True    
  10.   
  11. 'Open Excel File  
  12. Set workbook=excel.Workbooks.Open("D:\excel.xls")  
  13.   
  14. 'Get Control on Sheet  
  15. Set worksheet=excel.Worksheets.Item("raj")  
  16.   
  17. 'Get the count of used columns  
  18. ColumnCount=worksheet.usedrange.columns.count  
  19.   
  20. 'Get the count of used Rows  
  21. RowCount=worksheet.usedrange.rows.count  
  22.   
  23. 'Get the Starting used Row and column  
  24. top=worksheet.usedrange.row  
  25. lft=worksheet.usedrange.column  
  26.   
  27. 'Get cell object to get the values cell by cell   
  28. Set cells=worksheet.cells  
  29.   
  30. 'Loop through Rows  
  31. For row=top to (RowCount-1)  
  32.  rdata=""  
  33.  'Loop through Columns  
  34.  For col=lft to ColumnCount-1  
  35.   'Get Cell Value  
  36.   word=cells(row,col).value  
  37.     
  38.   'concatenate all row cell values into one variable  
  39.   rdata=rdata&vbtab&word  
  40.  Next  
  41.   
  42. 'Print complete Row Cell Values   
  43. print rdata  
  44. Next  
  45.   
  46. 'Close Work Book    
  47. workbook.Close    
  48.   
  49. 'Quit from Excel Application    
  50. excel.Quit    
  51.     
  52. 'Release Variables    
  53. Set worksheet=Nothing  
  54. Set workbook=Nothing  
  55. Set excel=Nothing  


Read complete data from an Excel Sheet content

  1. #############################################  
  2. ' Read complete data from an Excel Sheet content  
  3. '###############################################  
  4.   
  5. 'Create Excel Object   
  6. Set excel=createobject("excel.application")  
  7.   
  8. 'Make it Visible    
  9. excel.Visible=True    
  10.   
  11. 'Open Excel File  
  12. Set workbook=excel.Workbooks.open("D:\excel.xlsx")  
  13.   
  14. 'Get Control on Sheet  
  15. Set worksheet=excel.Worksheets.Item("Sheet1")  
  16.   
  17. 'Get Used Row and Column Count  
  18. rc=worksheet.usedrange.rows.count  
  19. cc=worksheet.usedrange.columns.count  
  20.   
  21. 'Loop through Rows  
  22. For Row=1 to rc  
  23.  'Loop through Columns  
  24.  For Column=1 to cc  
  25.   'Get Cell Data  
  26.   RowData=RowData&worksheet.cells(Row,Column)&vbtab  
  27.  Next  
  28. RowData=RowData&vbcrlf  
  29. Next  
  30.   
  31. 'Display complete Data  
  32. msgbox RowData  
  33.   
  34. 'Close Work Book    
  35. workbook.Close    
  36.   
  37. 'Quit from Excel Application    
  38. excel.Quit    
  39.     
  40. 'Release Variables    
  41. Set worksheet=Nothing  
  42. Set workbook=Nothing  
  43. Set excel=Nothing  


Assign Colours to Excel Sheet Cells, Rows

  1. ###############################################  
  2. ' Assign Colours to Excel Sheet Cells, Rows  
  3. '###############################################  
  4.   
  5. 'Create Excel Object   
  6. Set excel=createobject("excel.application")  
  7.   
  8. 'Make it Visible    
  9. excel.Visible=True    
  10.   
  11. 'Add a New work book  
  12. Set workbook=excel.workbooks.add()  
  13.   
  14. 'Get the Excel Sheet  
  15. Set worksheet=excel.worksheets(1)  
  16.   
  17. 'Coloring Excell Sheet Rows  
  18. Set objrange=excel.activecell.entirerow  
  19. objrange.cells.interior.colorindex=37  
  20.   
  21. 'Coloring Excell Sheet Cell  
  22. worksheet.cells(2,1).interior.colorindex=36  
  23.   
  24. 'Save Excel  
  25. workbook.SaveAs("D:\excel.xls")  
  26.   
  27. 'Close Work Book    
  28. workbook.Close    
  29.   
  30. 'Quit from Excel Application    
  31. excel.Quit    
  32.     
  33. 'Release Variables    
  34. Set objrange=Nothing  
  35. Set worksheet=Nothing  
  36. Set workbook=Nothing  
  37. Set excel=Nothing