Example Of assertNotEquals In Selenium WebDriver With TestNG

As described in my previous post, assertEquals assertion is useful to compare two string, boolean, byte[], char, double, float, int, etc.. and based on assertion result,  Your test method will pass or fail. This pass or fail indication will helps you in your testing process. So this is the very good function in selenium WebDriver which helps us to find and log issue. Please note one thing once more is - Use hard assertions in selenium webdriver only when you wants to skip remaining test execution on assertion fail. If you wants to continue execution on assertion failure, you can use TestNg soft assertions. We will discuss about it in my upcoming post.

You can VIEW SELENIUM IDE ASSERTION EXAMPLES for your reference.

Assert.assertNotEquals(actual, expected, message) assertion
Other one assertion in selenium WebDriver is assertNotEquals assertion. It's function is opposite to assertEquals assertion. Means if both sides values will not match then this assertion will pass else it will fail. Here you can write your own message for failure condition. Simplest example of Assert.assertNotEquals in selenium webdriver is as bellow.

Replace bellow given test methods (assertion_method_1() and assertion_method_2()) with example given in my PREVIOUS POST and then run it using testng.xml file.

        //Assertion Method
 @Test
  public void assertion_method_1() {
  Actualtext = driver.findElement(By.xpath("//h2/span")).getText();
  Assert.assertNotEquals(Actualtext, "Tuesday, 28 January 2014", "Expected and actual match in assertion_method_1");
  System.out.print("\n assertion_method_1() -> Part executed");
 }
 
 //Assertion Method
 @Test
 public void assertion_method_2() {  
  Assert.assertNotEquals(Actualtext, "Tuesday, 29 January 2014", "Expected and actual match in assertion_method_2");
  System.out.print("\n assertion_method_2() -> Part executed");
     }

You test execution result will looks like bellow..



Here, test method assertion_method_1() will fail because actual string text and expected string text will match. Assertion message will be printed in test result report as marked with green color in above image.

3 comments:

  1. Great to see you have created a post on this small thing!!! Please add assignment to the tutorials

    ReplyDelete