Powered by: Dharamveer Saxena

Wednesday, November 25, 2009

Testcomplete: To check if the file exists and to delete a file if it exists

Below is the vbscript code that can be used to check if the file exists on system or not

'In below examples: path is the path of file like "c:\text.txt"

Sub Main
set fs=createObject("Scripting.FileSystemObject")
If not fs.FileExists(path) Then
Log.Message("Fail: file does not exist")
Else
Log.Message("Pass: File exists")
End If

End Sub

Below is the code to delete a file


Sub Delete

set fs=createObject("Scripting.FileSystemObject")
If not fs.FileExists(path) Then
Else
fs.deletefile(path)
End If

End sub

Tuesday, November 24, 2009

TestComplete: To get all the sheets name in your excel workbook

To get all the sheets name from the excel file.

Sub Main

Set p = Sys.WaitProcess("EXCEL")
'this will Make sure you close any Excel work book already open
If p.Exists Then
Call p.Terminate()
End If

'This is setting the driver
Set MsExcel = Sys.OleObject("Excel.Application")

'This will open you workbook
'Specify the location of your excel file
Call MsExcel.Workbooks.Open("c:\test.xls")
MsExcel.Visible = True

'To Loop through all the sheet
for I = 1 to MsExcel.sheets.Count
'To activate the sheet
msexcel.Sheets(I).Activate
'To get the name of the active sheet
MsgBox(MsExcel.activesheet.name)
Next

End Sub

Monday, November 16, 2009

Testcomplete : Data Driven Testing Framework

Data-driven testing is a framework where test input and output values are read from data files (datapools, ODBC sources, cvs files, Excel files, DAO objects, ADO objects, and such) and are loaded into variables in captured or manually coded scripts. In this framework, variables are used for both input values and output verification values. Navigation through the program, reading of the data files, and logging of test status and information are all coded in the test script.

Data-driven testing means using a single test to verify many different test cases by driving the test with input and expected values from an external data source instead of using the same hard-coded values each time the test runs. This way, you can test how the application handles various input without having a lot of similar tests that only have different data sets.


To learn how to perform Data Driven testing using Testcomplete, you can view the following video file: Data Driven Testing Video Tutorial

Thursday, November 5, 2009

TestComplete : To access column of Infragistics Grid using its index

To access column of Infragistics Grid using its index:

------------------------

function get_column(grid, columnindex)
set get_column = grid.DisplayLayout.Bands.Item(0).Columns.Item(columnindex)
end Function

---------------------

You can use this function as bellow to get the header caption of a particular column

sub main
MsgBox( get_column(DIGridControl,4).Header.get_Caption.ToString)
end sub

TestComplete : To access cell of Infragistics UltraGrid using row index and column index

To access Infragistics grid's cell on the basis of row and column index:

-------------------

function get_cell(grid, row, column)
set get_cell = grid.DisplayLayout.Rows.Item(row).Cells.Item(column)
end Function

-------------------

You can use this function as follow to set the value in it:

sub main
'here Ultragrid is the object of grid
call get_cell(Ultragrid, 0, 4).set_Value("checking")
end sub




Wednesday, November 4, 2009

TestComplete : To make a perticular tab selected (Infratistics Ultratabcontrol)

In infragistics Ultratabcontrol clicktab() method do not work correctly to make a tab selected or active. It gets recorded but at the time of play, the testcomplete does not click on the tab.

In this case you can use the following procedure to select a tab.

'--------------------------

'tabcontrol is the name of ultratabcontrol object
'tabname is the name of tab (string)

sub tab_click(tabcontrol, tabname)
dim I
for I = 0 to tabControl.Tabs.Count-1
if tabControl.VisibleTabs.get_Item(I).Text.OleValue= cstr(tabname) then
tabControl.VisibleTabs.get_Item(I).Selected = true
end If
Next
end Sub

'------------------------------------------

Monday, November 2, 2009

Testcomplete : To get cell value from excel (.xls) file

'To get the cell value from .xls file workbook you can use the following function directly by passing the following parameters:

'file is the path of the excel file (string)
'sheet is the name of sheet (workbook) (string)
'row and colum are the index of cell's row and column for which you need to get the value (int)

---------
Function GetExcelData(file,sheet,row,colum)
Dim xlApp, xlFile, xlSheet,val
dim iRowCount,iColCount,cLoop

Set xlApp = CreateObject ("Excel.Application") 'Create access object

Set xlFile = xlApp.Workbooks.Open(file)
'Open the excel file,like"C:\myTestData.xls"
Set xlSheet = xlFile.Sheets(sheet) 'Set the sheet name in which the testdata locates
iRowCount = row
iColCount = colum

For rLoop = 1 To iRowCount 'Fetch value from the first row to the last row
'For cLoop = 1 To iColCount ' If you want to get the value cell-to-cell, you can add this line code
'numAdd = numAdd&" "&xlSheet.Cells(rLoop,cLoop)
GetExcelData = xlSheet.Cells(rLoop,colum)
'Next
Next
xlFile.Close
xlApp.Quit

Set xlSheet = Nothing
Set xlFile = Nothing
Set xlApp = Nothing

end function
---------

You can also use this function to get all the values in a row for all the columns in excel file or vice versa by passing rows or column accordingly.

eg: to get all the values in all the rows for column(index=2), call the function in this way:

'row_count is the row count of the .xls file.

sub main
for row = 1 to row_count
msgbox(cstr(GetExcelData(path,sheet, row,2)))
end sub

OR
'To get all the values in all the columns in row (index = 2) , all the function in this way:

'col_count is the row count of the .xls file.

sub main
for col = 1 to col_count
msgbox(cstr(GetExcelData(path,sheet, 2,col)))
end sub

TestComplete : to get row count of excel sheet using DDT.exceldriver

'To get row count of excel sheet
'path = path of .xls file (c:/test.xls)
'sheet is the name of workbook sheet

function get_row_count(path,sheet)
dim inputdata
dim rowc
set inputData = DDT.ExcelDriver(path, Sheet)
while not inputdata.EOF
inputdata.Next
rowc = rowc+1
Wend
get_row_count = rowc
end Function

TestComplete : To generate random number between two numbers


'To get random number

'Randomize is used as a seed to Rnd so that it will generate a random number each time.
' A simple formula is used to generate a random number between two number: Int((max-min+1)*Rnd+min)


Function makeRandom(min, max)
Randomize
makeRandom = Int((max-min+1)*Rnd+min)
End function

Testcomplete : To check if the value exists in Grid cell


'To check if the value exists in the grid cell

' p_value is the value which you need to check
' grid is the grid in which you need to check
' colid is the ID (string) or index (int) of the column in which you need to check
' you can get the function GetCellValue () in my previous post "TestComplete: To get cell value using Row number and Column ID"

'This function will return 1 if it founds p_value in any of the cell in grid colid column or else will return 0 if does not found the p_value

function value(p_value, grid, colid)
value = "0"
for row = 0 to grid.rows.count -1
if GetCellValue(grid,row,colid) = CStr(p_value) then
value = "1"
end If
Next
End Function

TestComplete : To get cell value using Row number and Column ID


'To get the cell Value based on Grid, row number and column ID
'working in Infragistics grid as well
' Grid is grid object in which you need value for rowIndex row and ColumnId column
' You can pass either ID (string) or Index (int) of the column for ColumnId parameter

Function GetCellValue (Grid, RowIndex, ColumnId)
GetCellValue = Grid.wValue(rowindex,columnID)
end Function

TestComplete : Connect to SQL database and fetch data by executing query



' Sample Code to Connect to database in Test Complete
' Database connection using Testcomplete


Sub main

Set AConnection = ADO.CreateADOConnection

AConnection.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=master;Data Source=DG18\SQLEXPRESS"

AConnection.LoginPrompt = False ' Suppress the login dialog box
AConnection.Open

Set RecSet = AConnection.Execute_("SELECT * FROM holding") ' Execute a simple query
RecSet.MoveFirst
While Not RecSet.EOF
Log.Message RecSet.Fields("Script").Value
RecSet.MoveNext
Wend
AConnection.Close
End sub