SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

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(condition) assertion. 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 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 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 assertion.

Replace bellow given variables and 3 methods with example given on my THIS POST and then run it using testng.xml file.

        WebElement chk1, chk2;
 @BeforeClass
 public void load_url(){
  driver.get("http://only-testing-blog.blogspot.in/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.

No comments:

Post a Comment