Selenium WebDriver AssertTrue Assertion Example With TestNG
Selenium WebDriver AssertTrue Assertion Example With TestNG
Previously we have learnt two assertions of selenium webdriver. You can view
practical example pages of selenium webdriver assertions - assertEquals 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 test
execution from that specific method. Syntax for assertTrue assertion is as
bellow.
Assert.assertTrue(condition);
Supposing you have one check box on page. You wants to check its status
like it is checked or not. And if it is not checked then you wants to skip
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 example in eclipse and get result, asserttrue1() method
will display pass and method asserttrue2() will display fail as shown in bellow
given image.