Upload
karthikeya-sri
View
228
Download
0
Embed Size (px)
Citation preview
8/11/2019 Gmail Automation using qtp
1/8
QTPAutomating Gmail
Anshoo Arora March 18, 2010 AllQTP-UFT161 Comments
This article is a tutorial to show how parts of Gmail can be automated using QTP. Gmail is anextremely 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, automatingany 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:
FunctionGmailLogin(sUserName, sPassword)GmailLogin = False
WithBrowser("title:=Gmail.*").Page("micclass:=Page")'Check if the UserName field existsIf.WebEdit("html id:=Email").Exist(0) Then
.WebEdit("html id:=Email").SetsUserName 'Set UserName
.WebEdit("html id:=Passwd").SetSecure sPassword 'Set Password
.WebButton("name:=Sign in").Click 'Click Submit
.SyncEndIf
'Check for Link Inbox(xyz)If.Link("innertext:=Inbox.*").Exist(15) ThenGMailLogin = True
EndWithEndFunction
'Usage 1:MsgBox GmailLogin("yourUserName", "yourPassword")
'Usage 2:IfGmailLogin("yourUserName", "yourPassword") = TrueThen
'Continue with testElse
'Stop
EndIf
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:=SignOut").Click
http://relevantcodes.com/category/all/http://relevantcodes.com/category/all/http://relevantcodes.com/category/qtp/http://relevantcodes.com/category/qtp/http://relevantcodes.com/category/qtp/http://relevantcodes.com/automating-gmail-with-qtp/#commentshttp://relevantcodes.com/automating-gmail-with-qtp/#commentshttp://relevantcodes.com/automating-gmail-with-qtp/#commentshttp://relevantcodes.com/automating-gmail-with-qtp/#commentshttp://relevantcodes.com/category/qtp/http://relevantcodes.com/category/all/8/11/2019 Gmail Automation using qtp
2/8
'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 blocksor 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, ")", "") '3PrintiEmails
RegExp
'Updated 03/12/2012
SetoRegExp = NewRegExpoRegExp.Global = TrueoRegExp.Pattern = "\d+"SetoMatches = oRegExp.Execute(sLink) 'oMatches(0) = 3iEmails = oMatches(0)IfoMatches.Count = 2 TheniEmails = iEmails & ""& oMatches(1)PrintiEmails
Left, Right
8/11/2019 Gmail Automation using qtp
3/8
iPosition = InStr(1, sLink, "(")iEmails = Right(sLink, Len(sLink) - iPosition) '3iEmails = Left(iEmails, Len(iEmails) - 1)PrintiEmails
Get Total Emails CountUnlike 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 weare looking for:
Retrieve 1 4 of 4
'Updated 03/15/2012sText = 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) ' 4iEmails = Split(iEmails, " ")(1) '4PrintiEmails
RegExp
'Updated 03/12/2012SetoRegExp = NewRegExp
oRegExp.Global = TrueoRegExp.Pattern = "of .*"SetoMatches = oRegExp.Execute(sText)sMatch = oMatches(oMatches.Count - 1)
oRegExp.Pattern = "\d+"SetoMatches = oRegExp.Execute(sMatch)iEmails = oMatches(0)IfoMatches.Count = 2 TheniEmails = iEmails & ""& oMatches(1)
8/11/2019 Gmail Automation using qtp
4/8
PrintiEmails
Left, Right
iLoc_Of = InStr(1, sText, "of")iTotals = Right(sText, Len(sText) - iLoc_Of - 1)iTotals = Trim(iTotals)PrintiTotals
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/2012sText = 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) '0PrintiSpace
RegExp
'Updated 03/12/2012SetoRegExp = NewRegExp
oRegExp.Pattern = "\d+ MB"SetoMatches = oRegExp.Execute(sText)iSpace = oMatches(0)PrintiSpace
Left, Right
iLoc_MB = InStr(1, sText, "MB")
8/11/2019 Gmail Automation using qtp
5/8
sText = Trim(Left(sText, iLoc_MB - 1)) 'You are currently using xxiSpace = Right(sText, Len(sText) - InStrRev(sText, " ")) '0PrintiSpace
Gmail Auto-Generated Response Message
If you would like to check the existence of any of the messages as shown below, accessing the
classof the Element will suffice to retrieve the entire text and verify it against our expectedresult.
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
DimoDesc, colObject
SetoDesc = Description.CreateoDesc("micclass").Value = "WebElement"oDesc("class").Value = "vh"
SetcolObject =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
GetRowWithCellTextmethod of the WebTable. However, chances are that the custom functionwill not prove to be quite as fast. A custom function has been created to find the row containing
the specified text:
Custom Function
8/11/2019 Gmail Automation using qtp
6/8
FunctionFindMailRow(sText)DimoTable, iRows, ix
FindMailRow = -1
WithBrowser("title:=Gmail.*").Page("micclass:=Page").WebTable("class:=F
cf zt") iRows = .GetROProperty("rows")
Forix = 1 toiRowssMailText = .GetCellData(ix, 3) & .GetCellData(ix, 5) &
.GetCellData(ix, 7)IfInStr(1, LCase(Replace(sMailText, vbLf, " ")), LCase(sText))
ThenFindMailRow = ixExitFunction
EndIfNext
EndWithEndFunction
'Usage:MsgBox FindMailRow("Text")
The outcome will be the same when using the GetRowWithCellTextmethod, but with a smallerperformance footprint.
GetRowWithCellText
MsgBox Browser("title:=Gmail.*").Page("micclass:=Page").WebTable("class:=F cfzt")_
.GetRowWithCellText("Redefining")
The custom function took 2.45 secondsto 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 beread. 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:
8/11/2019 Gmail Automation using qtp
7/8
'Updated 03/12/2012'I have used 'Access Gmail' but you can use any (unique) text on that rowiRow = Browser("title:=Gmail.*").Page("micclass:=Page").WebTable("class:=F cfzt")_
.GetRowWithCellText("Access Gmail")
WithBrowser("title:=Gmail.*").Page("micclass:=Page").WebTable("class:=F cfzt")Setting.WebPackage("ReplayType") = 2
.ChildItem(iRow, 5, "WebElement", 0).ClickSetting.WebPackage("ReplayType") = 1
EndWith
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") = 2Browser("title:=Gmail.*").WebElement("innertext:=COMPOSE","index:=1").ClickSetting.WebPackage("ReplayType") = 1
Recipient, Subject, Body
WithBrowser("title:=Gmail.*").WebEdit("html id:=:1b6").Set"[email protected]"'Recipient.WebEdit("html id:=:1c9").Set"Subject"'Subject.WebElement("html id:=:1cl", "index:=1").Object.innerText = "Message
Body"'BodyEndWith
Click Send
Setting.WebPackage("ReplayType") = 2Browser("title:=Gmail.*").WebElement("html id:=:1d3", "index:=1").Click
Setting.WebPackage("ReplayType") = 1
8/11/2019 Gmail Automation using qtp
8/8
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 letme know.