This is default featured slide 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Detailed Software Testing Tutorials and Interview Questions.

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Detailed Software Testing Tutorials and Interview Questions.3

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Detailed Software Testing Tutorials and Interview Questions.3

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Detailed Software Testing Tutorials and Interview Questions.3

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Detailed Software Testing Tutorials and Interview Questions.3

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Wednesday 19 October 2011

QTP Script: Write a value to the Registry


' =============================================================
' Sub :    RegistryWrite
' desc :   Writes a key value to the registry
' params : strRoot is the root key, i.e. "HKLM", "HKCU"
'          strPath is the path to create, i.e. 
'          "Software\Test\Automation"
'          strValue is the value to write in the key
' returns : void
' =============================================================
Function RegistryWrite(strRootstrPathstrValue)

' create the shell object
Set objShell = CreateObject("WScript.Shell")

' write the key
objShell.RegWrite strRoot & "\" & strPathstrValue"REG_SZ"

' destroy the object
Set objShell = Nothing

End Function 'RegistryWrite

QTP: Example of how to write text to a file


' =============================================================
' function: WriteLog
' desc :    Writes a message to a log file. File is created
'           inside a Log folder of the current directory
' params :  strCode is a code to prefix the message with
'           strMessage is the message to add to the file
' returns : void
' =============================================================
Function WriteLog(strCodestrMessage)

Dim objFS
Dim objFile
Dim objFolder
Dim strFileName

' create a file system object
Set objFS = CreateObject("Scripting.FileSystemObject")

' is there a log folder in the directory that we are currently working
If Not objFS.FolderExists(objFS.GetAbsolutePathName("."& "\log"Then

    ' if there is no log folder, create one
    Set objFolder = objFS.CreateFolder(objFS.GetAbsolutePathName("."& "\log"

End If ' folder exists

' set a name for the log file using year, month and day values
strFileName = objFS.GetAbsolutePathName("."& "\log\" & year(date& month(date& day(date& ".log"

' create the log file
Set objFile = objFS.OpenTextFile(strFileName8True)

' in case of any issues writing the file
On Error Resume Next

' write the log entry, include a carriage return
objFile.Write Date & ", " & Time & ", " & strCode & ", " & strMessage & vbcrlf

' disable the on error statement
On Error GoTo 0

' close the log file
objFile.Close

' destrory the object
Set objFS = Nothing

End Function ' WriteLog

QTP Script: Using Description Objects to interact with a web page


This example will illustrate how to use description objects to interact with a web page, www.QTPHelper.com to be more exact...

 

Note that for the Browser and Page I've used programmatic descriptions, but for the buttons, edits and check-boxes I've used Description Objects. Also take note of the regular expression in the Browser and Page description, just in case the titles change in the future.

 

You can add more properties to your description objects if you need to, i.e. if your web page has numerous objects of the same type with similar property values.

 

 

Dim objLogout
Dim objUser
Dim objPass
Dim objRemember

' create description objects for each item we are dealing with
Set objLogout   = Description.Create()
Set objUser     = Description.Create()
Set objPass     = Description.Create()
Set objRemember = Description.Create()

' define the properties of each item
objLogout("Name").Value   = "Logout"
objUser("Name").Value     = "username"
objPass("Name").Value     = "passwd"
objRemember("Name").Value = "remember"

' check that the user isn't already logged in
If Browser("Title:=QTP Helper.*").Page("Title:=QTP Helper.*").WebButton(objLogout).Exist(1Then

    ' click logout
    Browser("Title:=QTP Helper.*").Page("Title:=QTP Helper.*").WebButton(objLogout).Click 

End If

' set the user name
Browser("Title:=QTP Helper.*").Page("Title:=QTP Helper.*").WebEdit(objUser).Set "User"

' set the password
Browser("Title:=QTP Helper.*").Page("Title:=QTP Helper.*").WebEdit(objPass).Set "Password"

' tick the remember-me tickbox
Browser("Title:=QTP Helper.*").Page("Title:=QTP Helper.*").WebCheckBox(objRemember).Set "ON"

QTP Script: Using the replace method to find and replace text in a string


MsgBox ReplaceText("Automating with QTP is rubbish.""rubbish.""great!")

MsgBox ReplaceText("QTP is a great automation tool but I can't use it","but.*","!")

' =============================================================
' function: ReplaceText
' desc :    Uses a regular expression to replace text within a string
' params :  strString is the string to perform the replacement on
'           strPattern is the regular expression
'           strReplacement is the replacement string
' returns : The finished string
' =============================================================
Function ReplaceText(strStringstrPatternstrReplacement)

Dim objRegEx

' create the regular expression
Set objRegEx = New RegExp 

' set the pattern 
objRegEx.Pattern = strPattern

' ignore the casing
objRegEx.IgnoreCase = True

' make the replacement
ReplaceText = objRegEx.Replace(strStringstrReplacement

' destroy the object
Set objRegEx = Nothing

End Function ' ReplaceText 

QTP Script: Register a procedure with an object class


' add GetItemsCount as a method of the WebList class
RegisterUserFunc "WebList""GetItemsCount""GetItemsCountFunction"

' =============================================================
' function : GetItemsCountFunction
' desc : Returns the number of items from a weblist
' =============================================================
Function GetItemsCountFunction(objWebList)

If (objWebList = NothingThen
    GetItemsCount = 0
Else
    GetItemsCount = objWebList.GetROProperty("Items Count")
End If

End Function

QTP Script: Read a value from a key in the registry


' =============================================================
' function : RegistryRead
' desc     : Read a value from the registry
' params   : strRoot is the root key, i.e. "HKLM", "HKCU"
'            strPath is the path to read, i.e. 
'            "Software\Test\Automation"
' returns :  Value from the registry key
' =============================================================
Function RegistryRead(strRootstrPath)

' create the shell object
Set objShell = CreateObject("WScript.Shell")

' read the key
strValue = objShell.RegRead(strRoot & "\" & strPath)

' return the value
RegistryRead = strValue

' destroy the object
Set objShell = Nothing

End Function 'RegistryRead