我有使用selenium webdriver + Java检查忘记密码功能的情况。 以下步骤如下:
1]点击忘记密码链接。
2]输入电子邮件ID以恢复帐户:
3]输入有效的电子邮件ID OTP后发送到电子邮件ID: 到现在为止它工作得很好。
但现在我需要在文本字段中输入OTP:
是否可以自动执行此方案。
如果可能,请建议解决方案。
提前致谢.. :))
答案 0 :(得分:1)
您需要继续使用OTP,因此您无法跳过此部分。以下是您可以尝试的内容
答案 1 :(得分:1)
我建议的替代方式 如果这是你的内部开发,那么
与开发人员协调并在测试环境中以该表单添加一个OTP字段
您可以通过设置db连接从数据库获取OTP,因为您需要数据库凭据和连接器文件来建立连接
您可以在javax.mail
库的帮助下阅读该电子邮件,并通过一些正则表达式匹配从电子邮件中提取OTP代码
以下是
的示例代码package com.tkt.utils;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.mail.BodyPart;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Store;
public class ReadEmail
{
static String hostName ="imap.1and1.com";
static String username = "";
static String password = "";
static int messageCount;
public static String url=null;
public static String userid = null;
public static String pass=null;
//public static void showEmail()
public static void main(String ar[])
{
Properties sysProps = System.getProperties();
sysProps.setProperty("mail.store.protocol", "imap");
try
{
Session session = Session.getInstance(sysProps, null);
Store store = session.getStore();
store.connect(hostName, username, password);
Folder emailInbox = store.getFolder("INBOX");
emailInbox.open(Folder.READ_WRITE);
messageCount = emailInbox.getMessageCount();
System.out.println("Total Message Count: " + messageCount);
int unreadMsgCount = emailInbox.getNewMessageCount();
System.out.println("Unread Emails count:"+unreadMsgCount);
Message emailMessage = emailInbox.getMessage(messageCount);
System.out.println("Email Subject: " + emailMessage.getSubject());
Multipart multipart = (Multipart) emailMessage.getContent();
BodyPart part = multipart.getBodyPart(0);
String responseMessage = part.getContent().toString();
System.out.println("================"+responseMessage);
// Get Email ID
Pattern p = Pattern.compile("EmailId: (.+)");
Matcher m = p.matcher(responseMessage);
if (m.find()) {
userid = m.group(1);
}
System.out.println(userid);
/*Get URL*/
Pattern p2 = Pattern.compile("http(.+)");
Matcher m2 = p2.matcher(responseMessage);
if (m2.find()) {
url = m2.group();
}
System.out.println("URL :="+ url);
/*Get password from email*/
Pattern p1 = Pattern.compile("Password: (.*)");
Matcher m1 = p1.matcher(responseMessage);
if (m1.find()) {
pass = m1.group(1);
}
System.out.println(pass);
/*System.out.println("Email Content: " + emailMessage.getContent().toString());
Pattern p = Pattern.compile("<td>Password:<.*?td>([^<]+)<td>(.+)<.*?td>");
Matcher m = p.matcher(responseMessage);
if (m.find()) {
password1 = m.group(1); // this variable should contain the link URL
}*/
emailMessage.setFlag(Flags.Flag.SEEN, true);
emailInbox.close(true);
store.close();
}
catch (Exception mex)
{
mex.printStackTrace();
}
//return new ReadEmail();
}
}