Tuesday, December 22, 2009

QTP and DotNetfactory – ArrayList

Sponsored Ad

Arraylist contains a list of values, you can add, insert and remove easily. You can add value with no information on the size which makes it a flexible information structure.

To work with this we just need to create an instance of ‘System.Collections.ArrayList’ using CreateInstance method.

Set MyList = DotnetFactory.CreateInstance("System.Collections.ArrayList")

Here are some of the functions which can be used to work with ArrayLists

Add – adds an item in an arraylist

MyList.Add(”ListItem”)

Remove – removes an item from the arraylist

MyList.Remove(”ListItem “)

Insert - inserts an item at a specified position in the array list

MyList.Insert (3, "List Item")

RemoveAt - remove an item from the specified position in the array list

MyList.RemoveAt (3)

Sort - Sort items in the array list

MyList.Sort

Consider a case where you need to sort a list in ascending or descending order.

First create the arraylist and add your list items in the arraylist. We will add five items in the list in random order.

Set MyList = DotnetFactory.CreateInstance("System.Collections.ArrayList")

2.MyList.Add("ListItem5")

3.MyList.Add("ListItem3")

4.MyList.Add("ListItem2")

5.MyList.Add("ListItem1")

6.MyList.Add("ListItem4")

When you add a new item in the arraylist, it adds some empty memory locations, they ought to remove these empty memory locations. To do this they can use ‘TrimToSize’ method, which sets the capacity to the actual number of elements in the arraylist. You can easily understand this with the example below.

Continue reading...

QTP test data directly from external sheet

Sponsored Ad

In a quantity of our earlier posts they have seen How to work on QTP datatable, How to import/export datatable, what are the different functions obtainable for datatable & also the basics of QTP Automation Object Model(AOM).

Here they will merge AOM & Excel automation model to import information from an external excel file to QTP without actually importing the file as a ‘datatable’.

The workflow for the method would be:

1. Generate an excel object.

2. Generate a workbook object that takes file path as input.

3. Generate a sheet name object that takes sheet name as input.

4. Perform operations on sheet.

1. …………………………………

2. …………………………………

3. …………………………………

5. Quit excel application.

6. Destroy objects defined above.

We will show the whole process with an example. It will generate a function that accepts the file path and name of the entry sheet and give the output of an array containing all values of column 1 of the entry sheet.

  1. 1: Function ParamValues(InputFilePath,SheetName)
  2.  
  3. 2: Dim input()
  4.  
  5. 3: Set appExcel = CreateObject(“Excel.Application”) ‘Step 1
  6. 4: Set objWorkBook = appExcel.Workbooks.Open (InputFilePath) ‘Step 2
  7. 5: Set objSheet = appExcel.Sheets(SheetName) ‘Step 3
  8.  
  9. 6: For i=2 to 50 ‘Step 4
  10. 7: if objSheet.cells(i,1).value <> “” then
  11. 8: Input(i-2)=Trim(objSheet.cells(i,1).value)
  12. 9: else
  13. 10: Exit For
  14. 11: End If
  15. 14: Next
  16. 15: appExcel.quit ‘Step 5
  17. 16: Set appExcel=Nothing ‘Step 6
  18. 17: Set objSheet=Nothing
  19. 18: Set objWorkBook=Nothing
  20. 19: End Function

InputFilePath & SheetName input arguments are passed to the function call paramValue. The output of the previous function will be an entry () matrix containing all the values in column 1 of the given Excel worksheet.

You can call the function above by: paramValues,

Continue reading...

Website Updates