我在注册和登录流程中遇到了一个问题,需要生成一个邮件并获得验证的令牌,我需要一个服务,让用户生成n个数量的一次性邮件,并能在24小时内自毁。我需要一个服务,允许用户生成n个数量的一次性邮件,并能在24小时内自动销毁。谁能分享一下服务代码,以便在selenium java自动化中使用?
QA团队把这样的任务留给手动测试,而不是为这些场景编写自动化测试。\
但是,所有来自邮件的通信都可以通过一次性邮件服务实现自动化。本文将介绍如何使用Nightwatch.js(Node)在自动化套件中使用该服务。
你也可以用这段代码来自动完成这样的事情。
2. 在comm-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构建和维护,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();
}
}
}
}