i am using java to write Appium test script & now i want to compare two emails, but before comparison i have to fetch the email id from a text by splitting the string.
Ex: i have text like this in my application "your account email associated with pankaj#gmail.com" so i want split & capture this email id only from this text & compare it with other email id which is showing in a text box.
how can i do this ??
Currently i am doing it like this:
WebElement email_id= driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIATextField[1]"));
String edit_email=email_id.getText();
System.out.println(edit_email);
But getting the Full text.How can i split it.
You should try regular expression using java.util.regex.Pattern and java.util.regex.Matcher. I have prepared a snippet that finds email ids from the given chunk of text.
String text = "your account email associated with pankaj#gmail.com and he has emailed someone#gmail.com.";
Pattern pattern = Pattern.compile("[\\w]+[\\d\\w]*(#)[\\w]+[\\w\\d]*(\\.)[\\w]+");
Matcher matcher = pattern.matcher(text);
while(matcher.find()){
System.out.println(matcher.group());
}
This should help.
This is doing the trick for me:
String s = "your account email associated with pankaj#gmail.com";
s = s.replaceAll("^.+\\s", "");
System.out.println(s);
If you are sure that the text that you are attempting to split is of standard format (or some static content with different email Ids), you can use regular expressions to parse and retrieve the email addresses as Nitheesh Shah and dotvav mentioned in their answers.
Otherwise, you have to follow couple of RFCs as mentioned in the below thread to perfectly retrieve and validate the email addresses (Refer the best answer which is shown at the top of the below thread).
Using a regular expression to validate an email address
As OP has mentioned it will end with email id only , another solution can be this:
WebElement email_id= driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIATextField[1]"));
String s[] = email_id.getText().split(" ");
System.out.println(s[s.length-1]);
After getting email id you can compare it with another email in textbox.
Currently I am using this & it's working for me perfectly.implemented the solution as finding the particular email substring in the main string.
String word = edit_email;
String com_txt= email_text; //Edit page Static string
Boolean same_txt =com_txt.contains(word);
Boolean result=same_txt;
if(result==true)
{
System.out.println(result);
System.out.println("Edit screen & enter email screen contains the same email");
}
Is this the right way to perform the comparison??
Related
I tried to make security to display email data by replacing some words with symbol (*) but not as expected there might be an error in making the example script as below.
String email = "thismyemail#myhost.com";
String get_text = email.get_text(3, 6);
String hasil = email.replace(get_text,"*");
email_string = (EditText) findViewById(R.id.emailT);
email_string.setText(hasil);
But the result is like this
thi*email#myhost.com
Which I expect
thi***email#myhost.com
String hasil = email.replace(get_text,"***");
But please note that if that text appears anywhere else in the string it will be replaced as well.
Also, if the email is like jf#mymailserver.com you won't be replacing a part of their user id with *.
So you can probably find a better way to select the characters, taking into account email length and also not "replacing" text but rather putting those chars at the specific position you want to.
See this related question for some ideas on how to improve this:
masking of email address in java
Your code seems right. If ur expected output is like the one mentioned above, you can just add 2 more "*" to the code.
String hasil = email.replace(get_text,"***");
I hope it helps
I'm working with the ews-java-api, which I'm using to process incoming emails to specific Exchange accounts, so that I can extract out key information from the email (ie, subject, body, recipient, sender, etc) to forward on to another system through an API call. I'm able to identify the recipient of the email, because it naturally matches the account I'm retrieving new emails from, but I can't seem to identify what alias the sender may have used to send the email.
For example, if I send an email from janedoe#mycompany.com to bobsmith#mycompany.com, I can then grab an email from the "bobsmith" account, and read the subject, body, etc. But if Bob Smith has an alias of, say, "hero#mycompany.com" which goes to his bobsmith account, and Jane Doe emails him to that address, I only see "bobsmith#mycompany.com" as the recipient, not "hero...". I can't seem to find any method calls on the Exchange item (even when cast as an "EmailMessage" type, that allows me to get the address used in the "to:" field.
Does anyone know how to obtain that alias on the received message?
Okay, so, thanks to #diginoise, the resulting solution was to do the following. I didn't post code initially, but hopefully this will be helpful for anyone else searching for this same issue.
I started by using the default property set and added the mime content so that my property query would include mime content. I then add a regex to examine the mimecontent directly to get the alias that might have been used:
FindItemsResults<Item> findResults = ...; // This is several lines, but is well documented in the library
// Adding MimeContent to the set is key
PropertySet propertySet = new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.MimeContent);
service.loadPropertiesForItems(findResults, propertySet);
for (Item item : findResults) {
String messageContent = new String(((EmailMessage) item).getMimeContent().getContent());
// find the alias used
Pattern pattern = Pattern.compile("To: \"(.*)\" <(.*?)>");
Matcher matcher = pattern.matcher(messageContent);
if (matcher.find()) {
System.out.println("Alias is: " + matcher.group(1));
}
}
This works if you're only looking for the first email address listed, but won't handle a list of aliases, so you'd need to modify the Pattern and search for multiple instances on the "To:" line and extract them out, but this covers the basics of how to get the actual "sent to" address rather than the "received by" address.
I have a regular expression to validate an email and when I run it, it constantly tells me that its an invalid email address but the email is correct
email = (EditText) findViewById(R.id.email);
final String Email = email.getText().toString();
if (!Email.matches("^([a-zA-Z0-9_\\-\\.]+)#([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{2,5})$"))
{
email.requestFocus();
email.setError("INVALID EMAIL ADDRESS");
}
Does anyone know it would give me the error even though it is correct?
try this instead. this should remove any hidden spaces/characters after the email.
Email.matches
("([a-zA-Z0-9_\\-\\.]+)#([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{2,5}).*"))
according to http://developer.android.com/reference/java/lang/String.html
public boolean matches (String regularExpression)
Added in API level 1 Tests whether this string matches the given
regularExpression. This method returns true only if the regular
expression matches the entire input string. A common mistake is to
assume that this method behaves like contains(CharSequence); if you
want to match anywhere within the input string, you need to add .* to
the beginning and end of your regular expression. See matches(String,
CharSequence).
What is wrong here :
assert(Base.getdriver().findElement(By.xpath("//*[#id='mainBG']/div[1]/form[1]/div/div[1]/div/span[2]/span/span").getText().contains?("Email address is required"),"Validation message for Email is firing")));
In Eclipse , it is showing bracket missing red line at ("Email address is required")
Test Scenario :
There is login form , I am clicking on submit button without fill any data in email field and just want to verify it's validation alert message which is Email address is required.
Try to break it up like this to improve reading (note that I am making up return types)
Element element = Base.getdriver().findElement(By.xpath("//*[#id='mainBG']/div[1]/form[1]/div/div[1]/div/span[2]/span/span"));
String elemText = element.getText();
assert elemText.contains("Email address is required") : "Validation message for Email is NOT firing";
Just out of curiosity, what is that question mark after contains ?
EDIT : since it seems that the assert is a native Java assertion, try it like this.
I'd use Hamcrest assertions http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html:
assertThat(selenium.findElement(By.xpath("//*[#id='mainBG']/div[1]/form[1]/div/div[1]/div/span[2]/span/span").getText(), containsString("Email address is required" ,"Validation message for Email is firing"));
Probably you want to store the string to a variable first the print it (then delete this bit of code obviously) just to make sure you are asserting the correct data:
String emailData = selenium.findElement(By.xpath("//*[#id='mainBG']/div[1]/form[1]/div/div[1]/div/span[2]/span/span")).getText();
System.out.println("Data to be printed" +emailData);
And import:
import static org.hamcrest.CoreMatchers.*;
I would like a regular expression that will extract email addresses from a String (using Java regular expressions).
That really works.
Here's the regular expression that really works.
I've spent an hour surfing on the web and testing different approaches,
and most of them didn't work although Google top-ranked those pages.
I want to share with you a working regular expression:
[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*#[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})
Here's the original link:
http://www.mkyong.com/regular-expressions/how-to-validate-email-address-with-regular-expression/
I had to add some dashes to allow for them. So a final result in Javanese:
final String MAIL_REGEX = "([_A-Za-z0-9-]+)(\\.[_A-Za-z0-9-]+)*#[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})";
Install this regex tester plugin into eclipse, and you'd have whale of a time testing regex
http://brosinski.com/regex/.
Points to note:
In the plugin, use only one backslash for character escape. But when you transcribe the regex into a Java/C# string you would have to double them as you would be performing two escapes, first escaping the backslash from Java/C# string mechanism, and then second for the actual regex character escape mechanism.
Surround the sections of the regex whose text you wish to capture with round brackets/ellipses. Then, you could use the group functions in Java or C# regex to find out the values of those sections.
([_A-Za-z0-9-]+)(\.[_A-Za-z0-9-]+)#([A-Za-z0-9]+)(\.[A-Za-z0-9]+)
For example, using the above regex, the following string
abc.efg#asdf.cde
yields
start=0, end=16
Group(0) = abc.efg#asdf.cde
Group(1) = abc
Group(2) = .efg
Group(3) = asdf
Group(4) = .cde
Group 0 is always the capture of whole string matched.
If you do not enclose any section with ellipses, you would only be able to detect a match but not be able to capture the text.
It might be less confusing to create a few regex than one long catch-all regex, since you could programmatically test one by one, and then decide which regexes should be consolidated. Especially when you find a new email pattern that you had never considered before.
a little late but ok.
Here is what i use. Just paste it in the console of FireBug and run it. Look on the webpage for a 'Textarea' (Most likely on the bottom of the page) That will contain a , seperated list of all email address found in A tags.
var jquery = document.createElement('script');
jquery.setAttribute('src', 'http://code.jquery.com/jquery-1.10.1.min.js');
document.body.appendChild(jquery);
var list = document.createElement('textarea');
list.setAttribute('emaillist');
document.body.appendChild(list);
var lijst = "";
$("#emaillist").val("");
$("a").each(function(idx,el){
var mail = $(el).filter('[href*="#"]').attr("href");
if(mail){
lijst += mail.replace("mailto:", "")+",";
}
});
$("#emaillist").val(lijst);
The Java 's build-in email address pattern (Patterns.EMAIL_ADDRESS) works perfectly:
public static List<String> getEmails(#NonNull String input) {
List<String> emails = new ArrayList<>();
Matcher matcher = Patterns.EMAIL_ADDRESS.matcher(input);
while (matcher.find()) {
int matchStart = matcher.start(0);
int matchEnd = matcher.end(0);
emails.add(input.substring(matchStart, matchEnd));
}
return emails;
}