0% found this document useful (0 votes)
109 views

Selenium WebDriver AssertTrue Assertion Example With TestNG

The document discusses the assertTrue assertion in Selenium WebDriver. It explains that assertTrue is used to verify a boolean condition is true. If the condition returns true, the assertion passes, but if it returns false, the assertion fails and the test execution stops. An example is provided to check if checkboxes are selected using assertTrue - the first test passes because the checkbox is checked, while the second fails because its checkbox is not checked.

Uploaded by

sudheer reddy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views

Selenium WebDriver AssertTrue Assertion Example With TestNG

The document discusses the assertTrue assertion in Selenium WebDriver. It explains that assertTrue is used to verify a boolean condition is true. If the condition returns true, the assertion passes, but if it returns false, the assertion fails and the test execution stops. An example is provided to check if checkboxes are selected using assertTrue - the first test passes because the checkbox is checked, while the second fails because its checkbox is not checked.

Uploaded by

sudheer reddy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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.

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.

You might also like