22
33import com .testsigma .sdk .AndroidAction ;
44import com .testsigma .sdk .ApplicationType ;
5- import com .testsigma .sdk .WebAction ;
5+ import com .testsigma .sdk .Result ;
66import com .testsigma .sdk .annotation .Action ;
77import com .testsigma .sdk .annotation .RunTimeData ;
88import com .testsigma .sdk .annotation .TestData ;
99import lombok .Data ;
10+ import utils .GmailUtils ;
1011
1112import javax .mail .*;
12- import java .util .Properties ;
1313
1414@ Data
1515@ Action (actionText = "Get complete email content from Gmail using EmailID and Password and store it in a runtime variable var1" ,
1616 description = "Get complete email content from Gmail using EmailID and Password and store it in a runtime variable var1" ,
1717 applicationType = ApplicationType .ANDROID )
18-
1918public class GetGmailContent extends AndroidAction {
2019
2120 @ TestData (reference = "EmailID" )
@@ -28,85 +27,56 @@ public class GetGmailContent extends AndroidAction {
2827 private com .testsigma .sdk .RunTimeData runTimeData ;
2928
3029 @ Override
31- public com .testsigma .sdk .Result execute () {
32-
33- com .testsigma .sdk .Result result = com .testsigma .sdk .Result .SUCCESS ;
30+ public Result execute () {
3431 logger .info ("Initiating execution" );
35- String host = "imap.gmail.com" ;
36- String port = "993" ;
37- String username = EmailID .getValue ().toString ();
38- String password = Password .getValue ().toString ();
32+ String username = GmailUtils .sanitizeUsername (EmailID .getValue ().toString ());
33+ String password = GmailUtils .sanitizePassword (Password .getValue ().toString ());
3934 logger .info ("username: " + username );
40- logger .info ("password: " + password );
41- Properties props = new Properties ();
42- props .setProperty ("mail.store.protocol" , "imaps" );
43- props .setProperty ("mail.imaps.host" , host );
44- props .setProperty ("mail.imaps.port" , port );
45- props .setProperty ("mail.imaps.auth" , "true" );
46- props .setProperty ("mail.imaps.starttls.enable" , "true" );
47- props .setProperty ("mail.imap.ssl.protocols" , "TLSv1.2" );
48- props .setProperty ("mail.imap.socketFactory.class" , "javax.net.ssl.SSLSocketFactory" );
4935
50- logger .info ("properties fixed" );
36+ Store store = null ;
37+ Folder inbox = null ;
5138 try {
52-
53- Session session = Session .getInstance (props , new Authenticator () {
54- protected PasswordAuthentication getPasswordAuthentication () {
55- return new PasswordAuthentication (username , password );
56- }
57- });
58- logger .info ("" );
59-
60- Store store = session .getStore ("imaps" );
61- store .connect (host , username , password );
62- logger .info ("store " );
63- Folder inbox = store .getFolder ("INBOX" );
39+ store = GmailUtils .connectToGmail (username , password , logger );
40+ inbox = store .getFolder ("INBOX" );
6441 inbox .open (Folder .READ_ONLY );
6542 Message [] messages = inbox .getMessages ();
6643 logger .info ("messages: " + messages .length );
44+
45+ if (messages .length == 0 ) {
46+ setErrorMessage ("No emails found in the INBOX for '" + username + "'. The mailbox is empty." );
47+ return Result .FAILED ;
48+ }
49+
6750 Message latestMessage = messages [messages .length - 1 ];
68- logger .info ("latestMessage" + latestMessage .getContent ());
69- Object content = latestMessage .getContent ();
70- BodyPart bodyPart = null ;
71- String FullMessage = null ;
72- if (content instanceof String ) {
73- System .out .println (content );
74- FullMessage = (String ) content ;
51+ logger .info ("latestMessage subject: " + latestMessage .getSubject ());
7552
76- } else if (content instanceof Multipart ) {
77- // content is already a Multipart object, so just cast it and process the body part
78- Multipart multipart = (Multipart ) content ;
79- bodyPart = multipart .getBodyPart (0 );
80- Object bodycontent = bodyPart .getContent ();
81- if (bodycontent instanceof String ) {
82- FullMessage = (String ) bodycontent ;
53+ String fullMessage = GmailUtils .extractContent (latestMessage .getContent ());
8354
84- } else if (bodycontent instanceof Multipart ) {
85- Multipart bodymultipart = (Multipart ) bodycontent ;
86- logger .info ("Body content" + bodymultipart .getBodyPart (0 ).getContent ());
87- logger .info ("Body content type:" + bodymultipart .getContentType ());
88- FullMessage = (String ) bodymultipart .getBodyPart (0 ).getContent ();
89- }
90- } else {
91- System .out .println ("No content" );
92- logger .info ("NO CONTENT" );
55+ if (fullMessage == null || fullMessage .trim ().isEmpty ()) {
56+ setErrorMessage ("Latest email (Subject: " + latestMessage .getSubject () +
57+ ") was found but the email body is empty or could not be read." );
58+ return Result .FAILED ;
9359 }
9460
95- logger .info (FullMessage );
61+ logger .info ("Extracted content length: " + fullMessage . length () );
9662 runTimeData = new com .testsigma .sdk .RunTimeData ();
97- runTimeData .setValue (FullMessage );
63+ runTimeData .setValue (fullMessage );
9864 runTimeData .setKey (var1 .getValue ().toString ());
99- setSuccessMessage ("Email content stored in runtime variable: " + var1 .getValue ().toString () + "and value: " + FullMessage );
100-
101- inbox .close (false );
102- store .close ();
103-
65+ setSuccessMessage ("Email content stored in runtime variable: " + var1 .getValue ().toString () +
66+ " and value: " + fullMessage );
67+ return Result .SUCCESS ;
68+
69+ } catch (AuthenticationFailedException e ) {
70+ setErrorMessage ("Gmail authentication failed for '" + username +
71+ "'. Please verify: 1) App password is valid 2) 2-Step Verification is ON 3) IMAP is enabled in Gmail settings. Error: " + e .getMessage ());
72+ logger .warn ("Authentication failed: " + e .getMessage ());
73+ return Result .FAILED ;
10474 } catch (Exception e ) {
105-
106- result = com .testsigma .sdk .Result .FAILED ;
107- setErrorMessage ("Could not retrieve the email content. The error is " + e .getMessage ());
75+ setErrorMessage ("Failed to retrieve email content. Error: " + e .getMessage ());
10876 logger .warn (e .getMessage ());
77+ return Result .FAILED ;
78+ } finally {
79+ GmailUtils .closeQuietly (inbox , store );
10980 }
110- return result ;
11181 }
112- }
82+ }
0 commit comments