Example Of Assert.assertFalse Assertion In Selenium WebDriver With TestNG

When you are working with selenium webdriver, you must be aware about different kind of assertions which are available. If you have a enough knowledge about testng assertions, You can create very effective test cases for your test scenarios. assertEquals, assertNotEquals and assertTrue assertions are already explained in my previous posts. Now let we try to understand usage of testng assertfalse in selenium webdriver.

Assert.assertFalse(condition) Assertion

assertfalse in selenium will check boolean value returned by condition and will pass if returned value is "False". If returned value is pass then this assertion will fail and skip execution from current test method. Syntax for testng assertfalse in selenium assertion is as bellow.

In sort, assertFalse and assertTrue assertions are opposite to each other. When you wants to assert "True" value - you can use assertTrue assertion. And when you wants to assert false value - you can use assertFalse assertion in your selenium webdriver test.

Let we try to use assertFalse assertion with simple example. Supposing we have two checkboxs on page one from them is checked and another one is not checked. Let we apply assertFalse assertion on both of these check boxes for its checked status.

Replace bellow given example methods with example of THIS PAGE. and run it with testng.xml as described on that post.

TestNG Assertfalse in Selenium Example

        WebElement chk1, chk2;
 public void load_url(){
  chk1 = driver.findElement(By.xpath("//input[@name='option1']"));
  chk2 = driver.findElement(By.xpath("//input[@name='option2']"));  
 //Assertion Method - will Fail
 public void assertfalse1() {
 //Assertion Method - will Pass
 public void assertfalse2() {

In this case, assertfalse1() method will fail because it is already checked so chk1.isSelected() will return true value. assertfalse2() method will pass because 2nd checkbox is not checked so it will return false value. Execution result will looks like bellow.

1 comment: