可以使用selenium来测试Microsoft Outlook中电子邮件的电子邮件内容

时间:2013-12-20 07:31:46

标签: email testing selenium

我正在使用Selenium来测试Web应用程序。 其中一项功能是“上传文件” -

我上传文件后,会在后端执行cron作业以上传我的文件。一旦上传文件,用户就会收到一封电子邮件,通知上传的文件包含有关该文件的所有信息。

我在Microsoft Outlook上收到此电子邮件。

我有什么方法可以在Microsoft Outlook中测试此功能和电子邮件的内容吗?

4 个答案:

答案 0 :(得分:1)

测试电子邮件的最佳选择是询问电子邮件服务器(如果客户端连接到IMAP,最好在IMAP上配置它:-))。使用Apache Commons Email

在Java中轻松实现Java邮件客户端

如果您确实要连接到Outlook,则可以使用@ABP建议的连接器。见this SO question (and answer)

如果你真的(真的吗?)想要使用selenium,你会被困住,因为Selenium的基础是它的驱动程序(对于Firefox,Chrome等)而且没有Outlook的驱动程序(Selenium是网站自动化) , 毕竟)。 API本身用于单击链接,但没有用于检索电子邮件

如果您喜欢冒险,可以打赌Sikuli这是一个功能强大的桌面自动化解决方案,至少可以打开Outlook,检查电子邮件,并检查电子邮件主题是否与某种模式匹配。但它是OCR,当你开始进行OCR测试时......事情真的很快就会很糟糕

答案 1 :(得分:0)

您是否拥有电子邮件的网络版。 Outlook是电子邮件客户端。你有任何基于Web的电子邮件客户端,你可以这样做。我不认为你可以使用Selenium从outlook中读取..但在Java中我认为你会找到可以使用的Outlook连接器。

答案 2 :(得分:0)

driver.findElement(By.id("username")).sendKeys("xyz@company.com");

driver.findElement(By.id("password")).sendKeys("password");
driver.findElement(By.xpath("submit")).click();
Thread.sleep(3000);

//点击“安排方式”

driver.findElement(By.cssSelector("#tdAB")).click();

//点击选择以安排

driver.findElement(By.cssSelector("#spnT")).click();

//来到列表顶部

Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_HOME);
    robot.keyRelease(KeyEvent.VK_HOME);

//Procedure to find first email by subject and click the link in it

    Thread.sleep(5000);
List<WebElement> email = driver.findElements(By.cssSelector("#divSubject"));

for(WebElement emailsub : email){

    if(emailsub.getText().equals("<email subject>") == true){
           emailsub.click();
           break;
        }
    }

答案 3 :(得分:0)

这正是我项目中的要求。我已经通过使用JAVAX MAIL API和JAVA对此进行了排序。此外,它还会检查附件,如果是,请阅读pdf。

  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;

}