【问题标题】:How to test email verification flow with dynamic email generation and get email data in automation?如何使用动态电子邮件生成测试电子邮件验证流程并自动获取电子邮件数据?
【发布时间】:2020-09-28 15:30:19
【问题描述】:

我在注册和登录流程方面遇到问题,需要生成电子邮件并获取令牌以进行验证。我需要一项服务,它允许用户生成 n 封具有电子邮件数据访问权限的一次性电子邮件,这些电子邮件可以自毁最多 24 小时。任何人都可以分享在 selenium java 自动化中使用的服务代码吗?

【问题讨论】:

  • 如果您在本地基础架构中部署待测应用,您可以使用GreenMail 服务器来完全控制您的电子邮件流量

标签: java selenium-webdriver qa browser-automation


【解决方案1】:

QA 团队将此类任务留给手动测试,而不是为这些场景编写自动化测试。 \

但是,来自电子邮件的所有通信都可以由一次性电子邮件服务自动执行。本文将介绍如何使用 Nightwatch.js (Node) 在自动化套件中使用此服务。

Registration and Login automation

您也可以使用此代码来自动执行此类操作:

2。在 common-function 类中写下 getEmail 的逻辑,并在 pom.xml 文件中添加依赖:


<dependency>



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



<artifactId>unirest-java</artifactId>



<version>1.4.9</version>



</dependency>

我们将使用 Unirest 来处理 Mail7 API 代码。它是一组轻量级 HTTP 库,提供多种语言版本,由 Mashape 构建和维护,同时维护开源 API Gateway Kong。

使用以下代码创建一个 mail7.java 文件




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。为注册和登录事件的测试脚本创建一个类文件:


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

}

}

}

}

【讨论】:

    猜你喜欢
    • 2022-11-17
    • 2019-05-28
    • 2015-11-24
    • 2021-09-21
    • 1970-01-01
    • 2023-03-29
    • 2020-07-19
    • 1970-01-01
    相关资源
    最近更新 更多