Powered by: Dharamveer Saxena

Tuesday, June 29, 2010

How to handle null strings

In TestComplete whenever DDT diver or array or anything returns null or nothing instead of a string value then it gives error message. For example if you are fetching values using the DDT driver as follow it will return you the data string from the excel file if the value exists:

'path is the path of axcel file
'sheet is the sheet ID of excel file
'A is the column name in excel file

set inputdata = DDT.ExcelDriver(path,sheet)
name = inputData.value("A")

In case if it does not exists then, it will trough an error/exception. You can handle it versy easily by using &"" with the string.

set inputdata = DDT.ExcelDriver(path,sheet)
name = inputData.value("A") &"" 'this will add nothing but will remove the exception you are getting.

How to use Regular Expression to test Email ID and other strings


A regular expression can be used as follow:

'Here x is the string that needed to be tested and y is the pattern against which x will be tested


Function RegExpression(x,y)
Dim reg
Set reg = New RegExp
reg.Pattern = y ' Regular Expression filter
reg.IgnoreCase = True 'ignoring the case sensitive
RegExpression = reg.Test(x) ' Test the captured value against the filter
End Function


The above function can be used in the main procedure as:

Sub RegularExpression
x = "0.10" ' Captured value
y = "0[.]10" ' Regular expression filter and expected value
z = RegExpression(x,y) ' Call the function
if z = -1 then
log.message "The value is valid"
else
log.error "The value is not valid"
end if
End Sub


'An email ID can be tested with Regular expression using the above function by passing the following patern:

'here y can change as per the characters allowed in the email ID

Sub RegularExpression
x = "abc27@yahoo.com" ' Captured value
y = "^[A-Z0-9]+@[A-Z0-9]+.[A-Z]{3}" ' Regular expression filter and expected value
z = RegExpression(x,y) ' Call the function
if z = -1 then
log.message "The value is valid"
else
log.error "The value is not valid"
end if
End Sub

How to get excel row count


Excel row count can be get by using the following function:

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