Assert

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Assertion

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:

Hard Assertion is Selenium WebDriver using TestNg




AssertEquals

Syntax: -
Assert.assertEquals(actual, expected);
Example: -
@Test
public static void assertEqualsTestng() throws Exception {

String sActualValue = "Test One";


String sExpectedValue = "Test One";
Assert.assertEquals(sActualValue, sExpectedValue);
}

AssertNotEquals

Syntax: -
Assert.assertNotSame(actual, expected);

Example: -
@Test
public static void assertNotEqualsTestng() throws Exception {

String sActualValue = "Test One";


String sExpectedValue = "Test Two";
Assert.assertNotSame(sActualValue, sExpectedValue);
}

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);

WebElement logo_google = driver.findElement(By.id("hplogo"));

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;

public class AssertClass {

static WebDriver driver;

@Test
public static void assertEqualsTestng() throws Exception {

String sActualValue = "Test One";


String sExpectedValue = "Test One";
Assert.assertEquals(sActualValue, sExpectedValue);
System.out.println("Assert Equals - Pass");
}

@Test
public static void assertNotEqualsTestng() throws Exception {

String sActualValue = "Test One";


String sExpectedValue = "Test Two";
Assert.assertNotSame(sActualValue, sExpectedValue);
System.out.println("Assert Not Equals - Pass");
}

@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);

WebElement logo_google = driver.findElement(By.id("hplogo"));


Assert.assertTrue(logo_google.isDisplayed());
System.out.println("Assert True - Pass");
driver.close();
}

@Test
public static void assertFalseTestng() throws Exception {

int i = 5;
int j = 7;
Assert.assertFalse(i > j);
System.out.println("Assert False - Pass");
}
}

You might also like