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

Assert

This document discusses Selenium assertions and provides examples of different assertion types in Selenium WebDriver using TestNG: Assertions help verify test conditions and determine if a test passed or failed. There are three main types of assertions: assert, verify, and waitfor. Common assertion methods include assertEquals, assertNotEquals, assertTrue, and assertFalse. Code examples are provided demonstrating how to use each assertion method to validate test conditions.

Uploaded by

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

Assert

This document discusses Selenium assertions and provides examples of different assertion types in Selenium WebDriver using TestNG: Assertions help verify test conditions and determine if a test passed or failed. There are three main types of assertions: assert, verify, and waitfor. Common assertion methods include assertEquals, assertNotEquals, assertTrue, and assertFalse. Code examples are provided demonstrating how to use each assertion method to validate test conditions.

Uploaded by

kcmt35
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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