Skip to content

Commit dbc2693

Browse files
[CUS-11560] added filters for gmail api addon.
1 parent 5e7c165 commit dbc2693

11 files changed

Lines changed: 917 additions & 201 deletions

gmail_api/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<modelVersion>4.0.0</modelVersion>
77
<groupId>com.testsigma.addons</groupId>
88
<artifactId>gmailapi</artifactId>
9-
<version>1.1.7</version>
9+
<version>1.1.8</version>
1010
<packaging>jar</packaging>
1111

1212
<properties>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.android;
2+
3+
import com.testsigma.sdk.AndroidAction;
4+
import com.testsigma.sdk.ApplicationType;
5+
import com.testsigma.sdk.Result;
6+
import com.testsigma.sdk.annotation.Action;
7+
import com.testsigma.sdk.annotation.RunTimeData;
8+
import com.testsigma.sdk.annotation.TestData;
9+
import lombok.Data;
10+
import utils.GmailUtils;
11+
12+
import javax.mail.*;
13+
import javax.mail.search.SearchTerm;
14+
15+
@Data
16+
@Action(actionText = "Get email content from Gmail using EmailID and Password " +
17+
"filtered by filter-criteria with value filter-value and store it in a runtime variable var1",
18+
description = "Get email content from Gmail filtered by From address, To address, or Subject " +
19+
"and store it in a runtime variable",
20+
applicationType = ApplicationType.ANDROID)
21+
public class GetFilteredGmailContent extends AndroidAction {
22+
23+
@TestData(reference = "EmailID")
24+
private com.testsigma.sdk.TestData EmailID;
25+
26+
@TestData(reference = "Password")
27+
private com.testsigma.sdk.TestData Password;
28+
29+
@TestData(reference = "filter-criteria",
30+
allowedValues = {"From", "To", "Subject"})
31+
private com.testsigma.sdk.TestData filterCriteria;
32+
33+
@TestData(reference = "filter-value")
34+
private com.testsigma.sdk.TestData filterValue;
35+
36+
@TestData(reference = "var1", isRuntimeVariable = true)
37+
private com.testsigma.sdk.TestData var1;
38+
39+
@RunTimeData
40+
private com.testsigma.sdk.RunTimeData runTimeData;
41+
42+
@Override
43+
public Result execute() {
44+
logger.info("Initiating execution");
45+
String username = GmailUtils.sanitizeUsername(EmailID.getValue().toString());
46+
String password = GmailUtils.sanitizePassword(Password.getValue().toString());
47+
String criteria = filterCriteria.getValue().toString().trim();
48+
String value = filterValue.getValue().toString().trim();
49+
50+
logger.info("username: " + username);
51+
logger.info("Filter criteria: " + criteria + ", value: " + value);
52+
53+
Store store = null;
54+
Folder inbox = null;
55+
try {
56+
store = GmailUtils.connectToGmail(username, password, logger);
57+
inbox = store.getFolder("INBOX");
58+
inbox.open(Folder.READ_ONLY);
59+
60+
SearchTerm searchTerm = GmailUtils.buildSearchTerm(criteria, value);
61+
Message[] messages = inbox.search(searchTerm);
62+
logger.info("Found " + messages.length + " messages matching filter: " + criteria + " = " + value);
63+
64+
if (messages.length == 0) {
65+
setErrorMessage("No emails found in INBOX matching " + criteria + ": '" + value +
66+
"'. Please verify the filter criteria and value are correct.");
67+
return Result.FAILED;
68+
}
69+
70+
Message latestMessage = messages[messages.length - 1];
71+
logger.info("Subject: " + latestMessage.getSubject());
72+
73+
String fullMessage = GmailUtils.extractContent(latestMessage.getContent());
74+
75+
if (fullMessage == null || fullMessage.trim().isEmpty()) {
76+
setErrorMessage("Email matching " + criteria + ": '" + value +
77+
"' was found (Subject: " + latestMessage.getSubject() +
78+
") but the email body is empty or could not be read.");
79+
return Result.FAILED;
80+
}
81+
82+
logger.info("Extracted content length: " + fullMessage.length());
83+
runTimeData = new com.testsigma.sdk.RunTimeData();
84+
runTimeData.setValue(fullMessage);
85+
runTimeData.setKey(var1.getValue().toString());
86+
setSuccessMessage("Email content stored in runtime variable: " + var1.getValue().toString() +
87+
" (filtered by " + criteria + ": " + value + ")");
88+
return Result.SUCCESS;
89+
90+
} catch (AuthenticationFailedException e) {
91+
setErrorMessage("Gmail authentication failed for '" + username +
92+
"'. Please verify: 1) App password is valid 2) 2-Step Verification is ON 3) IMAP is enabled in Gmail settings. Error: " + e.getMessage());
93+
logger.warn("Authentication failed: " + e.getMessage());
94+
return Result.FAILED;
95+
} catch (IllegalArgumentException e) {
96+
setErrorMessage(e.getMessage());
97+
logger.warn(e.getMessage());
98+
return Result.FAILED;
99+
} catch (Exception e) {
100+
setErrorMessage("Failed to retrieve email content. Error: " + e.getMessage());
101+
logger.warn(e.getMessage());
102+
return Result.FAILED;
103+
} finally {
104+
GmailUtils.closeQuietly(inbox, store);
105+
}
106+
}
107+
}

gmail_api/src/main/java/com/android/GetGmailContent.java

Lines changed: 37 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@
22

33
import com.testsigma.sdk.AndroidAction;
44
import com.testsigma.sdk.ApplicationType;
5-
import com.testsigma.sdk.WebAction;
5+
import com.testsigma.sdk.Result;
66
import com.testsigma.sdk.annotation.Action;
77
import com.testsigma.sdk.annotation.RunTimeData;
88
import com.testsigma.sdk.annotation.TestData;
99
import lombok.Data;
10+
import utils.GmailUtils;
1011

1112
import 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-
1918
public 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+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package com.android;
2+
3+
import com.testsigma.sdk.AndroidAction;
4+
import com.testsigma.sdk.ApplicationType;
5+
import com.testsigma.sdk.Result;
6+
import com.testsigma.sdk.annotation.Action;
7+
import com.testsigma.sdk.annotation.RunTimeData;
8+
import com.testsigma.sdk.annotation.TestData;
9+
import lombok.Data;
10+
import utils.GmailUtils;
11+
12+
import javax.mail.*;
13+
import javax.mail.search.SearchTerm;
14+
import java.util.regex.Matcher;
15+
import java.util.regex.Pattern;
16+
17+
@Data
18+
@Action(actionText = "Get email content from Gmail using EmailID and Password " +
19+
"filtered by filter-criteria with value filter-value " +
20+
"and extract matching content using regex-pattern and store it in a runtime variable var1",
21+
description = "Get email content from Gmail filtered by From address, To address, or Subject, " +
22+
"then extract the portion matching the given regex pattern and store it in a runtime variable",
23+
applicationType = ApplicationType.ANDROID)
24+
public class GetGmailContentByRegex extends AndroidAction {
25+
26+
@TestData(reference = "EmailID")
27+
private com.testsigma.sdk.TestData EmailID;
28+
29+
@TestData(reference = "Password")
30+
private com.testsigma.sdk.TestData Password;
31+
32+
@TestData(reference = "filter-criteria",
33+
allowedValues = {"From", "To", "Subject"})
34+
private com.testsigma.sdk.TestData filterCriteria;
35+
36+
@TestData(reference = "filter-value")
37+
private com.testsigma.sdk.TestData filterValue;
38+
39+
@TestData(reference = "regex-pattern")
40+
private com.testsigma.sdk.TestData regexPattern;
41+
42+
@TestData(reference = "var1", isRuntimeVariable = true)
43+
private com.testsigma.sdk.TestData var1;
44+
45+
@RunTimeData
46+
private com.testsigma.sdk.RunTimeData runTimeData;
47+
48+
@Override
49+
public Result execute() {
50+
logger.info("Initiating execution");
51+
String username = GmailUtils.sanitizeUsername(EmailID.getValue().toString());
52+
String password = GmailUtils.sanitizePassword(Password.getValue().toString());
53+
String criteria = filterCriteria.getValue().toString().trim();
54+
String value = filterValue.getValue().toString().trim();
55+
String regex = regexPattern.getValue().toString();
56+
57+
logger.info("username: " + username);
58+
logger.info("Filter criteria: " + criteria + ", value: " + value);
59+
logger.info("Regex pattern: " + regex);
60+
61+
Store store = null;
62+
Folder inbox = null;
63+
try {
64+
store = GmailUtils.connectToGmail(username, password, logger);
65+
inbox = store.getFolder("INBOX");
66+
inbox.open(Folder.READ_ONLY);
67+
68+
SearchTerm searchTerm = GmailUtils.buildSearchTerm(criteria, value);
69+
Message[] messages = inbox.search(searchTerm);
70+
logger.info("Found " + messages.length + " messages matching filter: " + criteria + " = " + value);
71+
72+
if (messages.length == 0) {
73+
setErrorMessage("No emails found in INBOX matching " + criteria + ": '" + value +
74+
"'. Please verify the filter criteria and value are correct.");
75+
return Result.FAILED;
76+
}
77+
78+
Message latestMessage = messages[messages.length - 1];
79+
logger.info("Subject: " + latestMessage.getSubject());
80+
81+
String fullMessage = GmailUtils.extractContent(latestMessage.getContent());
82+
83+
if (fullMessage == null || fullMessage.trim().isEmpty()) {
84+
setErrorMessage("Email matching " + criteria + ": '" + value +
85+
"' was found (Subject: " + latestMessage.getSubject() +
86+
") but the email body is empty or could not be read.");
87+
return Result.FAILED;
88+
}
89+
90+
logger.info("Extracted content length: " + fullMessage.length());
91+
92+
Pattern pattern = Pattern.compile(regex);
93+
Matcher matcher = pattern.matcher(fullMessage);
94+
95+
if (matcher.find()) {
96+
String matchedContent = matcher.group(0);
97+
logger.info("Regex matched: " + matchedContent);
98+
99+
runTimeData = new com.testsigma.sdk.RunTimeData();
100+
runTimeData.setValue(matchedContent);
101+
runTimeData.setKey(var1.getValue().toString());
102+
setSuccessMessage("Matched content stored in variable: " + var1.getValue().toString() +
103+
", value: " + matchedContent);
104+
return Result.SUCCESS;
105+
} else {
106+
setErrorMessage("Email found (Subject: " + latestMessage.getSubject() +
107+
") but no content matching regex '" + regex +
108+
"' was found in the email body. Body length: " + fullMessage.length() + " chars.");
109+
return Result.FAILED;
110+
}
111+
112+
} catch (java.util.regex.PatternSyntaxException e) {
113+
setErrorMessage("Invalid regex pattern '" + regex + "': " + e.getMessage());
114+
logger.warn("Invalid regex: " + e.getMessage());
115+
return Result.FAILED;
116+
} catch (AuthenticationFailedException e) {
117+
setErrorMessage("Gmail authentication failed for '" + username +
118+
"'. Please verify: 1) App password is valid 2) 2-Step Verification is ON 3) IMAP is enabled in Gmail settings. Error: " + e.getMessage());
119+
logger.warn("Authentication failed: " + e.getMessage());
120+
return Result.FAILED;
121+
} catch (IllegalArgumentException e) {
122+
setErrorMessage(e.getMessage());
123+
logger.warn(e.getMessage());
124+
return Result.FAILED;
125+
} catch (Exception e) {
126+
setErrorMessage("Failed to retrieve email content. Error: " + e.getMessage());
127+
logger.warn(e.getMessage());
128+
return Result.FAILED;
129+
} finally {
130+
GmailUtils.closeQuietly(inbox, store);
131+
}
132+
}
133+
}

0 commit comments

Comments
 (0)