Home
API and SDKs
Testing E-Mails
Testing E-Mails
In this article you will learn how to programmatically test if emails are correctly sent from a web application or mobile app.
How to do this using the webmate web app is explained here .
Incomplete: Not all snippets available in this language yet! Coming soon!
You need an authenticated webmateSession
to call this function, see this article on how to set up and authenticate with the SDK.
First you need to create the temporary email address to use as receiver for your test mails.
Parameter
projectId
webmate ProjectId of the project the device is running in
testRunId
webmate TestRunId for the current test run
1
2
3
4
WebmateSeleniumSession webmateSeleniumSession = webmateSession. addSeleniumSession ( driver. getSessionId . toString );
TestRunId testRunId = webmateSeleniumSession. getTestRunId ();
TestMailAddress mailAddress = webmateSession. mailTest . createTestMailAddress ( "7c4f09fd-xxx-xxx-xxx-xxx" , testRunId);
You need an authenticated webmateSession
to call this function, see this article on how to set up and authenticate with the SDK.
First you need to create the temporary email address to use as receiver for your test mails.
Parameter
projectId
webmate ProjectId of the project the device is running in
testRunId
webmate TestRunId for the current test run
1
2
let testRunId = webmateSession .addSeleniumSession (browserObj .sessionId ).getTestRunId ();
let mailAddress = await webmateSession .mailTest .createTestMailAddress ("7c4f09fd-xxx-xxx-xxx-xxx" , testRunId ).toPromise ();
After triggering the emails in your application, you can fetch the received emails:
Incomplete: Not all snippets available in this language yet! Coming soon!
1
List< TestMail> mails = webmateSession. mailTest . getMailsInTestRun ( "7c4f09fd-xxx-xxx-xxx-xxx" , testRunId)
The webmate TestMail
class offers the following methods for checking if the correct mails were received:
function
description
getFrom()
get the sender address
getTo()
get the receiver address
getEmailContent()
get the email content
hashCode()
get a hashcode of the mail to compare
toString()
convert the mail to a string of the format “TestMail{from=‘XXX’, to=‘XXX’, emailContent=‘XXX’}”
equals(Object o)()
check if the TestMail equals to another object
1
let mails = await webmateSession .mailTest .getMailsInTestRun ("7c4f09fd-xxx-xxx-xxx-xxx" , testRunId ).toPromise ();
The webmate TestMail
class offers the following variables for checking the mail content:
variable
description
from
get the sender address
to
get the receiver address
emailContent
get the email content
Incomplete: Not all snippets available in this language yet! Coming soon!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public static void waitUntilNumMailsAvailable ( WebmateAPISession webmateSession, TestRunId testRunId, Integer numMails, long interval, WebDriver driver) {
List< TestMail> mails = webmateSession. mailTest . getMailsInTestRun ( "7c4f09fd-xxx-xxx-xxx-xxx" , testRunId);
long start = System. currentTimeMillis ();
long end = start + TestConfig. MAIL_RECEIVE_TIMEOUT * 1000 ;
System. out . println ( "Wait for an email to be received" );
while ( mails. size () < numMails + 1 && System. currentTimeMillis () < end) {
try {
Thread. sleep ( interval);
mails = webmateSession. mailTest . getMailsInTestRun ( "7c4f09fd-xxx-xxx-xxx-xxx" , testRunId);
// make sure the session does not timeout
String currentWindow = driver. getWindowHandle ();
driver. switchTo (). window ( currentWindow);
} catch ( Exception e) {
continue ;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
async function waitUntilNumMailsAvailable (WebmateAPISession webmateSession , TestRunId testRunId , var numMails , var interval , BrowserObject driver ) {
let mails = await webmateSession .mailTest .getMailsInTestRun ("7c4f09fd-xxx-xxx-xxx-xxx" , testRunId ).toPromise ();
var start = Date.now ();
var end = start + TestConfig .MAIL_RECEIVE_TIMEOUT * 1000 ;
console .log ("Wait for an email to be received" );
while (mails .size () < numMails + 1 && Date.now () < end ) {
try {
await new Promise (resolve => setTimeout (resolve , interval ));
await webmateSession .mailTest .getMailsInTestRun ("7c4f09fd-xxx-xxx-xxx-xxx" , testRunId ).toPromise ();
// make sure the session does not timeout
let currentWindow = driver .getWindowHandle ();
driver .switchToWindow (currentWindow );
} catch (err ) {
continue ;
}
}
}
Incomplete: Not all snippets available in this language yet! Coming soon!
1
2
3
WebmateSeleniumSession seleniumSession = webmateSession. addSeleniumSession ( driver. getSessionId (). toString ());
TestMailAddress testMailAddress = webmateSession. mailTest . createTestMailAddress ( WebmateCredentials. MY_WEBMATE_PROJECTID , seleniumSession. getTestRunId ());
waitForElement( driver, By. id ( "customer-email" )). sendKeys ( testMailAddress. getAddress ());
1
2
3
4
5
let testRunId = webmateSession .addSeleniumSession (browserObj .sessionId ).getTestRunId ();
let mailAddress = await webmateSession .mailTest .createTestMailAddress ("7c4f09fd-xxx-xxx-xxx-xxx" , testRunId ).toPromise ();
let mailInput = await browserObj .$ ("#customer-email" );
await mailInput .waitForExist ();
element (by .id ('customer-email' )).sendKeys (testMailAddress );
Last update on 23 October 2023