Sunday, April 21, 2013

Automatically login to GMail and send a mail using Selenium IDE ? - GMail automated.

      By just recording the login and emailing steps you can automatically login to GMail and send a mail using Selenium IDE.

This is very useful when you want to send test mails as part of testing, this saves time when you have to send different mails to different mailing list. You can use the html source and fill the contact lists and compose the mails prior to doing the task. You need not wait for server response for each individual mail sent status.

Here is the recorded code :

Login to GMail: 



Source



 ------------------------------------
 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="https://accounts.google.com/" />
<title>LoginToMail</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">LoginToMail</td></tr>
</thead><tbody>
<tr>
    <td>open</td>
    <td>/ServiceLogin?service=mail&amp;passive=true&amp;rm=false&amp;continue=https://mail.google.com/mail/&amp;ss=1&amp;scc=1&amp;ltmpl=default&amp;ltmplcache=2&amp;hl=en</td>
    <td></td>
</tr>
<tr>
    <td>type</td>
    <td>id=Email</td>
    <td>McKiran01234@gmail.com</td>
</tr>
<tr>
    <td>type</td>
    <td>id=Passwd</td>
    <td>*******</td>
</tr>
<tr>
    <td>clickAndWait</td>
    <td>id=signIn</td>
    <td></td>
</tr>
</tbody></table>
</body>
</html>

-----------------------------------


Compose and send mail 




Explanation:


  • setTimeOut command is used to rewrite the default timeout of 30 secs to 3 secs as open command unnecessarily waits here for 30 secs to realize the compose window is opened.
  •  The click command after open command does a bit tricky work, it actually click the "Recipients" field to make it "TO" field then the type commands recognizes the "TO" field and types the data.
  • The value parameter for last "type" command has the below text with inline html tags as below:
                          Hi All<br/>Hope the test is successful. <br/><br/><i> Lovingly, </i></br<b>McKiran.</b>
  • This is done to compose a mail that looks as if it is composed in mail editor (Rich Text Format). HTML is automatically rendered to show as rich text. Look at last image below to see how the mail content looks.

Source:

------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="https://mail.google.com/" />
<title>ComposeMail</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">ComposeMail</td></tr>
</thead><tbody>
<tr>
    <td>setTimeout</td>
    <td>3000</td>
    <td></td>
</tr>
<!--open command usually waits for 30 secs, so we are now asking it to wait for only 3 secs-->
<tr>
    <td>open</td>
    <td>/mail/u/0/?shva=1#inbox?compose=new</td>
    <td></td>
</tr>
<tr>
    <td>click</td>
    <td>class=aoD hl</td>
    <td></td>
</tr>
<tr>
    <td>type</td>
    <td>//table/tbody/tr[1]/td[2]/div/div/textarea</td>
    <td>McKiran@tested.com</td>
</tr>
<tr>
    <td>type</td>
    <td>name=subjectbox</td>
    <td>Test</td>
</tr>
<tr>
    <td>type</td>
    <td>class=editable LW-avf</td>
    <td>Hi All&lt;br/&gt;Hope the test is successful. &lt;br/&gt;&lt;br/&gt;&lt;i&gt; Lovingly, &lt;/i&gt;&lt;/br&gt;&lt;b&gt;McKiran.&lt;/b&gt;</td>
</tr>
<tr>
    <td>click</td>
    <td>class=T-I J-J5-Ji aoO T-I-atl L3</td>
    <td></td>
</tr>
</tbody></table>
</body>
</html>
------------------------------------------


SignOut/LogOut
 




Source

----------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="https://mail.google.com/" />
<title>New Test</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">New Test</td></tr>
</thead><tbody>
<tr>
    <td>click</td>
    <td>css=span.gbmai</td>
    <td></td>
</tr>
<tr>
    <td>clickAndWait</td>
    <td>text=&quot;Sign out&quot;</td>
    <td></td>
</tr>
</tbody></table>
</body>
</html> 

----------------------------------------


This is how it looks at GMail:



4 comments:

  1. [error] Element id=Email not found
    i am getting this error while login the thing is its showing failures:1 even its logged in
    can u please help

    ReplyDelete
    Replies
    1. Hey Naveen,
      Your query is not clear, I didn't get if u were already logged in Or trying to log in?
      I hope you knew that the code is trying to find the email text field to fill email/username.

      If you are already logged in then there is no point in finding email field (to login again). To remind you again, that command works only at login page. make sure you are already this page and set your IDE execution speed to moderate (not speed) Or slow.

      Delete
  2. mm nice post but... when i tried to switch to"canvas_frame" its throwing an error "Unable to locate frame: canvas_frame" you got any other solution for this....

    ReplyDelete
    Replies
    1. Are you using "selectFrame" command? Can you give me more info on where/what u tried to do?

      Delete