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.


2 comments:

cloud said...

How to Delete Gmail Account

Jack Leach said...

Hey Mike, really great thoughts on this topic, I recently blogged about this as well "how to delete all emails in gmail"

Post a Comment

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