0

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.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
User_k
  • 1
  • 1
  • 4

5 Answers5

2

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.

Nisheeth Shah
  • 600
  • 1
  • 9
  • 22
  • I even thought of suggesting the user to use the regular expression but it seems there is no simple or good regular expressions for email addresses. Refer this - http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address – Loganathan Mohanraj Aug 03 '15 at 07:37
  • Also this regular expression will return any string which contains '@' and '.' symbols. – Loganathan Mohanraj Aug 03 '15 at 07:43
  • Well, that depends on the regular expression you make for the email Id. I Just gave an example. – Nisheeth Shah Aug 03 '15 at 08:29
  • And i also found this solution i was getting the text & store thiis in a variable from where the user is entering the email id & then i am using IsEqual() to compare both. – User_k Aug 22 '15 at 05:49
0

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);
dotvav
  • 2,808
  • 15
  • 31
  • This will return the last token from the text which may not be a email address. This will work only with the given text. – Loganathan Mohanraj Aug 03 '15 at 07:32
  • I agree the question contains "text like this" which suppose it is not very accurate. I will update with finer regex pattern if the OP gives more precision. – dotvav Aug 03 '15 at 07:35
  • The text will remain same but the email id with differ according to user. – User_k Aug 03 '15 at 10:19
0

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

Community
  • 1
  • 1
Loganathan Mohanraj
  • 1,736
  • 1
  • 13
  • 22
0

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.

prab2112
  • 1,024
  • 10
  • 36
0

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??

User_k
  • 1
  • 1
  • 4