Wednesday, May 8, 2013

Read first few UN-Read mails from GMail using Selenium IDE

        To make yourself familiar with reading mails from GMail please read my previous post "http://www.seleniumhq.in/2013/05/read-mail-from-gmail-with-selenium-ide.html"

To know reading first few mail with help of control structures (flow control using while loop, Goto etc), please read the Previous post "http://www.seleniumhq.in/2013/05/read-first-n-mails-form-gmail-with.html".

This post explain how to read top/recent n UN-read mails from Gmail INBOX. Selenium IDE logs in to your Gmail on your behalf and give continuous n alerts with the contents of the UN-Read mails.


Below code picks up each mail by its FROM field and verifies it it is already read. If it is an Un-read mail then its contents are alerted to user by JavaScript alert, if not the next mail is considered for checking. Remember that the test case execution is paused until you click on OK of the alert. This code is very dynamic that no matter where the Un-Read mails present in the page, that gets picked up and alerted. 


Here is the Test Case:

store | //table/tbody/tr[ | target1
store | ]/td[5]/div[@class='yW'] | target2
store | //table/tbody/tr[ | targetBold1
store | ]/td[5]/div[@class='yW']//span | targetBold2
store | 1 | i
store | 1 | boldCount
click | link=Inbox * | 
while | ${boldCount}<=3 | 
storeEval | storedVars['targetBold1']+storedVars['i']+storedVars['targetBold2']+'@class' | targetBold
storeAttribute | javascript{storedVars['targetBold']} | targetBoldClass
echo | ${targetBoldClass} | 
gotoIf | storedVars['targetBoldClass']!='zF' | jump
storeEval | storedVars['target1']+storedVars['i']+storedVars['target2'] | target
clickAt | javascript{storedVars['target']} | 
storeText | class=hP | subject
storeText | class=iw | from
storeText | //div[2]/div/div/div/div/div/div/div[2]/div[6]/div | content
storeEval | javascript{alert(storedVars['from']+'  '+<br/>+storedVars['subject']+'  '+<br/>+storedVars['content'])} | 
storeEval | parseInt(storedVars['boldCount'])+1 | boldCount
echo | ${boldCount} | 
goBack |  | 
label | jump | 
storeEval | parseInt(storedVars['i'])+1 | i
endWhile |  | 



Commands explained

store | //table/tbody/tr[ | target1
store | ]/td[5]/div[@class='yW'] | target2
store | //table/tbody/tr[ | targetBold1
store | ]/td[5]/div[@class='yW']//span | targetBold2
store | 1 | i
store | 1 | boldCount


Store the actual target of FROM field "//table/tbody/tr[ i ]/td[5]/div[@class='yW']" in 3 parts so that the value of 'i' can be incremented later in a loop. And also Store the actual target of FROM field that in in BOLD text "//table/tbody/tr[ i ]/td[5]/div[@class='yW']//span" in 3 parts

click | link=Inbox * |
Click on the INBOX link

while | ${boldCount}<=3 |
Loop thru first 3 UN-Read mails

storeEval | storedVars['targetBold1']+storedVars['i']+storedVars['targetBold2']+'@class' | targetBold
For every iteration we are storing the bolded FROM field's Xpath and its attribute "Class" in a variable named "targetBold"

storeAttribute | javascript{storedVars['targetBold']} | targetBoldClass
Storing the attribut (class) value (i.e: Class='zF') in a variable "targetBoldClass"

gotoIf | storedVars['targetBoldClass']!='zF' | jump
If the chosen mail is already Read (not bold) then jump to label "jump"

storeEval | storedVars['target1']+storedVars['i']+storedVars['target2'] | target
For every iteration we are storing the target (Xpath) in a variable named "target" to click on it.

clickAt | //table/tbody/tr/td[5]/div[@class='yW'] |
Click at the FROM field of first/recent/top most mail to go to mail detail page. //tr for first mail, tr[2] for second and so on.

storeText | class=hP | subject
Store SUBJECT in a variable "subject"

storeText | class=iw | from
Store FROM in a variable "from"

storeText | //div[2]/div/div/div/div/div/div/div[2]/div[6]/div | content
Store mail content in a variable "content"

storeEval | javascript{alert(storedVars['from']+'   '+<br/>+storedVars['subject']+'   '+<br/>+storedVars['content'])} |
Show all the above fields in an alert

storeEval | parseInt(storedVars['boldCount'])+1 | boldCount
Increment the "boldCount" such that it stops reading mails at max number defined in while loop.

goBack |  |
Go back to Inbox

label | jump | 
Lavel where "gotoIf" command send the control to incase the chosen mail is already read.

storeEval | parseInt(storedVars['i'])+1 | i
Increment "i'"

endWhile |  |
Stop and  restart While


Source:
-------------------------
<tr>
    <td>store</td>
    <td>//table/tbody/tr[</td>
    <td>target1</td>
</tr>
<tr>
    <td>store</td>
    <td>]/td[5]/div[@class='yW']</td>
    <td>target2</td>
</tr>
<tr>
    <td>store</td>
    <td>//table/tbody/tr[</td>
    <td>targetBold1</td>
</tr>
<tr>
    <td>store</td>
    <td>]/td[5]/div[@class='yW']//span</td>
    <td>targetBold2</td>
</tr>
<tr>
    <td>store</td>
    <td>1</td>
    <td>i</td>
</tr>
<tr>
    <td>store</td>
    <td>1</td>
    <td>boldCount</td>
</tr>
<tr>
    <td>click</td>
    <td>link=Inbox *</td>
    <td></td>
</tr>
<tr>
    <td>while</td>
    <td>${boldCount}&lt;=3</td>
    <td></td>
</tr>
<tr>
    <td>storeEval</td>
    <td>storedVars['targetBold1']+storedVars['i']+storedVars['targetBold2']+'@class'</td>
    <td>targetBold</td>
</tr>
<tr>
    <td>storeAttribute</td>
    <td>javascript{storedVars['targetBold']}</td>
    <td>targetBoldClass</td>
</tr>
<tr>
    <td>echo</td>
    <td>${targetBoldClass}</td>
    <td></td>
</tr>
<tr>
    <td>gotoIf</td>
    <td>storedVars['targetBoldClass']!='zF'</td>
    <td>jump</td>
</tr>
<tr>
    <td>storeEval</td>
    <td>storedVars['target1']+storedVars['i']+storedVars['target2']</td>
    <td>target</td>
</tr>
<tr>
    <td>clickAt</td>
    <td>javascript{storedVars['target']}</td>
    <td></td>
</tr>
<tr>
    <td>storeText</td>
    <td>class=hP</td>
    <td>subject</td>
</tr>
<tr>
    <td>storeText</td>
    <td>class=iw</td>
    <td>from</td>
</tr>
<tr>
    <td>storeText</td>
    <td>//div[2]/div/div/div/div/div/div/div[2]/div[6]/div</td>
    <td>content</td>
</tr>
<tr>
    <td>storeEval</td>
    <td>javascript{alert(storedVars['from']+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+&lt;br/&gt;+storedVars

['subject']+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+&lt;br/&gt;+storedVars['content'])}</td>
    <td></td>
</tr>
<tr>
    <td>storeEval</td>
    <td>parseInt(storedVars['boldCount'])+1</td>
    <td>boldCount</td>
</tr>
<tr>
    <td>echo</td>
    <td>${boldCount}</td>
    <td></td>
</tr>
<tr>
    <td>goBack</td>
    <td></td>
    <td></td>
</tr>
<tr>
    <td>label</td>
    <td>jump</td>
    <td></td>
</tr>
<tr>
    <td>storeEval</td>
    <td>parseInt(storedVars['i'])+1</td>
    <td>i</td>
</tr>
<tr>
    <td>endWhile</td>
    <td></td>
    <td></td>
</tr>
-------------------------


Here is how the alerting happens.


End of Post.

No comments:

Post a Comment