
JMeter performance testing is the process of testing the performance of a web application using JMeter. As open-source, Java-based software, JMeter can also be used for functional testing and load testing. When you want to load test user registration scenario where your entered an email ID then a user registration code used to sent to verify the email to complete the user registration process. For such scenario we need to fetch the code from email and pass it following API request .
To fetch the code from email id we need to perform below actions using JMeter:
- JMeter connection with email account.
- Reading of that particular email from list of emails under inbox.
- Fetching of code from email body and storing into JMeter variable.
Lets fetch user registration or email verification code from Gmail account.

1.Setup Gmail Account
Setup gmail account to access by third party application like JMeter. Go to gmail account and click “See all settings”

Enable IMAP by selecting the radio button and click “Save Changes”

Now click your google account

Then click “Manage your Google Account” and click “Security” option on the left and enable/activate 2-step verification

Then click “App Passwords” to setup password. copy and save the generated password

2.Reading Email .
Use eclipse and create a maven project by using below pom xml.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qautomation.email</groupId>
<artifactId>QAUTO_Fetch_Email</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>QAUTO_Fetch_Email</name>
<dependencies>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
</dependencies>
</project>
User below code to fetch the email body content with a subject text:
package com.qautomation.utility;
import java.util.Properties;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeBodyPart;
public class EmailUtil {
public static String getEmailContentBySubject(String UserEmailID,String UserPassword,String EmailSubject)
{
String recivingHost = "imap.gmail.com";
String contentType = "";
String messageContent ="";
Properties props2 = System.getProperties();
props2.setProperty("mail.store.protocol","imaps");
Session session2 = Session.getDefaultInstance(props2,null);
try
{
Store store=session2.getStore("imaps");
store.connect(recivingHost,UserEmailID,UserPassword);
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
Message messages[] = folder.getMessages();
for(int i=messages.length - 1; i >=0;i--)
{
Message message = messages[i];
if(message.getSubject().contains(EmailSubject))
{
contentType = messages[i].getContentType();
if(contentType.toLowerCase().contains("multipart"))
{
Multipart multipart = (Multipart) messages[i].getContent();
int numberOfParts = multipart.getCount();
for(int partCount = 0; partCount < numberOfParts; partCount++)
{
MimeBodyPart part = (MimeBodyPart)multipart.getBodyPart(partCount);
if(!part.getContent().toString().equalsIgnoreCase(""))
{
messageContent = part.getContent().toString();
}
}
}
else if(contentType.toLowerCase().contains("text/plain") || contentType.toLowerCase().contains("text/html"))
{
Object content = messages[i].getContent();
if(content != null)
{
messageContent = content.toString();
}
}
messages[i].setFlag(Flags.Flag.DELETED,true);
}
}
folder.close(true);
store.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return messageContent;
}
}
Do a dry run to check if all working fine:

Now create a jar file for the class to be use in JMeter. Right click the class file and select export option.

Select the JAR file option in export wizard

Then select the jar file and location and click finish:

3.Fetching Code using JMeter:
Open JMeter ,create a script and add jar file in test plan

Add a Beanshell sampler in thread group and use below code:
import com.qautomation.utility.EmailUtil;
EmailUtil oEmail = new EmailUtil();
String emailBody = oEmail.getEmailContentBySubject("qautomationinfo@gmail.com","<gmail_app_paasword>","User Registration Code");
log.info("********************Email Body="+emailBody);
vars.put("EmailBody",emailBody);
Run the script:

Cheer’s 🙂
Categories: PerformanceTesting