Receive an Email and click a link?

Here is an odd one… the application I’m testing you sign up and click a link that is emailed to you. How would I even do that? Would I have to automate logging into outlook?

2 Likes

I assume you mean outlook.com (MS web-based email)? It certainly sounds like that is what you need to do. However, testing all possible paths that scenario might take sounds daunting and effectively impossible (what if the person only uses their phone for email? What about OS-native email clients? Would you use Winium et al?)

The other thing you might consider is “mocking” the emailed-link. That is, do enough to prove the link was sent out, then use a mock link to re-access the site independent of the emailed-link.

Please post back how you choose to approach this - it’s an intriguing problem.

1 Like

Yes outlook.com, I think its a terrible way to do it as well but first thing that came to mind. I’ll certainly share anything I come up with. I’ve been researching and thinking something like this might work:
https://www.quora.com/How-can-we-verify-an-email-via-Selenium-WebDriver
http://angiejones.tech/test-automation-to-verify-email/

Essentially write java that reads from the mail service. That would work but getting the link will be tricky. If I can pull the link out as a variable and pass that into a browse too I could do what I’m trying. Might take me a bit though.

What is the reason behind this scenario? You want to verify generated link is correct in term of its server address and generated activation token if any? Or extend further is when you click on this link the browser will redirect you to successful page?
Anyway in the first case :

  1. Clean-up the email inbox before this test scenario,any new email matching a pattern like ‘Welcome to my site…’ will be the correct activation email.
  2. Open this email and search for any text matching with the activation link. Make sure the server address is asserted first,and then assert generated activation token using regex comparison.
    You can store the search algorithm to be a variable and paste it directly to a browser

I believe if you really need to test this ,just make sure there is only one emailed-link in the email

Just an ideal to approach this test scenario.

3 Likes

Thanks, Blaze. That Quora thread was interesting.

Whatever you come up with, it would be nice if you could post a generic version (if possible) in the Tips & Tricks forum.

Take your time. I’m sure it’ll be worth it in the end

Addendum: Just saw Vinh’s approach arrive. Yes, that’s interesting too. Vinh’s main point seems to be, you’re not meant to be testing outlook.com – that’s someone else’s job :wink:

1 Like

This.

And this.

Testing an email service sounds like it should be out-of-scope of your responsibility to test the application itself. If you know the link that is injected into the email, this should suffice.

I’m not really understanding my error…any thoughts?

ohhh it’s maven stuff… not sure how to get this into Katalon…

Hi,

do it your java stuff in e.g in JIDEA (maven project) and then create artifacts .jar file
import jar to Katalon (Drivers folder)
create Keyword where call java methods
like
in Katalon groovy class
//from .jar file
import com.package.java_class_name.*;

//create object
<package. object = new package.java_class_name();

//call method
object.java_method();

Alternative: https://github.com/katalon-studio/gradle-plugin

Unfortunately there’s no dependency management tool integrated into the studio, so you would need to download a jar for each external library that you need. Follow this guide:

https://docs.katalon.com/katalon-studio/tutorials/import_java_library.html

Looks like I might not even need maven for this I just get this error:

Groovy:No expression for the array constructor call at line: 

which is referring to this:

messages = unreadMessages.toArray(new Message[]{});

this is the only error in code when I’m trying to set this up but don’t understand what to do about it…

I’ve tried some rewrites but all give different errors.

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.

4 Likes

Hi,

I am new in katalon.I want to automated the scenario in Outlook in which i need to click on search box and enter the text.

Could you please me.

I am trying to fetch the xpath for search box but it is not working.

Thanks,
Sristi.

You can automate outlook.com but not the Outlook executable (at least, not without using Winium).

However, I would refer you to the statement I made earlier in this thread:

Hey @Russ_Thomas

Thanks for replying.
Scenario which included following steps:
1.login to outlook.
2. click and enter the data on search text box.
3.Click on that filtered mail.
4.click on one button present on that email which opens the new window from the application.

Please let me know how can I automate these scenario.What should I do.

Thanks,
Sristi.

I am not able to understand this step. How we can filter email without giving any conditions ? I think you want to login into Outlook and then click on the Search text box, enter text to search ( that will display filtered data), then click on the filtered data and so on.

Hi @manpreet.mukkar

You are right.its my mistake.

Thanks for correcting it.
Could you please help me in that.
Thanks.

@sristisharma90

If you haven’t started automating this scenario yet then you can start recording this scenario using the recording functionality within the Katalon Studio, if you encounter any issue you can post it over here and someone would pick it up to help you automate it. Please follow this format

Hi Team, How can we handle the Xpath having Spaces   in Subject Line

As I need to verify the Notification mailers