Helping Hands
Helping Hands

Reputation: 5396

Trying to Assert value using selenium webdriver but seems something wrong

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.

Upvotes: 0

Views: 584

Answers (2)

frass
frass

Reputation: 125

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

Upvotes: 0

francesco foresti
francesco foresti

Reputation: 2043

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.

Upvotes: 1

Related Questions