Tuesday, May 7, 2013

Read first n mails form GMail with 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"

This post explain how to read top/recent n mails from Gmail INBOX. You just need to run the test suite of LoginToGmail and ReadFirstNMails testcases and let Selenium IDE login to your Gmail on your behalf and give continuous n alerts with the contents of the mail.



Here is the Test Case:

store | //table/tbody/tr[ | target1
store | ]/td[5]/div[@class='yW'] | target2
store | 1 | i
click | link=Inbox * |
while | ${i}<=5 |
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'])} |
goBack |  |
storeEval | parseInt(storedVars['i'])+1 | i
endWhile |  |



Commands explained

store | //table/tbody/tr[ | target1
store | ]/td[5]/div[@class='yW'] | target2
store | 1 | i
Store the actual target "//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.



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

while | ${i}<=5 |
Loop thru first 5 mails

storeEval | storedVars['target1']+storedVars['i']+storedVars['target2'] | target
Fo every iteration we are storing the target (Xpath) in a variable named "target"

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

goBack |  |
Go back to Inbox

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>1</td>
    <td>i</td>
</tr>
<tr>
    <td>click</td>
    <td>link=Inbox *</td>
    <td></td>
</tr>
<tr>
    <td>while</td>
    <td>${i}&lt;=3</td>
    <td></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>goBack</td>
    <td></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>
-------------------------

No comments:

Post a Comment