QTP Reusable Functions Codes 4


QTP Reusable Function Codes :


Automation testing using QTP has always been talked upon the most in the testing industry. There are numerous features available within this very toolset, but most of us might not be aware of this very feature which has been discussed in this very articles on automation testing using QTP which is functions created in Vbscript , and is very useful in code customization to meet our automation testing requirements , as it reduces the script maintenance effort.

1. Can I change the Active Screen which is shown on every new test?

You can change the Active Screen to any valid HTML page. The page can be located either locally or on the network.

For example, if you want your entire organization to view the same Active Screen when opening QuickTest Professional, open the NewTest.inf file located in QuickTest Professional\dat\SnapShots folder and add the following line:

FileName1= "any full path to an HTML file"

For example:
FileName1=\mercury\public\MainPage.html

2. How do I create an action template?

If you want to include one or more statements in every new action in your test, you can create an action template. For example, if you always enter your name as the author of an action, you can add this comment line to your action template. An action template applies only to actions created on your computer.

To create an action template, create a text file containing the comments, function calls, and other statements that you want to include in your action template. The text file must be in the structure and format used in the Expert View.

Save the text file as ActionTemplate.mst in your QuickTest Professional\dat folder. All new actions you create contain the script lines from the action template.

3. How can I configure the report to show only errors by default?

You can configure the report to show only errors by default by adding the following section to the QTReport.ini file in your QuickTest Professional\bin folder.

[FilterDialog]

ReportAppDefaultFilter=1 # for error only

ReportAppDefaultFilter=3 # shows all messages (default)

4. How do I use environment variables?

QuickTest supports using environment variables. Environment variables can be either system variables or user-defined variables.

You can define the user-defined variables in an external file QuickTest reads when it is launched.

5. I have a Microsoft Access database that contains data I would like to use in my test. How do I do this?

The Expert View enables you to access databases using ADO and ODBC. Below is a sample test that searches for books written by an author in the "Authors" table of the database.

Dim MyDB
Dim MyEng
Set MyEng = CreateObject("DAO.DBEngine.35")
Dim Td
Dim rs
' Specify the database to use.
Set MyDB = MyEng.OpenDatabase("BIBLIO.MDB")
' Read and use the name of the first 10 authors.
Set Td = MyDB.TableDefs("Authors")
Set rs = Td.OpenRecordset
rs.MoveFirst
For i = 1 To 10
   Browser("Book Club").Page("Search Books").WebEdit("Author Name").Set rs("Author")
   Browser("Book Club").Page("Search Books").WebButton("Search").Click
Next

6. How do I add a manual wait step to my test?

A manual wait (think time) can be added to a QuickTest Professional test using the following command:

Call Wait()

7. How do I make the test prompt the user for input while it is running?

The VBScript InputBox function enables you to display a dialog box that prompts the user for input and then continues running the test. You can use the value that was entered by the user later in the test. For more information on the InputBox function, refer to the VBScript Reference.

The following example shows the InputBox function used to prompt the user for a password.

Browser("Mercury Tours").Page("Mercury Tours").WebEdit("username").Set "administrator"

Passwd = InputBox ("Enter password", "User Input")

Browser("Mercury Tours").Page("Mercury Tours").WebEdit("password").Set Passwd

8. How can I configure the report to show only errors by default?

You can configure the report to show only errors by default by adding the following section to the QTReport.ini file in your QuickTest Professional\bin folder.

[FilterDialog]

ReportAppDefaultFilter=1 # for error only

ReportAppDefaultFilter=3 # shows all messages (default)

9. How can I remove test result files from old tests?

You can use the Test Results Deletion Tool to view a list of all the test results in a specific location in your file system or in a Quality Center project. You can then delete any test results that you no longer require.

The Test Results Deletion Tool enables you to sort the test results by name, date, size, and so forth, so that you can easily identify the results you want to delete.

You can find this utility in Start > Programs > QuickTest Professional > Tools > Test Results Deletion Tool.

10. How can I launch a new browser from a test?

A new browser window (and any other application) can be launched from within a test by adding the following step to your test:

SystemUtil.Run "iexplore.exe", "http://www.testingfaqs.co.in"

11. How can I record on nonstandard menus?

You can modify how QuickTest behaves when it records menus.

The options that control this behavior are located in the Advanced Windows Applications Options dialog box. (Tools > Options > Windows Applications > Advanced).

12. How can I terminate an application that is not responding?

You can terminate any standard application while running a test in QuickTest by adding one of the following steps to the test:

SystemUtil.CloseProcessByName "app.exe"
SystemUtil.CloseProcessByWndTitle "Some Title"

13. Can I copy and paste to and from the Clipboard during a test run?

You can use the Clipboard object to copy, cut, and paste text during a QuickTest test run.

The object has the same methods as the Clipboard object available in Visual Basic:

Clear
GetData
GetFormat
GetText
SetData
SetText

Below is an example of Clipboard object usage:

Set MyClipboard = CreateObject("Mercury.Clipboard")
MyClipboard.Clear
MyClipboard.SetText "TEST"
MsgBox MyClipboard.GetText

14. How do I close QuickTest after "n" test runs when running from Quality Center?

When running multiple QuickTest tests from Quality Center, you can specify that you want to close QuickTest after a specified number of tests are executed.

To do so, add the following lines to the end of the mic.ini file, located in your QuickTest Professional\bin folder:
[RemoteAgent]
CloseToolAfterRuns= (number)

15. Adding Defects to Quality Center

Connects to Quality Center (TestDirector) from a QuickTest test and adds a bug to the database.

Dim TDConnection
Set TDConnection = CreateObject("TDApiOle.TDConnection")
TDConnection.InitConnection "http://yovav/tdbin" ' URL for the DB
TDConnection.ConnectProject "TD76","bella","pino" ' Valid login information
If TDConnection.Connected Then
MsgBox("Connected to " + chr (13) + "Server " + TDConnection.ServerName _
+ chr (13) +"Project " + TDConnection.ProjectName )
Else
MsgBox("Not Connected")
End If
'Get the IBugFactory
Set BugFactory = TDConnection.BugFactory
'Add a new empty bug
Set Bug = BugFactory.AddItem (Nothing)
'Fill the bug with relevant parameters
Bug.Status = "New"
Bug.Summary = "Connecting to TD"
Bug.Priority = "4-Very High" ' depends on the DB
Bug.AssignedTo = "admin" ' user that must exist in the DB's users list
Bug.DetectedBy = "admin" ' user that must exist in the DB's users list
'Post the bug to database (commit)
Bug.Post

16. Using Message Boxes That Close Automatically

The function below shows a message box that disappears after the specified timeout (in seconds). The script execution then continues.

Public Sub MsgBoxTimeout (Text, Title, TimeOut)
   Set WshShell = CreateObject("WScript.Shell")
   WshShell.Popup Text, TimeOut, Title
End Sub

If TimeOut is 0, it behaves just like a normal message box. If TimeOut is greater than 0, the dialog box disappears after the specified number of seconds.

17. Using Microsoft Word Spell Check

The following code shows a function for checking the number of spelling and grammar errors in a string. This function is also used to check the accuracy of a specific property in all the objects of a given application.

Function NumberOfSpellErrors(strText)
Dim objMsWord
Set objMsWord = CreateObject("Word.Application")
objMsWord.WordBasic.FileNew
objMsWord.WordBasic.Insert strText
NumberOfSpellErrors = objMsWord.ActiveDocument.SpellingErrors.Count
objMsWord.Documents.Close (False)
objMsWord.Quit ' close the application
Set objMsWord = Nothing' Clear object memory
End Function

' The following function uses the Spell errors function to check a specific property
' of all the objects with a given description which are under a given parent

Sub CheckAllObjects(ParentObj, ObjDesc, PropName)
Dim ObjCol, idx, PropValue, OldReportMode
OldReportMode = Reporter.Filter
Reporter.Filter = 2 ' Report only errors
If (IsNull(ParentObj)) Then
Set ObjCol = Desktop.ChildObjects(ObjDesc)
Else
Set ObjCol = ParentObj.ChildObjects(ObjDesc)
End If
For idx=0 to ObjCol.count-1
PropValue = ObjCol.Item(idx).GetROProperty(PropName)
RetVal = NumberOfSpellErrors(PropValue) ' The actual spell check result
If (RetVal > 0) Then
ReportText = "Object #" & idx+1 & ": The '" & PropName & "' Property has " & RetVal & " spell errors (" & PropValue & ")"
Reporter.ReportEvent 1, "Spell Check", ReportText
End If
Next
Reporter.Filter = OldReportMode
End Sub

'''''''''''''''''''''''''''''''''''''
' An example of usage:
' Go over all the static objects in the Login window of the Flight Application
' and for each object check the text for spelling and grammatical errors
'''''''''''''''''''''''''''''''''''''

' Go over all the links in the page and report all the ones that fail the spellcheck
Set Desc = Description.Create()
Desc("nativeclass").Value = "Static"
Set Obj = Dialog("nativeclass:=#32770", "text:=Login")
' Invoke the Flight Application before calling the function
CheckAllObjects Obj, Desc, "text"


18. Using Microsoft Outlook to Send Email

The code below illustrates two methods for sending email using the VBScript code in QuickTest Professional, and two different COM objects.

' Example 1

Function SendMail(SendTo, Subject, Body, Attachment)
Set ol=CreateObject("Outlook.Application")
Set Mail=ol.CreateItem(0)
Mail.to=SendTo
Mail.Subject=Subject
Mail.Body=Body
If (Attachment <> "") Then
Mail.Attachments.Add(Attachment)
End If
Mail.Send
ol.Quit
Set Mail = Nothing
Set ol = Nothing
End Function

' Example 2

Function SendMail(SendFrom, SendTo, Subject, Body)
Set objMail=CreateObject("CDONTS.Newmail")
ObjMail.From = SendFrom
ObjMail.To = SendTo
ObjMail.Subject = Subject
ObjMail.Body = Body
ObjMail.Send
Set objMail = Nothing
End Function

19. DP Example

SetEditBox "userID", 0, "Sairam"

Sub SetEditBox(ObjName, index, TestData)
strHandle = Window("RegExpWndClass:=IEFrame","index:=0" ).GetROProperty("hWnd")
Set gobjBrowser = Browser("hWnd:=" & strHandle)
Set gobjPage = gobjBrowser.Page("index:=0")
Set objDescription = Description.Create()
objDescription("Class Name").Value = "WebEdit"
objDescription("html tag").Value = "INPUT"
objDescription("name").Value =ObjName
objDescription("index").Value = index
gobjPage.WebEdit(objDescription).Set TestData
set gobjBrowser = nothing
Set gobjPage = nothing
End Sub

20. SaveAs Excel file with QTP

Set xl = CreateObject("Excel.Application")
xl.visible = True
xl.Application.Workbooks.open "C:\sai.xls"
xl.Worksheets(1).Activate
xl.Application.Visible = True
xl.Cells(1,1).Value = "mum"
xl.Cells(1,2).Value = "dad"
xl.activeworkbook.SaveAs("C:\sai1.xls")
xl.Application.Quit


Learn More On QTP Reusable Function creation codes :

QTP-reusable-functions-codes-5
QTP-reusable-functions-codes-4
QTP-reusable-functions-codes-3
QTP-reusable-functions-codes-2
QTP-reusable-functions-codes-1


For gaining more insights in the automation using QTP log on to below url :
Automation Testing Using QTP



Post a Comment

Previous Post Next Post