Automation Beyond

Testing and Test Automation for Software Quality Assurance. Above Requirements. Beyond Expectations. Methodologies and concepts. Framework Design. Programming and Scripting. Created by Albert Gareev.

Service Functions – String (QTP, VBScript)

Posted by Albert Gareev on 2009/10/01

In the code examples I present I often refer to routine functions.

In my framework I have specialized libraries to call from.  In my blog I have been providing them along with the posts but now I see that better to keep all in one place and “call” with the link.

Matching string with a pattern (Regular Expression, RegEx)


Public Function Regex_Test(ByVal strSrc, ByVal strRegEx)
 Dim objRegEx
 Dim boolRC, intRC

 Set objRegEx = New RegExp 
 objRegEx.Pattern = strRegEx

 On Error Resume Next
 boolRC = objRegEx.Test(strSrc)
 intRC = Err.Number
 On Error GoTo 0
 If intRC <> 0 Then boolRC = FALSE

 Set objRegEx = Nothing
 Regex_Test = boolRC
End Function

Replace pattern(s) in a string


Public Function ReplaceEx(ByVal sSource, ByVal sPattern, ByVal sDest, ByVal boolMatchCase)
   Dim objRegExp

 Set objRegExp = new RegExp
 objRegExp.Global = TRUE
 objRegExp.IgnoreCase = Not boolMatchCase
 objRegExp.Pattern = sPattern

 ReplaceEx = objRegExp.Replace(sSource, sDest)

 Set objRegExp = Nothing

End Function

Number conversion and initialization with a default value


Public Function IntVal(ByVal sVal)
   If isNumeric(sVal) Then
    IntVal = CLng(sVal)
 Else
  IntVal = 0
   End If
End Function
Public Function InitLong(ByVal sActualValue, ByVal DefaultValue)
 DefaultValue = IntVal(DefaultValue)
  
 If sActualValue = "" Then
  InitLong = DefaultValue
 Else
    If isNumeric(sActualValue) Then
     InitLong = CLng(sActualValue)
  Else
   InitLong = DefaultValue
    End If
 End If
End Function

Initialize string with a default value


Public Function InitValue(ByVal sActualValue, ByVal DefaultValue)
 If sActualValue = "" Then
  InitValue = DefaultValue
 Else
  InitValue = sActualValue
 End If
End Function

Initialize boolean with a default value


Public Function InitBool(ByVal sActualValue, ByVal boolDefaultValue)
  
   sActualValue = UCase(sActualValue)

   Select Case boolDefaultValue
   Case TRUE
   'By default set to TRUE
   'Set to FALSE if explicitely defined
     If (sActualValue = "FALSE") OR (sActualValue = "NO") Then
      InitBool = FALSE
     Else
      InitBool = TRUE
     End If
   Case FALSE
   'By default set to FALSE
   'Set to TRUE if explicitely defined
     If (sActualValue = "TRUE") OR (sActualValue = "YES") Then
      InitBool = TRUE
     Else
      InitBool = FALSE
     End If
   End Select

End Function

12 Responses to “Service Functions – String (QTP, VBScript)”

  1. [...] Service Functions – String (QTP, VBScript) [...]

  2. [...] ReplaceEx function source code: Service Functions – String (QTP, VBScript) [...]

  3. [...] Service Functions – String (QTP, VBScript) [...]

  4. [...] Service Functions – String (QTP, VBScript) [...]

  5. [...] Service Functions – String (QTP, VBScript) [...]

  6. [...] Service Functions – String (QTP, VBScript) [...]

  7. [...] Service Functions – String (QTP, VBScript) [...]

  8. [...] Service Functions – String (QTP, VBScript) [...]

  9. [...] Service Functions – String (QTP, VBScript) [...]

  10. [...] Used resources: Service Functions – String (QTP, VBScript) [...]

  11. [...] Used resources: Service Functions – String (QTP, VBScript) [...]

  12. [...] Used resources: Service Functions – String (QTP, VBScript) [...]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>