'How to test email verification flow with dynamic email generation and get email data in automation?

I am facing a problem with registration and login flow where it is required to generate an email and get token for verification. I need a service which allows user to generate n numbers of disposable emails with email data access that can self-destructs max 24 hours. Could anyone please share the service code to use in selenium java automation?



Solution 1:[1]

The QA teams leave such tasks for manual testing instead of writing automation tests for these scenarios. \

But, all communication from the email can be automated by the disposable email service. This article will describe how to use this service within the automation suite using Nightwatch.js (Node).

Registration and Login automation

You can use this code as well to automate such things:

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();

}

}

}

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kuldeep chhipa