1

I am having scenario to check forgot password functionality using selenium webdriver + Java. Following steps am following :

1] Click on forgot password link.

2] Enter email id to recover account:

enter image description here

3] After entering valid email id OTP sent to email Id: Till now it's working great.

But now I need to enter OTP in the text field:

enter image description here

Is it possible to automate this scenario.

If possible then please suggest solution.

Thanks in advance..:)

1
  • no, you can't. You need the OTP to proceed. Your best attempt could be automate the login of the email service provider, and get the OTP email from there. Commented May 16, 2017 at 5:36

2 Answers 2

1

You need the OTP to continue, so you can't skip this part. Here is what you can try

  1. Go to the email service provider
  2. Log In
  3. Check for email containing OTP
  4. Get the OTP
  5. Use the OTP in the TextField
Sign up to request clarification or add additional context in comments.

1 Comment

@Shaswat Thanks for your help. Will try this solution.
1

The alternate way i would suggest If this is your in house development then

  1. Co-ordinate with developer and add one OTP field in that form in Testing Environment

  2. You can get the OTP from the database via setting up db connection for that you need database credentials and connector file to establish connection

  3. You can read the email with the help of javax.mail libraries and extract the OTP code from email via some regex matching

Below is the sample Code for that

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


    }
}

1 Comment

Thanks Narendra. This is great solution. I will look forward to work with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.