TestNG also
gives us the power to take decisions in the middle of the test run with the
help of Asserts. With this we can put various checkpoints in the test.
Asserts are the most popular and frequently used methods while creating
Selenium Scripts. In selenium there will be many situations in the test
where you just like to check the presence of an element. All you need to do is
to put an assert statement on to it to verify its existence.
Different
Assert Statements
1)
Assert.assertTrue() & Assert.assertFalse()
package automationFramework;
import
java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import
org.testng.annotations.Test;
public class Asserts {
private
static WebDriver driver;
@Test
public void f() {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("url");
// Here driver will try to find out My
Account link on the application
WebElement myAccount =
driver.findElement(By.xpath(".//*[@id='account']/a"));
//Test will only continue, if the below
statement is true
//This is to check whether the link is
displayed or not
Assert.assertTrue(myAccount.isDisplayed());
//My Account will be clicked only if the
above condition is true
myAccount.click();
}
}
Note: Assert true statement fails the
test and stop the execution of the test, if the actual output is false.
Assert.assertFalse() works opposite of Assert.assertTrue(). It means that if
you want your test to continue only if when some certain element is not present
on the page. You will use Assert false, so it will fail the test in case of the
element present on the page.
2) Assert.assertEquals()
@Test
public void testCompare() {
String sValue = "Nagarjun Reddy K";
System.out.println(" What is your name?");
Assert.assertEquals("Nagarjun Reddy K",
sValue);
System.out.println(sValue);
}
It
also works the same way like assert true and assert fail. It will also stop the
execution, if the value is not equal and carry on the execution, if the value
is equal.
No comments:
Post a Comment