Monday 1 December 2014

Hard Assertion - assertTrue(condition) Assertion in Selenium Webdriver



-> 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 test execution from that specific method.

-> Syntax for assertTrue assertion is as bellow.

Assert.assertTrue(condition);


Example:

Suppose you want to continue the process if checkbox Selected/Checked.



Example :

package TestNG_Package;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.testng.Assert;

public class TestNG_Test1 {

WebDriver driver1=new FirefoxDriver();
WebElement check1;

@BeforeMethod
public void OpenBrowser(){
driver1.get("http://google.com");
check1 = driver1.findElement(By.xpath("//input[@name='option1']"));
driver1.manage().window().maximize();
}

@AfterMethod
public void CloseBrowser(){
driver1.quit();
}


@Test
public void Assertion_method1() {
String title = driver1.getTitle();
Assert.assertTrue(check1.isSelected());
System.out.println("title"+title);
}


}


From above Example:

-> Assertion_method1 will get pass and complete all steps if select the check box and return the condition value as True .

-> Assertion_method1 will get fail and skip all steps if unselect the check box and return the condition value as False .

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

No comments:

Post a Comment