Assert
Assert
Assert
Asserts helps us to verify the conditions of the test and decide whether test has
Failed or Passed. A test is considered successfully Only if it is completed without
throwing any exception.
Assertion verify that the state of Application is same to what we are expecting.
Selenium Assertions can be of three types: “assert”, “verify” and “waitfor”.
Assert: -
Verify: -
Waitfor:
➢
➢
➢
➢
AssertEquals
Syntax: -
Assert.assertEquals(actual, expected);
Example: -
@Test
public static void assertEqualsTestng() throws Exception {
AssertNotEquals
Syntax: -
Assert.assertNotSame(actual, expected);
Example: -
@Test
public static void assertNotEqualsTestng() throws Exception {
AssertTrue
Syntax: -
Assert.assertTrue(condition);
Example: -
@Test
public static void assertTrueTestng() throws Exception {
System.setProperty("webdriver.chrome.drier", "C://chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.google.com/");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Assert.assertTrue(logo_google.isDisplayed());
driver.close();
}
AssertFalse
Syntax: -
Assert.assertFalse(condition);
Example: -
@Test
public static void assertFalseTestng() throws Exception {
int i = 5;
int j = 7;
Assert.assertFalse(i > j);
}
Code
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
@Test
public static void assertEqualsTestng() throws Exception {
@Test
public static void assertNotEqualsTestng() throws Exception {
@Test
public static void assertTrueTestng() throws Exception {
System.setProperty("webdriver.chrome.drier", "C://chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.google.com/");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
@Test
public static void assertFalseTestng() throws Exception {
int i = 5;
int j = 7;
Assert.assertFalse(i > j);
System.out.println("Assert False - Pass");
}
}