Introduction
Assertions provide a means for users to validate any kind of test. The Assertion results are based on the comparison of Actual & Expected Results. A test is considered passed only when the Assertions show no Exceptions. Thus, the usage of Assertions plays a vital role in identifying the flaws in the application being tested. This blog is intended to provide insight into the various Assert statements available in Selenium Web driver.
Assert Vs Verify Commands in Selenium
The validation checks made in Selenium usually come in two flavors: one using Assertions and the other inculcating “Verify” statements. Though both serve the same purpose there persists one major difference in their working functionality.
Verify checks get test results for multiple conditions even if one of them fails wherein Hard Assertions put a stringent restriction on the test script when it fails thereby terminating the program execution further. Although Soft Assertions work in a way different from Hard Assertion where normal flow of execution resumes although there is failure in the asserting script. Usage of either Assertions or verify statement purely falls on the users cup of tea.
Commonly used Assertions
Assert Equals
Assert Equals works by comparing the Expected condition with that of the Actual condition based on which the test results are displayed. The following example illustrates the usage of assert equals as a validation check to verify the Title of the homepage displayed for the mentioned site.
public void testCaseVerifyTitlePage() {
driver= new FirefoxDriver();
driver.navigate().to(“https://wiki.perficient.com/ /confluence/login.action?os_destination=%2Fhomepage.action “);
Assert.assertEquals(“Log In – Perficient Wiki “, driver.getTitle());
}
Assert Not Equals
“Assert Not Equals” serves the purpose of negation testing for Testers. If the Actual and Expected do not match, then the test Script passes else it fails. The following example demonstrates how a user inculcates “Assert Not Equals” for verifying the display of text that dynamically changes as per date.
public void testCaseVerifyDate () {
Actualtext = driver.findElement(By.xpath(“//h3/span”)). getText();
Assert.assertNotEquals(Actualtext, “Here comes the surprise gift for every order on Sunday, 01 January 2017”, “Expected and Actual do not match as the test is performed on December 31st,2016″);
}
This Test Case will pass as the above Scenario is tested on December 31st, 2016.
Assert True
This Assertion passes the Test Step only when the boolean value returned is “True”. The following example illustrates that the user validates the default checkbox is checked in the specified site.
public void testDefaultCheckbox() {
driver= new FirefoxDriver();
driver.get(“https://wiki.perficient.com/ /confluence/login.action?os_destination=%2Fhomepage.action “);
defaultchk1 = driver.findElement(By.name(“os_cookie”));
System.out.print(“\n”+ defaultchk1.isSelected());
Assert.assertTrue(defaultchk1.isSelected());
System.out.print(“Assertion Passed successfully “);
}
Assert False
“Assert False” passes the Test Step only when the boolean value returned is “False”. The following example illustrates that the user validates the optional checkbox is unchecked in the specified site.
public void testOptionalCheckbox() {
driver= new FirefoxDriver();
driver.get(“https://wiki.perficient.com/ /confluence/login.action?os_destination=%2Fhomepage.action “);
optionalchk = driver.findElement(By.name(“os_cookie”));
System.out.print(“\n”+ optionalchk.isSelected());
Assert.assertFalse(optionalchk.isSelected());
System.out.print(“Assertion Passed successfully “);
}
Assert Null
This Assertion verifies if the object under test is null, and the passes the same if the result is so.
Let’s consider an element that is supposed to be enabled in the application and the user verifies the same using Assert Null statement.
public void testEnabledText() {
driver.get(“https://wiki.perficient.com/ /confluence/login.action?os_destination=%2Fhomepage.action “);
enatxt = driver.findElement(By.xpath(“//input[@id=’text2′]”));
Assert.assertNull(enatxt.getAttribute(“disabled”));
System.out.print(“Assertion Passed successfully “);
}
In above example, enatxt.getAttribute(“disabled”) object will return “null” because the “enatxt” element is not a disabled attribute. So that assertion will pass.
Assert Not Null
This Assertion functions opposite to that of “Assert Null”. Thus this Assertion verifies if the object under test is not null, and the passes the same if the result is so. Let’s consider an element that is supposed to be disabled in the application and the user verifies the same using Assert Not Null statement.
public void testDisabledText() {
driver.get(“https://wiki.perficient.com/ /confluence/login.action?os_destination=%2Fhomepage.action “);
distxt = driver.findElement(By.xpath(“//input[@id=’text21′]”));
Assert.assertNull(distxt.getAttribute(“disabled”));
System.out.print(“Assertion Passed successfully “);
}
Assert Same
This Assertion checks that two objects refer to the same object, if it does, then the Assertion passes else it fails the same. The below example illustrates the usage of “Assert.assert same” for testing two objects that refer the same object:
public class AssertSameTest_Demo {
public String getTextValue(final String key){
Map<string, string=””> textProps = new HashMap<string, string=””>();
textProps.put(“text1”, “value 1”);
textProps.put(“text2”, “value 2”);
textProps.put(“text3”, “value 3”);
return textProps.get(key);
}
@Test
public void isSameTest(){
AssertSameTest_Demo abc = new AssertSameTest_Demo ();
assertSame(abc. getTextValue (“text1”), abc. getTextValue (“text1”));
}
Assert Not Same
This Assertion checks that two objects do not refer to the same object, if it does than the Assertion passes else it fails the same. The below example illustrates the usage of “Assert.assert Not same” for testing two objects that refer two different objects.
public class AssertNotSameTest_Demo {
public String getTextValue(final String key){
Map<string, string=””> textProps = new HashMap<string, string=””>();
textProps.put(“text1”, “value 1”);
textProps.put(“text2”, “value 2”);
textProps.put(“text3”, “value 3”);
return textProps.get(key);
}
@Test
public void isNotSameTest() {
AssertNotSameTest_Demo xyz = new AssertNotSameTest_Demo ();
assertNotSame(xyz. getTextValue (“text1”), xyz. getTextValue (“text3”));
}
Thanks for such a detailed information. Worth to read..