Write a Script to open an excel workbook through VB Scripting

The below vb script will open an excel work book for which the path is mentioned.
set xl = CreateObject("Excel.Application")
xl.visible = true
set wb = xl.workbooks.open("C:\test.xls")
wb.close
xl.quit

Child Objects in QTP scripts with examples

Many at times in Scripting we come across scenarios where we need to count the objects in the application or even highlight a child object like highlighting a text box or check and uncheck check boxes using scripting in QTP.

Below are some of the examples where in you can do different actions. For our example we are using Flight Reservation application as reference.


'Script to count the number of child objects of flight reservation application
Set chd_obj = Dialog("text:=Login").ChildObjects
msgbox chd_obj.count

'Script to highlight all the child objects in Login dialog of flight reservation application
Set chd_obj = Dialog("text:=Login").ChildObjects
For i = 0 to chd_obj.count - 1
chd_obj(i).Highlight
Next

'Script to highlight all the child objects(buttons) in Login dialog of flight reservation application
Set button_obj = Description.Create
button_obj("nativeclass").Value = "Button"

Set chd_obj = Dialog("text:=Login").ChildObjects(button_obj)
For i = 0 to chd_obj.count - 1
chd_obj(i).Highlight
Next

'Script to check and uncheck all the check boxes in open order dialog box of flight reservation application
Set checkbox_obj = Description.Create
checkbox_obj("Class Name").value = "WinCheckBox"

Set chd_obj = Window("text:=Flight Reservation").Dialog("text:=Open Order").ChildObjects(checkbox_obj)
For i = 0 to chd_obj.Count - 1
chd_obj(i).Set "ON"
chd_obj(i).Set "OFF"
Next

Write a Script to delete all the spam mails in Gmail

We will write a script in QTP for deleting all the spam mails in Gmail Account. What we will do is first we will get the innertext of the Spam Link using GetROProperty so that if there are no spam mails then we will exit the test using ExitTest method or can use ExitAction method if we want to only exit that particular action.


If there are spam mails then we will count the unread spam mails and then display it to the user and then delete all the spam mails by checking all the check boxes and clicking the delete forever button. To check all the check boxed we will create a description object and add properties to it WebCheckBox Property. We will use the Childobjects method to get all the checkboxes and using the count method we will get the number of checkboxed. By using a For loop we will set the Checkbox to On so that all will be selected and then click the Delete Forever button to delete the mails.

'Script to Delete spam messages from Gmail
'First get the spam count. if there are no spam mails then exit test
'using GetROProperty to get the innertext of the Spam Link

spamct = browser("title:=Gmail.*").Page("title:=Gmail.*").link("innertext:=Spam.*").GetROProperty("innertext")

'Checking whether spam mails are there or not

If spamct = "Spam" Then
Print "No spam Mails"

'Since there are no spam mail you can exit Test or Exit Action. Based on users decision

ExitTest

'If spam mails are present then we will enter the else part

Else

'we will take the innertext of Spam link and remove all the unnecessary characters and print only the spam count.

spamcount = Replace(spamct, "spam", "") 'replacing the spam word with space
spamcount = Trim(spamcount)
spamcount = Replace(spamcount, "(","")
spamcount = Replace(spamcount, ")","")

'Printing the number of spam mails to the user

print "You have "&spamcount& " unread spam mails and all the available spam mails will be deleted forever"


'clicking on the spam link

Browser("title:=Gmail.*").Page("title:=Gmail.*").sync
Browser("title:=Gmail.*").Page("title:=Gmail.*").link("innertext:=Spam.*").Click

'Creating a description object for check boxes

Set abc_obj = Description.Create
abc_obj("micclass").Value = "WebCheckBox"

'Getting the childobjects i.e Checkboxes

Set browser_checkbox = Browser("title:=Gmail.*").Page("title:=Gmail.*").childobjects(abc_obj)

'using a for loop to set the check boxes to on position i.e selecting them

For i = 0 to browser_checkbox.Count - 1
browser_checkbox(i).Set "ON"
Next

'Clicking on the delete forever button to delete the spam mails

Browser("title:=Gmail.*").Page("title:=Gmail.*").webElement("innertext:=Delete forever","index:=*.*").Click

'using windows script host environment to make sure that the delete forever button is clicked

Set wsh = CreateObject("wscript.Shell")
wsh.sendkeys ("{Enter}")

End If

Description Object in QTP (Description. Create)

We will look at Description Objects (Description. Create in QTP) which is very important in Descriptive programming in QTP. In Descriptive programming we can create object with description being a single property or multiple properties. We actually put a collection of properties for an object. We can create and empty object and place all the required properties in it. This object can be used instead of the actual object.


We will see how to create a Description Object

Set Desc = Description.Create

We are actually using a variable called Desc and add properties to the variable using the Description.Create method. The variable Desc should follow a Set statement and the variable Des is an object reference.

Let us look at the Flight Reservation Application and create some Description objects for logging in to the Flight Reservation Application.


'script to login flight reservation using dynamic descriptive programming

Set dialog_login = Description.create
dialog_login("text").value ="Login"

Set edit_agentname = Description.Create
edit_agentname("attached text").value = "Agent Name:"

Set edit_password = Description.Create
edit_password("attached text").value = "Password:"

Set button_ok = Description.Create
button_ok("text").value ="OK"

Dialog(dialog_login).Activate
Dialog(dialog_login).WinEdit(edit_agentname).Set "Ravi"
Dialog(dialog_login).WinEdit(edit_password).Set "mercury"
Dialog(dialog_login).WinButton(button_ok).Click

In the above code we are actually using the properties and values and creating the objects and using them instead of the actual objects. This kind of method is very easy and maintainable because there is always a scope of adding new properties and values to the Description objects.

The other way to login in to the Flight Reservation Application is by simply putting in the properties and values in the script it self instead of creating description Objects. Instead of learning the objects into the object repository we use the associated properties and values of the object and directly pass them.

Look at the simple script using descriptive programming.

'Descriptive programming to login to the flight application.

Dialog("text:=Login").Activate
Dialog("text:=Login").WinEdit("attached text:=Agent Name:").Set "Ravi"
Dialog("text:=Login").WinEdit("attached text:=Password:").Set "mercury"
Dialog("text:=Login").WinButton("text:=OK").Click

Write a script to delete the mails in the Trash Folder in Gmail Account?

We can write a script to delete all the trash mails in the Gmail Account. First we need to Click the Trash link then click the all link in the trash mail page so that all the available mails in the table are checked and then we need to click the Delete Forever button. I have faced a problem when clicking on the All Link because when the All link is clicked it is actually selecting all the mails in the Inbox because the objects used like Select: All, None, Read, Unread, Starred, Unstarred are being used the same. So I faced a little problem but we can solve that by using object repository for that particular line of code.


If the below script is unable to run then use the other script given below and also learn the objects into the repository specially the Trash Link and the All Link in Gmail. Please look at the Object Repository Image below.

Please note that you need to be logged in to the Gmail Account to successfully run the below scripts.

First Method to delete the trash mails from Gmail Account:

Browser("title:=Gmail.*").Page("title:=Gmail.*").Link("text:=Trash", "class:=n.*").Click
Browser("title:=Gmail - Trash.*").Page("title:=Gmail - Trash.*").Sync
If Browser("title:=Gmail - Trash.*").Page("title:=Gmail - Trash.*").WebElement("innertext:=Empty Trash.*", "html id:=:.*").Exist(1) Then
Browser("title:=Gmail - Trash.*").Page("title:=Gmail - Trash.*").WebElement("innertext:=All", "Index:=*.*").Click
Wait 2
Browser("title:=Gmail - Trash.*").Page("title:=Gmail - Trash.*").WebElement("innertext:=Delete forever","index:=*.*").Click
Else
Print "You are not in proper page"
End If
Set wsh = CreateObject("WScript.Shell")
wsh.SendKeys ("{ENTER}")

If the above script does not work then you need to add objects to the repository and then use this script. We use Windows Script Host because we need to explicitly tell QTP to Click the Delete Forever Button which otherwise some times does not work so in order to resolve this issue we need to create a variable to interact with the Windows Script Host Library. We are also using the Reporter.ReportEvent method to return a pass or fail to the test result whether the messages are deleted or not.

Second Method to delete the trash mails from Gmail Account:

Browser("Gmail").Page("Gmail").Frame("frame").Link("Trash").Click
If Browser("Gmail").Page("Gmail").Frame("frame").Webtable("No conversations in the").Exist (1) Then
Reporter.ReportEvent micPass, "No messages", "There are no messages to be deleted"
Else
Browser("Gmail").Page("Gmail").Frame("frame").WebElement("All").Click
Browser("Gmail").Page("Gmail").Frame("frame").WebElement("Delete forever").Click
Set wsh = CreateObject("wscript.shell")
wsh.sendkeys ("{Enter}")
Reporter.ReportEvent micPass, "Deleted", "All the selected messages are deleted"
End If
The Object repository should learn these objects from the Application in order to run the above script.


Write a Script in QTP to count the no of buzz messages in a Gmail Account.

We will need to use the GetROProperty method to get the count of the buzz messages in the Gmail Account for a user and this number is dynamic so we use the GetRoProperty method and get the innertext value. We will use the GetROProperty (Get Run Time Object Property) to get the count of the Buzz Messages ex Buzz (25) and then replace Buzz, (,) with a null and then print only the number to the user.


'You need to be logged into the Gmail account
Browser("title:=Gmail.*").Page("title:=Gmail.*").Link("innertext:=Buzz.*","class:=n.*").Click
'To get the innertext of Buzz link
buzzcount = Browser("title:=Gmail.*").Page("title:=Gmail.*").Link("innertext:=Buzz.*","class:=n.*").GetROProperty("innertext")
If buzzcount = "Buzz" Then
Print "No Buzz Messages"
ExitAction
Else
buzzct = Replace(buzzcount, "Buzz","")
buzzct = Rtrim(ltrim(buzzct))
buzzct = Replace(buzzct, "(","")
buzzct = Replace(buzzct,")","")
Print "You have "&buzzct& " Buzz Messages."
End If

Write a Script in QTP to count the unread emails of a gmail account.

In order to count the unread emails of a gmail account you need to be logged into the Gmail account and then run this script which will count all the unread email and display the no of unread emails. We will use the GetROProperty (Get Run Time Object Property) to get the no of unread emails ex Inbox (25) and then replace Inbox, (, ) with a null and then print only the number.


The reason for us to use the GetROProperty on the Inbox Link is that the count of unread mails is dynamic and so we need to get the innertext and print the unread emails. So no matter you run any number of times you will get the exact number of unread emails in gmail account. If no unread emails are there then it will print a message saying " No Unread Emails" else it will print "You have xxx unread emails in your inbox"

'You need to be logged into the Gmail account
Browser("title:=Gmail.*").Page("title:=Gmail.*").Link("innertext:=Inbox.*").Click
Browser("title:=Gmail.*").Page("title:=Gmail.*").Sync

'To get the innertext of inbox link
inboxcount = Browser("title:=Gmail.*").Page("title:=Gmail.*").Link("innertext:=Inbox.*").getroproperty("innertext")
If inboxcount = "Inbox" Then
Print "No Unread Emails."
ExitAction
Else
emailcount = replace(inboxcount, "Inbox","") 'replaces inbox with space
emailcount = Rtrim(ltrim(emailcount)) 'removes all the spaces
emailcount = replace(emailcount, "(","")
emailcount = replace(emailcount, ")","")
Print "You have "&emailcount& " unread emails in your inbox."
End If

Write a Script to Login in to Gmail Account using QTP?

We will write a script to login to a Gmail Account. This QTP Script to log into Gmail Account is a Generic one. There is no need for us to have Object Repository since I am using Descriptive Programming. Where we use the properties and the values to identify the run time object in the Application under test.



We will write a descriptive program to login to Gmail Account. What I am doing is that each of the script contains a line of code to launch Gmail Application and then log into the application

To launch the Gmail application we need to use QTP reserved object like Systemutil or can interact with the Internet Explorer library and launch gmail. After launching the Gmail Application we have the script to login to the account by passing username and password. We will be printing the result at each step to see how the application is behaving

Systemutil.Run "http://www.gmail.com"

Or

'Launching Internet Explorer and Opening Gmail Application
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "http://www.gmail.com"

'We will use the sync method to wait till Gmail opens
Browser("Title:=Gmail.*").Page("title:=Gmail.*").Sync

'Script to set username and password to login
Browser("Title:=Gmail.*").Page("title:=Gmail.*").WebEdit("name:=Email").Set "xxxxxx"
Browser("Title:=Gmail.*").Page("title:=Gmail.*").WebEdit("name:=Passwd").Set "yyyyyy"
Browser("Title:=Gmail.*").Page("title:=Gmail.*").WebButton("name:=Sign in").click
Browser("Title:=Gmail.*").Page("title:=Gmail.*").Sync
If Browser("Title:=Gmail.*").Page("title:=Gmail.*").Link("innertext:=Sign out", "html id:=:.*").Exist(1) Then

'Print a Pass message for the user if logged in
Print "User " &Username& " is successfully Logged in"
Else If not Browser("Title:=Gmail.*").Page("title:=Gmail.*").Link("innertext:=Sign out", "html id:=:.*").Exist(1) Then

'Print a Fail message for the user if not able to log in and terminate the action
Print "User " &Username& " is not able to Login in"
Print "The Test has been terminated"
ExitAction
End If
End If

What is a Virtual Object and it helps in solving Object Recognition Problem in QTP ?

Virtual Objects comes into picture if QTP is unable to recognize an object and we often see errors as Object not found. This is because even thought you have recorded the actions during playback time QTP is unable to recognize an object and because of which the script fails.


Virtual Objects in QTP are created to resolve the object recognition problems in QTP. When an area of an application is not recognized by QTP we use the Virtual Object Wizard to map the area to a standard Class. These virtual objects are generally used to resolve the object recognition problems. All the Virtual Objects created are stored in the Virtual Object Manager. After we have learned an object as a Virtual Object we can record on that object successfully. You can create a Virtual Object by navigating to Tools, Virtual Objects, New Virtual Object.

Points to Note:

1. We cannot use object spy on a Virtual Object.
2. We can only record on Virtual Objects.
3. Scroll Bars and Labels cannot be treated as Virtual Objects.

In order to disable the Virtual Objects Navigate to Tools, Options, General, Check the check box for "Disable Recognition of Virtual Objects While Recording"

When Object recognition Problem occurs in QTP during playback on the Application Under test there are ways to handle this Object Recognition Problem. It depends solely on the user as to what they want to use.

The different ways to handle Object Recognition Problem are:

1. By Creating Virtual Objects.
2. Using Low Level Recording.
3. Using Analog Recording.

Creating a MS Word Document using QTP 9.2

Creating a MS Word Document in QTP is not difficult but is very easy and one needs to know how to use the Word Application in QTP.

It is very easy to create a MS Word Document using QTP. There is no need to invoke the MS Word application in QTP but we require to use the Word Library by using the CreateObject method. If you have word on Excel Application in QTP then it is more or less similar working with Word Library. I will show a sample script to create a word document and also add some text to the word Document.


Script to create a Word Document in QTP

Set wordapp = CreateObject("Word.Application")
wordapp.visible = True
wordapp.documents.Add
wordapp.Selection.TypeText "This is a sample document"
wordapp.ActiveDocument.SaveAs "E:\testdoc.doc"
wordapp.Quit
Set wordapp = Nothing

We are using Wordapp.Visible = true because we want the application to be visible for the user while the text is being written to the Word Document. TypeText property is used to write the text in the word document. In order to save the document we use ActiveDocument menthod because in case if there are other word document already opened then the active document will be saved.

/* Tynt Insight tracker ----------------------------------------------- */