Selenium WebDriver assertTrue assertion example with TestNG

Previously we have learnt two assertions of selenium webdriver software testing tool. You can view practical example pages of selenium webdriver assertionsassertEquals and assertNotEquals before learning asserttrue in selenium. There is also one another related assertion is assertFalse(condition) assertion but we will look about it in my next post.

assertTrue(condition) Assertion

asserttrue in selenium assertion is generally used for Boolean condition true. It will pass if condition returns "true". If it will return false then it will fail and skip software test execution from that specific method. Syntax for asserttrue testng assertion is as bellow.

Assert.assertTrue(condition);

Supposing you have one check box on page of software web application. You wants to check its status like it is checked or not. And if it is not checked then you wants to skip software test execution from that method and wants to fail that method in testng report. This thing can be done very easily using assertTrue assertion. Let we look at practical example of asserttrue testng assertion.

Below given asserttrue selenium example will help you to understand it. Replace bellow given variables and 3 methods with example given on my THIS POST and then run it using testng.xml file.

Asserttrue selenium testng 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 pass
 @Test
  public void asserttrue1() {  
  System.out.print("\n"+chk1.isSelected());
  Assert.assertTrue(chk1.isSelected());  
  System.out.print("\n asserttrue1 - > Executed - means assertion is pass");
 } 
 //Assertion Method - will fail
 @Test
  public void asserttrue2() {  
  System.out.print("\n"+chk2.isSelected());
  Assert.assertTrue(chk2.isSelected());
  System.out.print("\n asserttrue2 - > Executed - means assertion is pass");
 }

When you run above software test example in eclipse and get result, asserttrue1() method will display pass and method asserttrue2() will display fail as shown in bellow given image.


asserttrue1() will pass because 1st check box is checked on page so chk1.isSelected() will return true.
asserttrue2() will fail because 2nd check box is not checked on page so chk2.isSelected() will return false. In assertion failure case, code written after Assert.assertTrue(chk2.isSelected()); will be not executed. Run above example in your eclipse and verify the results then try it for your own project.

This way, Assert.assertTrue(condition) is helps use to assert Boolean condition true.

1 comment:

  1. Thank you for taking time and creating a page to display an example which can help in understanding. I would request if you can give some assignment at the end of each post it will be even more helpful for everyone

    ReplyDelete