Selenium with a disposable email

Registration and Login automation using selenium with a disposable email


Selenium is a widely used and popular open-source automation tool in the automation testing industry by the developers. As wherever we searched about the automation tool to test any application which runs on web browsers, Selenium ranks first along with JAVA even it supports so many other languages like** JAVA, C#, Python, Ruby, Pearl, JavaScript, PHP**.

We majorly faced the issues with mailing projects where some of the automation test cases require verification of email, check welcome email, OTP to log in, multi-factor authentication on email for login, etc. Sometimes we are required to check mail content or just to verify email received or not. As major applications/projects work with unique email address features and enforce to check email before accessing the application, Here our problems begin with an automation logic that how to handle the mail feature dynamically?

In this article, we will discuss a Disposable Email Service named Mail7.io developed a first of its kind Email Workflow Testing tool for Software Developers and Testers. This service allows receiving emails at a temporary address that self-destructs after 24 hours. With the help of Mail7.io we can easily automate the email verification workflow of any application.

Here I am demonstrating Mail7 Service with using Selenium & JAVA with the below-mentioned steps:- (PS I am assuming that you are pretty much familiar with Selenium and JAVA ).

1. Explore the API’s of Mail7 and know how it works:-

Mail7 Apis collection has Get Email, Single Email, and Delete Email Apis. For our usage only Get Email API is required for authentication of email workflow.

  1. Get Email -

Get : https://api.mail7.io/inbox

This API will allow you to send a request for the same and retrieve emails including attachments that were received in the provided email address.

Get Email API retrieves emails of only one private id at a time. For this, Request parameters will be API Key, API Secret, Username, and Domain Response will receive in JSON format to extract mail content.

**Request Data:**

  
|**domain**  | **string** |
|--|--|
| **to** |  **string**|
|  **apikey**| **string** |
|**apisecret**  |  **string** |
**Response Data :**
{
"status": "success",
"data": [
{
"_id": "5e393141dab117001799a5d1",
"to_username": "{{username}}",
"to_domain": "{{hostname}}",
"mail_source": {
"attachments": [],
"headers": {.....},
"headerLines": [{....}],
"html": "Email Message\n",
"text": "Email Message\n",
"textAsHtml": "Email Message",
"subject": "Subject",
"date": "2020-02-04T08:54:12.000Z",
"to": {
"value": [{
"address": "{{username}}@{{hostname}}",
"name": ""
}],
"html": "{{username}}@{{hostname}}",
"text": "{{username}}@{{hostname}}"
},
"from": {
"value": [{
"address": "{{from_email_address}}",
"name": "{{from_username_address}}"
}],
"html": "{{from_username_address}} <{{from_email_address}}>",
"text": "{{from_username_address}} <{{from_email_address}}>"
},
"messageId": ""
},
"is_deleted": false,
"is_read": true,
"sender_time": "2020-02-04T08:54:25.711Z"
}]}

2. Write down the logic of getEmail in common-function class and add dependencies in pom.xml file :

 <dependency>

<groupId>com.mashape.unirest</groupId>

<artifactId>unirest-java</artifactId>

<version>1.4.9</version>

</dependency>

We will use Unirest for handling the Mail7 API code. it is a set of lightweight HTTP libraries available in multiple languages, built and maintained by Mashape, who also maintain the open-source API Gateway Kong.

Create a mail7.java file with below code

import org.apcahe.commons.lang3.RandomStringUtils;

public class mail7{

private static final String EMAIL-DOMAIN = ‘mail.io’;
private static final String EMAIL_APIKEY = ‘mail7_api_key’;
private static final String EMAIL_APISecret = ‘mail7_api_secret’;
private String emailid;

public usernameGenerator(){

String username = RandomStringUtils.randomAlphanumeric(8).toLowerCase();
System.out. println(“Random Username is ” + username);
return username;
}

public getInbox(String username){

HttpResponse <String> httpResponse = Unirest.get(“"https://api.mail7.io/inbox?apikey=" + EMAIL_APIKEY + "&apisecret=" + EMAIL_APISecret + "&to=" + username”)
.asString();
System.out.println( httpResponse.getHeaders().get("Content-Type"));
System.out.println(httpResponse.getBody());
return httpResponse.getBody();

}

3. Create a class file for Test Script of Register and Login event :

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.List;
import java.util.concurrent.TimeUnit;  

public class TestEmail throws IOException, UnirestException
{

public static void main(String[] args) {

//create a Selenium WebDriver instance
System.setProperty("webdriver.gecko.driver","dir_path\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();

//launch the Firefox browser and navigate to the website
driver.get(“YOUR_TEST_URL");

//puts an implicit wait for 10 seconds before throwing exceptions
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

//locate the email field
WebElement email = driver.findElement(By.xpath("//input[@type='email']"));

// create a random email address

String username = usernameGenerator();

String emailID = username.concat(“@”+EMAIL_DOMIAN);

//set the field's value
email.sendKeys(emailID);

//locate the password field
WebElement password = driver.findElement(By.xpath("//input[@type='password']"));

//set the password's value
password.sendKeys("password");

//locate and click the submit button
driver.findElement(By.xpath("//input[@type='submit']")).click();

//check if the mail has been received or not
String response = getInbo(username );

if(response.isEmpty()) {
System.out.println("Test not passed");
}else {
System.out.println("Test passed");
}

//close the Firefox browser.
driver.close();
}
}
}
}

Conclusion: PS this is the logic to automate email verification in Selenium without a hassle. With Mail7 service, you can access the contents of the delivered messages and extract texts, attachments automatically, which aid to check whether your email application is functioning as desired.

Remarkably, Mail7 offers you a robust testing platform for validating the functionalities of your emails before distributing them to real users. You should check it a try today!