Table of Contents
Handling Assertions in Selenium WebDriver
The different types of assertions that can be used with the
Assert Equals
Assert equals compares the expected and actual conditions based on the test results. Following are the different syntaxes of using assertEquals statements in TestNG.
assert.assertEquals(string actual, string expected);
assert.assertEquals(string actual, string expected, string message);
assert.assertEquals(boolean actual, boolean expected);
Following is an example of the use of assert equals which verifies the title of the page.
package testngpackage;
import org.testng.annotations.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
public class TestNGTest {
public String testurl="https://www.siteforinfotech.com/";
String driverPath="E:\\geckodriver.exe";
public WebDriver driver;
@BeforeTest
public void beforeTest() {
System.out.println("Browser was opening now");
System.setProperty("webdriver.gecko.driver", driverPath);
driver= new FirefoxDriver();
driver.get(testurl);
}
@Test
public void PrintTitle() {
String Title=driver.getTitle();
System.out.println(Title);
}
@Test
public void VerifyTitle() {
String ExpectedTitle="InfoTechSite | CS/IT Tutorials, MCQs, Guides And Reviews";
String ActualTitle=driver.getTitle();
Assert.assertEquals(ActualTitle, ExpectedTitle);
}
@AfterTest
public void afterTest() {
driver.quit();
}
}
Assert Not Equal
The assertion command “Assert Not Equal” is used for negation testing by the testers. The test script will passes if the actual and expected result, not matches. Here is the sample code which shows the uses of assert.NotEquals command for verifying the title of the page.
@Test
public void VerifyTitle() {
String ExpectedTitle="InfoTechSite";
String ActualTitle=driver.getTitle();
Assert.assertNotEquals(ActualTitle, ExpectedTitle);
Assert True
The assert true assertion verifies whether the given statement returns the Boolean value True and passes the test step. The assertTrue method accepts two types of syntax, which are as follows.
assert.assertTrue(<condition>);
assert.assertTrue(<condition>, message);
package testngpackage;
import org.testng.Assert;
import org.testng.annotations.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TestNG2 {
public String testurl="http://localhost/test/sample-register.html";
String driverPath="E:\\geckodriver.exe";
public WebDriver driver;
@BeforeTest
public void beforeTest() {
System.out.println("Browser was opening now");
System.setProperty("webdriver.gecko.driver", driverPath);
driver= new FirefoxDriver();
driver.get(testurl);
}
@Test
public void submitform() {
WebElement agreeChk=driver.findElement(By.id("agree"));
agreeChk.click();
Assert.assertTrue(agreeChk.isSelected());
driver.findElement(By.name("submit")).click();
}
@AfterTest
public void afterTest() {
driver.quit();
}
}
Assert False
It was the negation command of the assert True command. When the test step returned the Boolean value “False”, assert.AssertFalse() method will pass the test step. The assertFalse method will also accept two types of syntax, which are as follows.
assert.assertFalse(<condition>);
assert.assertFalse(<condition>, message);
Following is a sample program code that shows the uses of the “Assert False” command for validating whether the check box is selected or not.
@Test
public void submitform() {
WebElement agreeChk=driver.findElement(By.id("agree"));
Assert.assertFalse(agreeChk.isSelected());
agreeChk.click();
driver.findElement(By.name("submit")).click();
}
Assert Null
The assert null assertion will verify the test and passes the object under test as null. The following example illustrates that the user verifies whether the email field has a “disabled” attribute. It will return true since there is no attribute named “disabled” on the
package testngpackage;
import org.testng.Assert;
import org.testng.annotations.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TestNG3 {
public String testurl="http://localhost/test/sample-register.html";
String driverPath="E:\\geckodriver.exe";
public WebDriver driver;
@BeforeTest
public void beforeTest() {
System.out.println("Browser was opening now");
System.setProperty("webdriver.gecko.driver", driverPath);
driver= new FirefoxDriver();
driver.get(testurl);
}
@Test
public void enabled() {
WebElement Email=driver.findElement(By.id("email"));Assert.assertNull(Email.getAttribute("disabled"));
System.out.println("Email was enabled");
}
@AfterTest
public void afterTest() {
driver.quit();
}
}
Assert Not Null
@Test
public void disabled() {
WebElement Address=driver.findElement(By.id("address"));Assert.assertNotNull(Address.getAttribute("disabled"));
System.out.println("Address was disabled");
}
Comments are closed.