package bentley.bc.automation import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase import static com.kms.katalon.core.testdata.TestDataFactory.findTestData import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject import static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject import static org.junit.Assert.assertTrue import com.kms.katalon.core.annotation.Keyword import com.kms.katalon.core.checkpoint.Checkpoint import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile import com.kms.katalon.core.model.FailureHandling import com.kms.katalon.core.testcase.TestCase import com.kms.katalon.core.testdata.TestData import com.kms.katalon.core.testobject.TestObject import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI import com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows import com.sun.mail.util.MailSSLSocketFactory import javax.mail.Address import javax.mail.AuthenticationFailedException import javax.mail.BodyPart import javax.mail.Folder import javax.mail.Message import javax.mail.MessagingException import javax.mail.Multipart import javax.mail.NoSuchProviderException import javax.mail.Part import javax.mail.Session import javax.mail.Store import javax.mail.Message.RecipientType import javax.mail.internet.MimeBodyPart import javax.mail.internet.MimeMultipart import javax.mail.search.AndTerm import javax.mail.search.RecipientStringTerm import javax.mail.search.SearchTerm import javax.mail.search.SubjectTerm import internal.GlobalVariable public class EmailHelpers { private Folder folder; public enum EmailFolder { INBOX("INBOX"), SPAM("SPAM"); private String text; private EmailFolder(String text){ this.text = text; } public String getText() { return text; } } /** * default constructor to get the connection to our email inbox */ public EmailHelpers() { // connect to our qa test email inbox connectToInbox(GlobalVariable.qaEmailAccountAddress, GlobalVariable.qaEmailAccountPassword, EmailFolder.INBOX) } /** * Gets a connection to our test email account * @param emailAddress * @param password * @param emailFolder * @return Valid Store * @throws MessagingException */ def Store connectToInbox(String emailAddress, String password, EmailFolder emailFolder) throws MessagingException { Properties props = new Properties(); props.setProperty("mail.store.protocol", "imaps"); Session session = Session.getDefaultInstance(props, null); Store store; try { store = session.getStore("imaps"); store.connect("imap.gmail.com", emailAddress, password); folder = store.getFolder(emailFolder.getText()); folder.open(Folder.READ_WRITE); }catch (NoSuchProviderException e) { e.printStackTrace(); throw e; }catch (MessagingException e) { e.printStackTrace(); throw e; } return store; } def Map getStartAndEndIndices(int max) throws MessagingException { int endIndex = getNumberOfMessages(); int startIndex = endIndex - max; //In event that maxToGet is greater than number of messages that exist if(startIndex < 1){ startIndex = 1; } Map indices = new HashMap(); indices.put("startIndex", startIndex); indices.put("endIndex", endIndex); return indices; } /** * Get the total count of messages in the Inbox * @return message count */ @Keyword def getNumberOfMessages() { int msgCount = folder.getMessageCount() return msgCount } /** * Verify if a message exists in the inbox with a specific subject that matches in FULL * @param subject * @return True or False */ @Keyword def verifyMessageExists(String subject) { Boolean messageExists = false Map indices = getStartAndEndIndices(10) Message[] messages = folder.search(new SubjectTerm(subject), folder.getMessages(indices.get("startIndex"), indices.get("endIndex"))) if (messages.size() == 1) { messageExists = true } if (messageExists == false) { assert messageExists == true: 'QA EMail Inbox does NOT contain email with subject : ' + subject } return messageExists } @Keyword def getLastReceivedEmailBody() { Boolean messageExists = false; String messageContent; int i = getNumberOfMessages(); Message[] foundMessages = folder.getMessages(1,1) // found any? if (foundMessages.size() > 0) { messageContent = getTextFromMessage(foundMessages[0]); println(">>> email content: " + messageContent); } else { assert messageExists == true: 'Did not find any emails' } return messageContent } private String getTextFromMessage(Message message) throws MessagingException, IOException { String result = ""; if (message.isMimeType("text/plain")) { result = message.getContent().toString(); } else if (message.isMimeType("multipart/*")) { MimeMultipart mimeMultipart = (MimeMultipart) message.getContent(); result = getTextFromMimeMultipart(mimeMultipart); } return result; } private String getTextFromMimeMultipart(MimeMultipart mimeMultipart) throws MessagingException, IOException { String result = ""; int count = mimeMultipart.getCount(); for (int i = 0; i < count; i++) { BodyPart bodyPart = mimeMultipart.getBodyPart(i); if (bodyPart.isMimeType("text/plain")) { result = result + "\n" + bodyPart.getContent(); break; // without break same text appears twice in my tests } else if (bodyPart.isMimeType("text/html")) { String html = (String) bodyPart.getContent(); result = result + "\n" + org.jsoup.Jsoup.parse(html).text(); } else if (bodyPart.getContent() instanceof MimeMultipart) { result = result + getTextFromMimeMultipart((MimeMultipart)bodyPart.getContent()); } } return result; } /** * Verify if a message exists in the inbox from a specific email address with a subject * @param emailFrom - case insensitive * @param partialMatch - case SENSITIVE (at the moment) * @return */ @Keyword def verifyMessageExistsByEmailFromAndSubject(String emailFrom, String subjectPartialMatch) { Boolean messageExists = false // build search condition SearchTerm searchCondition = new SearchTerm() { @Override public boolean match(Message message) { try { // test if we have a full or partial match on subject if (message.getSubject().contains(subjectPartialMatch)) { // get address of email (should only ever be 1) Address[] fromAddress = message.getFrom(); // ok to continue? if (fromAddress != null && fromAddress.length > 0) { // check first email address if (fromAddress[0].address.equalsIgnoreCase(emailFrom)) { return true; } } } } catch (MessagingException ex) { ex.printStackTrace(); } return false; } }; // search for messages that match our condition Message[] foundMessages = folder.search(searchCondition); // found any? if (foundMessages.size() > 0) { messageExists = true } else { assert messageExists == true: 'QA EMail Inbox does NOT contain email from ' + emailFrom + ' with a subject matching or containing : ' + partialMatch } return messageExists } }