Você está na página 1de 8

QTP Automating Gmail

Anshoo Arora March 18, 2010 All QTP-UFT 161 Comments


This article is a tutorial to show how parts of Gmail can be automated using QTP. Gmail is an
extremely dynamic UI and always quite challenging to automate successfully. Its dynamic
behavior also makes it an excellent candidate to practice QTP with and sharpen your skills. I will
try to show a few techniques that can be helpful in automating Gmail, and through it, automating
any dynamic application that you encounter.
Gmail Login
Im sure everyone who has worked with Gmail would have already created this part of the script
and chances are that it would have been created as a function. The same has been done here with
the help of conditional statements:
Function GmailLogin(sUserName, sPassword)
GmailLogin = False

With Browser("title:=Gmail.*").Page("micclass:=Page")
'Check if the UserName field exists
If .WebEdit("html id:=Email").Exist(0) Then
.WebEdit("html id:=Email").Set sUserName 'Set UserName
.WebEdit("html id:=Passwd").SetSecure sPassword 'Set Password
.WebButton("name:=Sign in").Click 'Click Submit
.Sync
End If

'Check for Link Inbox(xyz)
If .Link("innertext:=Inbox.*").Exist(15) Then GMailLogin = True
End With
End Function

'Usage 1:
MsgBox GmailLogin("yourUserName", "yourPassword")

'Usage 2:
If GmailLogin("yourUserName", "yourPassword") = True Then
'Continue with test
Else
'Stop
End If
Sign Out of Gmail
No tricks here. A simple inline DP or OR statement would accomplish Signing out of Gmail:
Browser("title:=Gmail.*").Page("micclass:=Page").Link("innertext:=Sign
Out").Click

'Update March 15, 2011: The Gmail interface has changed slightly.
'If the above does not work, please try this:
Browser("title:=Gmail.*").Page("micclass:=Page").Link("innertext:=Sign Out",
"class:=gbml1").Click
Get New Email Count
Each automation developer prefers a different approach of retrieving desired values from blocks
or text. For each task which requires a value to be retrieved, 3 techniques are demonstrated for
the retrieval of expected values: Split, RegExp and Left/Right. But first, we must retrieve the
entire string containing the number of Unread emails:

Retrieve Inbox (3)
sLink =
Browser("title:=Gmail.*").Page("title:=Gmail.*").Link("innertext:=Inbox.*")_
.GetROProperty("innertext")
Once the string containing the number of unread e-mails is retrieved, one of the following
approaches can be used to produce the same result:
Split
iEmails = Split(sLink, " ")(1) '(3)
iEmails = Replace(iEmails, "(", "") '3)
iEmails = Replace(iEmails, ")", "") '3
Print iEmails
RegExp
'Updated 03/12/2012
Set oRegExp = New RegExp
oRegExp.Global = True
oRegExp.Pattern = "\d+"
Set oMatches = oRegExp.Execute(sLink) 'oMatches(0) = 3
iEmails = oMatches(0)
If oMatches.Count = 2 Then iEmails = iEmails & "" & oMatches(1)
Print iEmails
Left, Right
iPosition = InStr(1, sLink, "(")
iEmails = Right(sLink, Len(sLink) - iPosition) '3
iEmails = Left(iEmails, Len(iEmails) - 1)
Print iEmails
Get Total Emails Count
Unlike the scenario above, which retrieved the number of unread e-mails in the AUT, this
section shows how the total number of e-mails can be retrieved. Just like the previous scenario, a
simple inline DP statement can be used to retrieve the number string that contains the value we
are looking for:

Retrieve 1 4 of 4
'Updated 03/15/2012
sText = Browser("title:=Gmail.*").Page("title:=Gmail.*")_
.WebElement("innertext:=\d+\d+ of \d+.*",
"index:=0").GetROProperty("innertext")

'Alternate:
sText = Browser("title:=Gmail.*").Page("title:=Gmail.*")_
.WebElement("class:=J-J5-Ji amH J-JN-I").GetROProperty("innertext")
After retrieving the entire string, one of the following approaches can be used to produce the
result:
Split
iEmails = Split(sText, "of")(1) ' 4
iEmails = Split(iEmails, " ")(1) '4
Print iEmails
RegExp
'Updated 03/12/2012
Set oRegExp = New RegExp
oRegExp.Global = True
oRegExp.Pattern = "of .*"
Set oMatches = oRegExp.Execute(sText)
sMatch = oMatches(oMatches.Count - 1)

oRegExp.Pattern = "\d+"
Set oMatches = oRegExp.Execute(sMatch)
iEmails = oMatches(0)
If oMatches.Count = 2 Then iEmails = iEmails & "" & oMatches(1)
Print iEmails
Left, Right
iLoc_Of = InStr(1, sText, "of")
iTotals = Right(sText, Len(sText) - iLoc_Of - 1)
iTotals = Trim(iTotals)
Print iTotals
Space Used by Emails
One important, and a little complex (in comparison to the above scenarios) one: retrieving the
space used by Emails. The process will remain the same, but notice the usage of the wild card
character for the WebElement:

Retrieve You are currently using 0 MB (0%) of your 7430 MB.
'Updated 03/15/2012
sText = Browser("title:=Gmail.*").Page("title:=Gmail.*")_
.WebElement("innertext:=.*Using.*of your.*", "index:=0")_
.GetROProperty("innertext")
The above inline DP statement will retrieve and store the entire string with sText. Once
executed, one of the following methods can be used to retrieve the result:
Split
iSpace = Trim(Split(sText, "Using")(1))
iSpace = Split(iSpace, " ")(0) '0
Print iSpace
RegExp
'Updated 03/12/2012
Set oRegExp = New RegExp
oRegExp.Pattern = "\d+ MB"
Set oMatches = oRegExp.Execute(sText)
iSpace = oMatches(0)
Print iSpace
Left, Right
iLoc_MB = InStr(1, sText, "MB")
sText = Trim(Left(sText, iLoc_MB - 1)) 'You are currently using xx
iSpace = Right(sText, Len(sText) - InStrRev(sText, " ")) '0
Print iSpace
Gmail Auto-Generated Response Message
If you would like to check the existence of any of the messages as shown below, accessing the
class of the Element will suffice to retrieve the entire text and verify it against our expected
result.




Out of the numerous ways these messages could be checked, I am going to show 2 possible uses
with a description object and an inline DP statement.
Inline DP
Browser("title:=Gmail.*").Page("micclass:=Page")_
.WebElement("class:=vh", "html tag:=TD",
"index:=0").GetROProperty("innertext")
Description Object + ChildObjects
Dim oDesc, colObject

Set oDesc = Description.Create
oDesc("micclass").Value = "WebElement"
oDesc("class").Value = "vh"

Set colObject =
Browser("title:=Gmail.*").Page("micclass:=Page").ChildObjects(oDesc)

MsgBox colObject(0).GetROProperty("innertext")
Finding Row with Containing Text
To find an e-mail row using text, we can either create a custom function or use the
GetRowWithCellText method of the WebTable. However, chances are that the custom function
will not prove to be quite as fast. A custom function has been created to find the row containing
the specified text:
Custom Function
Function FindMailRow(sText)
Dim oTable, iRows, ix

FindMailRow = -1

With Browser("title:=Gmail.*").Page("micclass:=Page").WebTable("class:=F
cf zt")
iRows = .GetROProperty("rows")

For ix = 1 to iRows
sMailText = .GetCellData(ix, 3) & .GetCellData(ix, 5) &
.GetCellData(ix, 7)
If InStr(1, LCase(Replace(sMailText, vbLf, " ")), LCase(sText))
Then
FindMailRow = ix
Exit Function
End If
Next
End With
End Function

'Usage:
MsgBox FindMailRow("Text")
The outcome will be the same when using the GetRowWithCellText method, but with a smaller
performance footprint.
GetRowWithCellText
MsgBox Browser("title:=Gmail.*").Page("micclass:=Page").WebTable("class:=F cf
zt")_
.GetRowWithCellText("Redefining")
The custom function took 2.45 seconds to find the row whereas GetRowWithCellText took
only 1.22 seconds.
Reading Emails
The above method can be coupled with a click event to find and open the e-mail that is to be
read. A simple inline DP statement can be used to click the target row and open the e-mail. In
this example, were going to click on the 2nd Email:

'Updated 03/12/2012
'I have used 'Access Gmail' but you can use any (unique) text on that row
iRow = Browser("title:=Gmail.*").Page("micclass:=Page").WebTable("class:=F cf
zt")_
.GetRowWithCellText("Access Gmail")

With Browser("title:=Gmail.*").Page("micclass:=Page").WebTable("class:=F cf
zt")
Setting.WebPackage("ReplayType") = 2
.ChildItem(iRow, 5, "WebElement", 0).Click
Setting.WebPackage("ReplayType") = 1
End With
When the row is clicked, the Email is opened for reading:

Compose
This can be tricky. Breaking down each component below:
Clicking Compose
Setting.WebPackage("ReplayType") = 2
Browser("title:=Gmail.*").WebElement("innertext:=COMPOSE",
"index:=1").Click
Setting.WebPackage("ReplayType") = 1
Recipient, Subject, Body
With Browser("title:=Gmail.*")
.WebEdit("html id:=:1b6").Set "test@test.com" 'Recipient
.WebEdit("html id:=:1c9").Set "Subject" 'Subject
.WebElement("html id:=:1cl", "index:=1").Object.innerText = "Message
Body" 'Body
End With
Click Send
Setting.WebPackage("ReplayType") = 2
Browser("title:=Gmail.*").WebElement("html id:=:1d3", "index:=1").Click
Setting.WebPackage("ReplayType") = 1
I hope this article provides more in-depth knowledge of testing complex Web applications. I
hope to find more such applications in the future to share with everyone. If you feel there is a
scenario that this article lacks, or will become more useful with the addition of one, please do let
me know.

Você também pode gostar