0

I am using TestNG and having one issue.

Test Case : I am checking validation message that if user leave email id blank & Click on Login , System should fire message "Please Enter Valid Email".

I am verifying this message using Selenium & Java.

Code Logic :

  If(Dynamicmsg.equals("Please Enter Valid Email"))   
{
       System.out.println("Email validation working");

}
else
{
       System.out.println("Email validation not working");

 }           

It works fine, When other message found, condition goes to else and print that Email validation not working.

But TestNG Report says :

Passed -1

What I want is I want to tell testNG that fail this test case in report as well if conditions do not match.

  • 1
    Throw an exception or just put this in your else statement: org.testng.Assert.fail("Email validation didn't work"); – cakes88 Jul 19 '16 at 20:51
2

You need to add assertion in TestNG to check. Your code should be as below remove whole code and just add below 1 line code.

Assert.assertEquals("Wrong error message shown", "Please Enter Valid Email", driver.findElement(By.xpath("Your path of element")).getText());

In this Assert.assertEquals is function which has 3 arguments, 1st the error message which you what to display on failure, 2nd your expected error message and 3rd is you actual error message which will be text displayed by your element.

Edit -

Just saw you already have taken text in Dynamicmsg variable, so you can rewrite above line as below

Assert.assertEquals("Wrong error message shown", "Please Enter Valid Email", Dynamicmsg);
  • Ok Test case gets failed , but getting exception also : java.lang.AssertionError: The Email field is required. expected [Please Enter Valid Email] but found [Wrong error message shown] – Helping Hands Feb 26 '16 at 8:25
  • 1
    Looks like you are using different version of TestNG, rewrite the assert statement as Assert.assertEquals("Please Enter Valid Email", Dynamicmsg); or Assert.assertEquals(Dynamicmsg, "Please Enter Valid Email", "Wrong error message shown"); – Jeevan Bhushetty Feb 26 '16 at 8:45
  • @HelpingHands Does this resolve your query? – Jeevan Bhushetty Feb 27 '16 at 6:40
  • I tried both but getting exception. Working fine but exception should not be there. Exception is : java.lang.AssertionError: Wrong Message Shown Here expected [The Email field is required] but found [The Email field is required.] – Helping Hands Feb 29 '16 at 13:06
  • I think so you are going wrong way, When you want TestNG to mark as fail there are only 2 ways 1. when exception is thrown 2. when you call Assert.fail(); you want testcase to fail and not to throw exception both won't work together , because testng throws exception when test case fails – Jeevan Bhushetty Feb 29 '16 at 13:15
0

To achieve this, you can use Assert.AssertEquals as already answered above.

Syntax is: Assert.assertEquals(actual value, Expected value, "Your debug message that you want to display in case of failure")

For you it failed because I guess there is additional character "." at the end of actual error msg. But in your expected error message "." is not there so the assertion might have failed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.