TestNG Assertion assertNotNull With WebDriver Example

As you know, there are many assertions in TestNG and you will find most of them on THIS PAGE. Each of these TestNG assertions are designed to fulfill different kind of conditions. I have described mostly used all assertions with very clear examples with selenium webdriver test. If you will see on that link, you will find example of assertNull assertion. assertNotNull assertion works opposite to assertNull assertion means it will assert not null condition.

TestNG Assertion assertNotNull(object) WebDriver Assertion
assertNotNull assertion is designed to check and verify that values returned by object is not null. Means it will be pass if returned value is not null. Else it will fail.

Best example to experiment assertNotNull assertion is assertion of enabled and disabled text fields. Let me give you simple example where we will assert "disabled" attribute of text box to verify is it disabled or not using assertNotNull assertion.

Copy paste bellow given code in example given on THIS PAGE and then run it using testng.xml file.

        WebElement txt1, txt2;
 public void load_url(){
  txt1 = driver.findElement(By.xpath("//input[@id='text1']"));
  txt2 = driver.findElement(By.xpath("//input[@id='text2']"));  
 //Example Of Assertion Method - will Fail
 public void notnull1() {
 //Example Of Assertion Method - will Pass
 public void notnull2() {

In above example, assertNotNull assertion of notnull1() method will fail because expected was not null but textbox text1 is not disabled so returned null. assertNotNull assertion of notnull2() will pass because expected was not null and textbox text2 is disabled so it has return true value means not null.

Sameway, You can use assertNotNull assertion to assert checkbox is checked or not, to assert textbox is visible or hidden, etc..

No comments:

Post a Comment