Trying to Assert value using selenium webdriver but seems something wrong - java

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.*;

Related

Replace String with characters with substring

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

How do I identify the recipient email using ews-java-api if the address is an alias?

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.

Check for email id in a text in java

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

Selenium Java Webdriver: Asserting double quotes

A website displays the following text I need to assert:
Living Place "123" hasn't been found
I have a piece of ghurkin/cucumber on a webpage I need to assert.assertTrue using Selenum Webdriver Java:
The text "Living Place "123" hasn't been found" is present on the page
The java Code I've written for this, is as follows:
#Then("^The Text \"([^\"]*)\" isnt present on the page$")
public void not_present(String text) throws Throwable {
waitForTextInElementVisible(By.id("main-content"), text);
Assert.assertTrue(driver.findElement(By.id("main-content")).getText().contains(text));
}
The problem is, the Gherkin script can't handle the String this way, as it contains a double quote. Is there a way to assert the exact string as given above?
I am not sure if I get your problem fully.
But you can pass the string as "Living Place \"123\" hasn't been found".
[Note \ before " inside string]
You can call like follows.
not_present("Living Place \"123\" hasn't been found");
try this one #Then("^The Text \"([^\"]*)\"\d+\"([^\"]*)\" isnt present on the page$"). This step mapping will be mapped to string in feature file Then The Text "Living Place "123" hasn't been found" isnt present on the page. This substitutes the text before digits \"([^\"]*)\" i.e. 'Living Place ', this searches more then one digit symbol in quotation marks \"\d+\" i.e 123, and again part to match text.
Apparently, instead of using
\"([^\"]*)\",
I had to use \"(.*)\"
this will make the gherkin script work:
And The text "Living Place "123" hasn't been found" is present on the page

regular expression for email validation in Java

I am using the follwoing regular expression
(".+#.+\\.[a-z]+")
Bit it accepts ###.com as a valid email. What's the pattern I should use?
You should use apache-commons email validator. You can get the jar file from here.
Here is a simple example of how to use it:
import org.apache.commons.validator.routines.EmailValidator;
boolean isValidEmail = EmailValidator.getInstance().isValid(emailAddress);
Here's a web page that explains that better than I can: http://www.regular-expressions.info/email.html (EDIT: that appears to be a bit out of date since it refers to RFC 2822, which has been superseded by RFC 5322)
And another with an interesting take on the problem of validation: http://www.markussipila.info/pub/emailvalidator.php
Generally the best strategy for validating an email address is to just try sending mail to it.
If somebody wants to enter non-existent email address he'll do it whatever format validation you choose.
The only way to check that user owns email he entered is to send confirmation (or activation) link to that address and ask user to click it.
So don't try to make life of your users harder. Checking for presence of # is good enough.
[A-Z0-9._%+-]+#[A-Z0-9.-]+.[A-Z]{2,4}
I usually use the following one:
([a-zA-Z0-9]+(?:[._+-][a-zA-Z0-9]+)*)#([a-zA-Z0-9]+(?:[.-][a-zA-Z0-9]+)*[.][a-zA-Z]{2,})
import java.util.regex.*;
class ValidateEmailPhone{
public static void main(String args[]){
//phone no validation starts with 9 and of 10 digit
System.out.println(Pattern.matches("[9]{1}[0-9]{9}", "9999999999"));
//email validation
System.out.println(Pattern.matches("[a-zA-Z0-9]{1,}[#]{1}[a-z]{5,}[.]{1}+[a-z]{3}", "abcd#gmail.com"));
}
}
This is my regex for email validation:
(([a-zA-Z0-9]+)([\.\-_]?)([a-zA-Z0-9]+)([\.\-_]?)([a-zA-Z0-9]+)?)(#)([a-zA-Z]+.[A-Za-z]+\.?([a-zA-Z0-9]+)\.?([a-zA-Z0-9]+))
For username it allows ".", "_", "-" for separators.
After "#" allows only "." and "-".
Can be easy modified for more words.

Categories

Resources