SUCCESS! So a fellow manager and I spent 4 hours working on this and finally came up with the solution together. He doesn’t really do java and never seen Katalon till I showed it to him. So we put our heads together and hit the web and used this as a starting point:
https://stackoverflow.com/questions/36707939/how-to-extract-a-registration-url-from-a-mail-content
Here is how I made it work in Katalon, with outlook and with returning a URL I can navigate to:
First I created a package and a class under keywords. I import the package and class into my test when I use it.
Here is the code I have in the class:
package emailVerification
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.mail.Folder;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.jsoup.Jsoup as Jsoup;
/*
* from https://stackoverflow.com/questions/36707939/how-to-extract-a-registration-url-from-a-mail-content
* Author(s): B_L
* Created: 01/30/2019
*
* Last Modified by: B_L
* Last Modified: 01/30/2019
*
*
* Purpose: Grab verify link from email
*
*/
public class FetchEmailVarLink {
public static check(String host, String storeType, String user,
String password) {
try {
String emailLink = ''
//create properties field
Properties properties = new Properties();
properties.put("mail.imap.host",host);
properties.put("mail.imap.port", "993");
properties.put("mail.imap.starttls.enable", "true");
properties.setProperty("mail.imap.socketFactory.class","javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.imap.socketFactory.fallback", "false");
properties.setProperty("mail.imap.socketFactory.port",String.valueOf(993));
Session emailSession = Session.getDefaultInstance(properties);
//create the imap store object and connect with the pop server
Store store = emailSession.getStore("imap");
store.connect(host, user, password);
//create the folder object and open it
Folder emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
// retrieve the messages from the folder in an array and print it
MimeMessage[] messages = emailFolder.getMessages();
//System.out.println("messages.length---" + messages.length);
int n=messages.length;
//change n- to number of emails you want to dig through
for (int i = n-5; i<n; i++) {
MimeMessage message = messages[i];
ArrayList<String> links = new ArrayList<String>();
if(message.getSubject().contains("Verify your email for project") || message.getSubject().contains("Sign in to project")){
//System.out.println("Subject: " + message.getSubject());
MimeMultipart messageBody = message.getContent();
String desc = messageBody.getBodyPart(1).getContent().toString();
//System.out.println("Description: " + desc);
Pattern linkPattern = Pattern.compile("href=\"(.*?)\"", Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
Matcher pageMatcher = linkPattern.matcher(desc);
while(pageMatcher.find()){
links.add(pageMatcher.group(1));
}
}else{
System.out.println("Email:"+ i + " is not a wanted email");
}
for(String temp:links){
if(temp.contains("emailSubjectHere")){
emailLink = temp;
//System.out.println(temp);
}
}
}
emailLink = Jsoup.parse((emailLink).toString()).text();
System.out.println(emailLink);
return emailLink;
//close the store and folder objects
emailFolder.close(false);
store.close();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getEmailLinkFromEmail(){
String host = "outlook.office365.com";
String mailStoreType = "imap";
String username = "yourEmail";
String password = "yourPassword";
String emailLink = check(host, mailStoreType, username, password);
return emailLink;
}
}
short example:
import emailVerification.FetchEmailVarLink
String emailLink = FetchEmailVarLink.getEmailLinkFromEmail()
WebUI.comment(emailLink)
WebUI.navigateToUrl(emailLink, FailureHandling.CONTINUE_ON_FAILURE)
Long example in a test case:
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 com.kms.katalon.core.checkpoint.Checkpoint as 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 as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable
import miscMethods.MiscMethods
import filePath.FilePath
import areaValuesAndObjects.Page1
import areaValuesAndObjects.Page2
import areaValuesAndObjects.Login
import emailVerification.FetchEmailVarLink
/*
* Author: B_L
* Last Modified: 01/30/2019
* Last Modified By: B_L
*
*/
'-------------DECLARELIST---------------'
String FilePath = FilePath.getScreenShotFilePath()
String screenShotPath = FilePath + '02_testfolder\\'
'-------------START-AUTOMATION---------------'
WebUI.openBrowser(MiscMethods.getCOP_URL(), FailureHandling.CONTINUE_ON_FAILURE)
WebUI.maximizeWindow()
WebUI.click(Page1.clickLink, FailureHandling.CONTINUE_ON_FAILURE)
WebUI.setText(Login.emailAddressInput, 'yourEmail', FailureHandling.CONTINUE_ON_FAILURE)
WebUI.click(Login.loginButton, FailureHandling.CONTINUE_ON_FAILURE)
WebUI.delay(2)
WebUI.takeScreenshot(screenShotPath + '01 screenname.jpg', FailureHandling.OPTIONAL)
WebUI.delay(1)
WebUI.refresh()
WebUI.delay(1)
WebUI.delay(10)
String emailLink = FetchEmailVarLink.getEmailLinkFromEmail()
WebUI.comment(emailLink)
WebUI.navigateToUrl(emailLink, FailureHandling.CONTINUE_ON_FAILURE)
WebUI.verifyElementPresent(Page2.thingLabel, 0, FailureHandling.CONTINUE_ON_FAILURE)
WebUI.verifyElementPresent(Page2.otherThingLabel, 0, FailureHandling.CONTINUE_ON_FAILURE)
WebUI.delay(1)
WebUI.takeScreenshot(screenShotPath + '02 Navigating to link from email.jpg', FailureHandling.OPTIONAL)
WebUI.delay(1)
WebUI.closeBrowser()
I hope this is helpful for everyone this was a nightmare for me as I am not really a programmer. Self taught on java when I looked into Katalon. I tried to take out custom data so no secure data is shared but you still get use out of it.