10

We have merely just had a kick off meeting for a new project that I am required to automate ASAP. The project is essentially a "mailbox project", if you will, and I have no idea as to where to start my test automation on this type of project as I am fairly new to selenium automation. Emails will however be sent/received via Gmail. I would really appreciate any help or advice on how/where to start my automation with regards to this project... if at all possible :).

The framework I am using is Eclipse, TestNG and of course selenium:). Language - Java

1
  • 2
    What does it mean for emails to be sent via Gmail? Do you mean you will use Google's SMTP servers, or do you intend to send emails via the browser?
    – user246
    Commented Jan 19, 2012 at 14:47

11 Answers 11

9

If I remember correctly, automating the use of GMail is a technical violation of their terms of service. It's not something that their likely to take action against you for, as many other people/companies do do this as well.

From Google Terms of Service

5.3 You agree not to access (or attempt to access) any of the Services by any means other than through the interface that is provided by Google, unless you have been specifically allowed to do so in a separate agreement with Google. You specifically agree not to access (or attempt to access) any of the Services through any automated means (including use of scripts or web crawlers) and shall ensure that you comply with the instructions set out in any robots.txt file present on the Services.

Do you actually need to login to the google site in order to do this?

If you wanted to, you could definitely use a third party client, and, it will probably run faster than to access it through the UI.

I'm sure that you could do something similar with Java and a third party client, so, I'll give you how I normally handle email using outlook. I typically setup a folder for each application that I need to validate the emails that were sent. From there, I have my code access the client and then the specific folder. When in the folder, I attempt to Match the From, To, CC, Subject, and body with my expected values. If I don't need to verify much, I'll just pass these in as parameters. If I'm having to validate all of the information, I would typically create some strings that I could set to the values of what I need to check and then attempt the match. This is easier to do than through the UI as well.

2
  • Where the technical violation of automating GMail is concerned,i was not aware of that AT ALL and appreciate you actually making me aware of that,thanx. I have however gave it some thought and the way i was intending on going about this was to actually have a "tester GMail account",whereby i login using the "tester account" credentials and do a simple UI automation technique where i would use the search feature of GMail to assert the unread email(subject) and verify whether all text is present after opening the mail. I really am clueless as to how i can go about this:(.
    – Faheem
    Commented Jan 19, 2012 at 12:54
  • Updated answer with reference and a possible workaround. Commented Jan 19, 2012 at 14:19
4

Look into the Javamail API and Dumbster (Fake SMTP Server).

3

I think you really don’t need to automate Gmail UI unless you are working for Google.

  1. I bet your application requires an SMTP server configuration. So, it uses SMTP protocol to send email to Gmail. In this case you can search Google for “SMTP for developers”. And choose some SMTP server that fits your needs. In this program turns your workstation into the real SMTP server and it will store the emails your application sent into some specific folder as a text file. So, you could check that your application had sent the email and look its content.

  2. But, if the first case is not appropriate for you, then you could use the POP3 client in your Java code. Your Application will send the email using the real smtp server, that email will be stored on the Gmail server and then you could receive it using your test code. You could change/allow the POP3 protocol in the Gmail settings.

Anyway, automated checking of the text formatting through the Gmail interface will be really painful for automation. But if you want to check that the application had sent the email, and the email is in the server and it has correct content – use a POP3 client/Development SMTP Server.

1
  • Hey Dmitry. I've had a look at POP3 client and as i have mentioned before,in my question,i am fairly new to all of this and i merely know the fundamentals in Java. I have however done some research on POP3 client and it seems really simple to implement. So i will definitely give it a shot. Thank you so much :). (I'm really digging this SQA community/website..its awesome :D).
    – Faheem
    Commented Jan 20, 2012 at 8:16
2

Here's what I'll suggest:

Setup a library, have it accept the mails and send to Gmail when your app is running in production mode. But when the app is running in development/testing mode, have your emails stored in database. Now create a simple script to show all mails from the database (keep this script under same URL as app). While testing, access this script from browser to read the emails. We've been doing this from quite a while now.

This way you also speed up your app's testing because you don't have to retrieve data from remote server.

2
  • I will not be the one who will be sending the mails,all i am required to do is to verify that the mail has been sent to the correct user and verify that the content of the mail is correct. The starting point for the mail to be sent however is the website...this is merely a reply to an ad that goes via a message center and i would have to make sure that the process actually works. (The company I work for is a classifieds company,which mainly deals with ads..thus reply via mail to an ad)
    – Faheem
    Commented Jan 19, 2012 at 13:04
  • Yes, you can keep a simple 'test_mails' table in your database with fields like to, from, body, headers etc. etc. and implement a custom assert function then. Of course, this is limited till your application is in test mode. If you're planning to verify this function in production mode, you'll need some other type of setup.
    – Abhimanyu
    Commented Jan 28, 2012 at 10:19
2

Keeping in mind your excerpt : " I have however gave it some thought and the way i was intending on going about this was to actually have a "tester GMail account",whereby i login using the "tester account" credentials and do a simple UI automation technique where i would use the search feature of GMail to assert the unread email(subject) and verify whether all text is present after opening the mail. I really am clueless as to how i can go about this:( "
And Assuming that you are well aware and comfortable /got permission with Google terms of service as mentioned in one of the answers and would like to try from the UI , you could try this bit below:

Login to your "tester gmail acount" in basic HTML view.
Input your search term in the "search box" and search.
If there are no mails in the search results : exit.
While there are unread mail/s (there would be an css identifier to differentiate from read mails): Do Open the mail and verify whether all text is present .

You could quickly bootstrap this code using selenium ide, export the same to eclipse java and add the loops.

OR If you would not prefer to interact with the gmail UI and use a client i.e IMAP , there is some useful stuff here ,which is a superset and can be easily tweaked to cover the issue in concern here.

1
  • 1
    The link you provided in your response is a gem...it really seems to be exactly what i have been looking for and i will most certainly give it a shot :).!* Thanks a mill for your advice/suggestions,its highly appreciated.
    – Faheem
    Commented Jan 20, 2012 at 7:11
2

Try out mailosaur.com It gives you unlimited test email addresses and allow for automated and manual testing of emails.

We have Java, .NET, Ruby, Python and Node.js bindings to make it easy to integrate this in your Selenium test scripts.

Disclosure: I am a co-founder.

2
1

Though the question is not so specific on what part of that web mail is to be automated, the best solution for whatever your question is Selenium IDE.

If your tests involved repeated logins, and sending mails to many then you can instal this addon to firefox and start recording your tests like login to gmail and compose and send mails.

You can find related code and steps for this at stackoverflow and many other blogs such as seleniumhq dot in.

1

I just replaced our Gmail-based tests with a tool called Inbucket (http://www.inbucket.org/) and just query its REST API to check the email contents. It works pretty well so far. If you use c#, it's pretty simple to create a small class to query the API using RestSharp.

0

GMail has official JAVA API. Here is your bag: https://developers.google.com/gmail/api/quickstart/quickstart-java

0

Here is what we did at my company: write a webapp that talks to Gmail over IMAP. The webapp displays your inbox, or individual messages, in a form that is easy for Webdriver to process. It can also display individual messages.

-1

Java Implementation for reading outlook and gmail using javamail api. Here is the working code snippets:

public GmailUtils(String username, String password, String server, EmailFolder emailFolder) throws Exception {
    Properties props = System.getProperties();
    props.setProperty("mail.store.protocol", "imap");
    props.setProperty("mail.imaps.partialfetch", "false");
    props.put("mail.imap.ssl.enable", "true");
    props.put("mail.mime.base64.ignoreerrors", "true");

    Session session = Session.getDefaultInstance(props, null);
    Store store = session.getStore("imap");
    store.connect("imap.gmail.com", 993, "<your email>", "<your password>");

    Folder folder = store.getFolder(emailFolder.getText());
    folder.open(Folder.READ_WRITE);

    System.out.println("Total Messages:" + folder.getMessageCount());
    System.out.println("Unread Messages:" + folder.getUnreadMessageCount());

    messages = folder.getMessages();

    for (Message mail : messages) {
        if (!mail.isSet(Flags.Flag.SEEN)) {

            System.out.println("***************************************************");
            System.out.println("MESSAGE : \n");

            System.out.println("Subject: " + mail.getSubject());
            System.out.println("From: " + mail.getFrom()[0]);
            System.out.println("To: " + mail.getAllRecipients()[0]);
            System.out.println("Date: " + mail.getReceivedDate());
            System.out.println("Size: " + mail.getSize());
            System.out.println("Flags: " + mail.getFlags());
            System.out.println("ContentType: " + mail.getContentType());                
            System.out.println("Body: \n" + getEmailBody(mail));
            System.out.println("Has Attachments: " + hasAttachments(mail));

        }
    }
}



public OutlookEmail(String username, String password, String server, EmailsFolder 
     inbox) throws Exception {
    Properties props = System.getProperties();
    props.setProperty("mail.store.protocol", "imap");
    props.setProperty("mail.imap.ssl.enable", "true");
    props.setProperty("mail.imaps.partialfetch", "false");
    props.put("mail.mime.base64.ignoreerrors", "true");

    Session mailSession = Session.getInstance(props);
    mailSession.setDebug(true);
    Store store = mailSession.getStore("imap");
    store.connect("outlook.office365.com", "<your email>", "<your password>");


    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_WRITE);

    System.out.println("Total Message:" + folder.getMessageCount());
    System.out.println("Unread Message:" + folder.getUnreadMessageCount());

    messages = folder.getMessages();

    for (Message mail : messages) {
        if (!mail.isSet(Flags.Flag.SEEN)) {

            System.out.println("***************************************************");
            System.out.println("MESSAGE : \n");

            System.out.println("Subject: " + mail.getSubject());
            System.out.println("From: " + mail.getFrom()[0]);
            System.out.println("To: " + mail.getAllRecipients()[0]);
            System.out.println("Date: " + mail.getReceivedDate());
            System.out.println("Size: " + mail.getSize());
            System.out.println("Flags: " + mail.getFlags());
            System.out.println("ContentType: " + mail.getContentType());
            System.out.println("Body: \n" + getEmailBody(mail));
            System.out.println("Has Attachments: " + hasAttachments(mail));

        }
    }
}

public boolean hasAttachments(Message email) throws Exception {

    // suppose 'message' is an object of type Message
    String contentType = email.getContentType();
    System.out.println(contentType);

    if (contentType.toLowerCase().contains("multipart/mixed")) {
        // this message must contain attachment
        Multipart multiPart = (Multipart) email.getContent();

        for (int i = 0; i < multiPart.getCount(); i++) {
            MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(i);
            if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
                System.out.println("Attached filename is:" + part.getFileName());

                MimeBodyPart mimeBodyPart = (MimeBodyPart) part;
                String fileName = mimeBodyPart.getFileName();

                String destFilePath = System.getProperty("user.dir") + "\\Resources\\";

                File fileToSave = new File(fileName);
                mimeBodyPart.saveFile(destFilePath + fileToSave);

                // download the pdf file in the resource folder to be read by PDFUTIL api.

                PDFUtil pdfUtil = new PDFUtil();
                String pdfContent = pdfUtil.getText(destFilePath + fileToSave);

                System.out.println("******---------------********");
                System.out.println("\n");
                System.out.println("Started reading the pdfContent of the attachment:==");


                System.out.println(pdfContent);

                System.out.println("\n");
                System.out.println("******---------------********");

                Path fileToDeletePath = Paths.get(destFilePath + fileToSave);
                Files.delete(fileToDeletePath);
            }
        }

        return true;
    }

    return false;
}

public String getEmailBody(Message email) throws IOException, MessagingException {

    String line, emailContentEncoded;
    StringBuffer bufferEmailContentEncoded = new StringBuffer();
    BufferedReader reader = new BufferedReader(new InputStreamReader(email.getInputStream()));
    while ((line = reader.readLine()) != null) {
        bufferEmailContentEncoded.append(line);
    }

    System.out.println("**************************************************");

    System.out.println(bufferEmailContentEncoded);

    System.out.println("**************************************************");

    emailContentEncoded = bufferEmailContentEncoded.toString();

    if (email.getContentType().toLowerCase().contains("multipart/related")) {

        emailContentEncoded = emailContentEncoded.substring(emailContentEncoded.indexOf("base64") + 6);
        emailContentEncoded = emailContentEncoded.substring(0, emailContentEncoded.indexOf("Content-Type") - 1);

        System.out.println(emailContentEncoded);

        String emailContentDecoded = new String(new Base64().decode(emailContentEncoded.toString().getBytes()));
        return emailContentDecoded;
    }

    return emailContentEncoded;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.