Delivered by FeedBurner

Selenium WebDriver Assertion assertNull Example With TestNG

Assertions are very useful to check your expected result and skip execution if assertion fails on run time. If you are selenium IDE user, you can read all these selenium IDE ASSERTION COMMANDS examples posts to use them in your test cases for your web application. Before describing about assertNull assertion of testngI recommend you all to READ OTHER WEBDRIVER ASSERTION METHOD examples for your reference.

assertNull(object) WebDriver Assertion
assertNull(object) assertion will check and verify that object is null. It will pass if object found null. If object found not null then it will return error message like "java.lang.AssertionError: expected [null] but found [true]". Whenever your assertion fails, it will mark that specific test method as fail in TestNG result.

Let me present here one simple example of assertNull assertion. We have two text boxes. 1st text box is enabled and 2nd is disabled. Now let me use assertNull assertion in my test script to assert that textbox1 is enabled and textbox2 is disabled. Here we use textbox's disabled attribute for this assertion.

Copy and replace bellow given test methods with example given on THIS PAGE and then run it with 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 Pass
 public void null1() {
 //Example Assertion Method - will Fail
 public void null2() {

In above given example, txt1.getAttribute("disabled") object will return "null" because there is not any disabled attribute with 1st textbox. So that assertion will pass. And method null1() will pass too. txt2.getAttribute("disabled") object will return "true" because disabled attribute is available with textbox2. So in this case that assertion and method null2() will fail as shown in bellow given image.

No comments:

Post a Comment