
JMeter is most widely used open source tools for load testing . we have used jmeter for different kind of load testing but in this tutorial we will see how we can perform the Email load testing by using JMeter.

Sending email through JavaEmail API
The JavaMail API defines classes which represent the components of a mail system. JavaMail does not implement an email server, instead it allows you to access an email server using a Java API. In order to test the code presented, you must have access to an email server. While the JavaMail API specification does not mandate support for specific protocols, JavaMail typically includes support for POP3, IMAP, and SMTP.

Prerequisite
1.Java should be installed on your machine , check this link to download the latest JDK version based on your local system.
2.Download the JMeter if you don’t have.
3.Install an Eclipse or Intellij IDE.
4.Open Eclipse and create a java project.
5.Add below dependency to the POM for JavaEmail APIs:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.6.2</version>
</dependency>
6.Create an Class file and write the code to send the Email with single and multiple attachments by using JavaEmail APIs. Write different function as per your requirement and test them in eclipse.You can find the code from Git link here.

7.Right Click your class file in Eclipse and select the Export option.
8.Select the JAR file option and click Next button.

9.Select the resource class file to export and browse the location of generated JAR file on you local system and click Finish button.You will get a JAR file.

10.Open the JMeter by clicking on jmeter.bat file.

11. Go to the Test plan and browse the JAR file into JMeter.

12. Add the Thread Group ,CSV Data Set Config and BeanShell Sampler to the Test Plan and add variable to test plan.

Note – from User Defined variable you can change the Email server ,To Email Address , cC Email Address ,path of file attachment and csv file path of from email addresses.
add file path of csv file of From Email Address and password to CSV Data Set Config.

13. Add the code to bean shell sampler by importing the package from JAR file and call the function written inside the JAR file. PFB code for sending email with single attachment.
import com.test.EmailUtil;
int threadNum = ctx.getThreadNum();
int itr = vars.getIteration();
String strFilePath = vars.get("AttachmentFilePath");
String strToEmailID = vars.get("ToEmailID");
String strFromEmailID = vars.get("FromEmailID");
String strcCEmailAddresses = vars.get("ccEmailAddresses");
String strHostName = vars.get("HostName");
String strPassword = vars.get("Password");
try{
EmailUtil emailObj = new EmailUtil();
String result = emailObj.sendMailWithSingleAttachment("Test Email from User-"+String.valueOf(threadNum)+"-"+String.valueOf(itr),strFilePath,strToEmailID,strFromEmailID,strcCEmailAddresses,strHostName,strPassword);
log.info("******************Email Status="+result+" User-"+String.valueOf(threadNum)+" Iteration-"+String.valueOf(itr));
}
catch (Throwable ex) {
log.error("Error: ", ex);
throw ex;
}

similarly for sending email with multiple attachement:
import com.test.EmailUtil;
int threadNum = ctx.getThreadNum();
int itr = vars.getIteration();
String strFilePath = vars.get("AttachmentFilePath");
String strToEmailID = vars.get("ToEmailID");
String strFromEmailID = vars.get("FromEmailID");
String strcCEmailAddresses = vars.get("ccEmailAddresses");
String strHostName = vars.get("HostName");
String strPassword = vars.get("Password");
try{
EmailUtil emailObj = new EmailUtil();
String result = emailObj.sendMailWithMultipleAttachments("Test Email from User-"+String.valueOf(threadNum)+"-"+String.valueOf(itr),strFilePath,strToEmailID,strFromEmailID,strcCEmailAddresses,strHostName,strPassword);
log.info("******************Email Status="+result+" User-"+String.valueOf(threadNum)+" Iteration-"+String.valueOf(itr));
}
catch (Throwable ex) {
log.error("Error: ", ex);
throw ex;
}
14. Go to the thread group and set the number of threads and iterations you want load test you email functionality and run the test.

In that we can perform the email load testing with different scenarios like email with single and multiple attachments etc.
References:
http://learn-automation.com/send-report-through-email-in-selenium-webdriver/
https://www.linkedin.com/pulse/using-custom-jar-beanshell-sampler-jmeter-lakshmi-narayan/
Categories: Out of Box
1 reply »