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.
Assert.assertFalse(condition);

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;
 @BeforeClass
 public void load_url(){
  driver.get("http://only-testing-blog.blogspot.com/2014/02/attributes.html");
  chk1 = driver.findElement(By.xpath("//input[@name='option1']"));
  chk2 = driver.findElement(By.xpath("//input[@name='option2']"));  
 } 
 //Assertion Method - will Fail
 @Test
 public void assertfalse1() {
  System.out.print("\n"+chk1.isSelected());
  Assert.assertFalse(chk1.isSelected());
 }
 //Assertion Method - will Pass
 @Test
 public void assertfalse2() {
  System.out.print("\n"+chk1.isSelected());
  Assert.assertFalse(chk2.isSelected());
 }

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: