Selenium Assertion Examples – Practical Applications In Projects

In this Tutorial, we will Discuss How to use Assertions in Various Real-Time Project Scenarios:

  1. To verify if an object is visible (Button, link, alert).
  2. To verify if a text or value is present.
  3. To verify if a checkbox or radio button is selected/deselected.
  4. To verify if an object is editable.

=> Check Out The Perfect Selenium Training Guide Here.

Assertion Real Time Example


Assertions Usage – A Video Tutorial

Assertions Practical Examples

We would handle these scenarios using functions like:

  • isDisplayed() – This is used to verify the presence of an element in the web page. It returns a true value if the element is present.
  • isSelected()This method determines if an element is selected or not. It returns true if the element is selected and false if it is not.  It is widely used on checkboxes, radio buttons, and options in a select.
  • isEnabled() – This is used to verify if a particular text box is editable or available for the user to interact with. It returns true if a user is able to enter data in it, else if it is disabled, it returns a false value.

Click here for a sample test cases for the irctc website.

#1) To verify if an object is visible :

In order to verify the presence of an object, we can use isDisplayed() to determine its state.

@Test
public void verifyLogo()
{
WebDriver driver = new FirefoxDriver();
driver.get(“https://www.irctc.co.in”);
WebElement IrctcLogo = driver.findElement(By.Id(“irctclogo”));
Assert.assertEquals(true, IrctcLogo.isDisplayed());
System.out.println(“IRCTC logo is displayed – Assert passed”);
}

Here, IrctcLogo is the WebElement and it asserts if that element is displayed.
AssertEquals verifies for both expected and actual value to be equal. In this case, it is the Boolean value (true).

Expected value: true
Actual value: IrctcLogo.isDisplayed() – returns T/F depending on its availability on the web page.

The same can be verified using AssertTrue(IrctcLogo.isDisplayed());
Here, it asserts for a true boolean condition, that is, IrctcLogo.isDisplayed() to be True.

#2) To verify if text or value is present :

In order to verify the presence of a certain text/value, we can get the text of an element from the HTML source code using the getText() method and compare it with the expected string.

@Test
public void verifyLogo()
{
WebDriver driver = new FirefoxDriver();
driver.get(“https://www.irctc.co.in”);

WebElement IrctcLogo = driver.findElement(By.Id(“irctclogo”));
String ExpectedText = “IRCTC railways”;
Assert.assertEquals(ExpectedText, IrctcLogo.getText());
System.out.println(“IRCTC text is a expected – Assert passed”);
}

Here, IrctcLogo is the web element and it asserts if those elements hold a text displayed as “IRCTC railways”.
AssertEquals verifies for both expected and actual value to be equal. In this case, it is a string value (IRCTC railways)

Expected value: IRCTC railways
Actual value: IrctcLogo.getText()– returns the text of the element – IrctcLogo from the web page

#3) To verify if checkbox or radio button is selected/deselected

To verify the status of the checkbox, we can use isSelected() to understand its state.

Sample HTML Page: (sample.html)

<html> 
<head> 
<title>Sample Page</title> 
</head> 
<body> 
<form name="Sampleform" action="http://www.checkdomain.com/Sampleform.cgi" method="POST"> 
<div align="center"><br> 
<input type="radio" id=”Milk” value="Milk"> Milk<br> 
<input type="radio" id=”Butter” value="Butter"> Butter<br> 
<input type="radio" id=”Cheese” value="Cheese"> Cheese 
<hr> 
<input type="radio" id=”Water” value="Water"> Water<br> 
<input type="radio" id=”Juice” value="Juice"> Juice<br> 
<input type="radio" id=”Ice” value="Ice"> Ice<br> 
</div> 
</form> 
</body> 
</html>

To verify if checkbox or radio button is selected or deselected

Radio Button – Selected:

@Test
public void verifyElementSelection()
{
WebDriver driver = new FirefoxDriver();
driver.get("C:\\Users\\UserName\\Desktop\\sample.html");
WebElement Butter = driver.findElement(By.Id(“Butter”));
Butter.click(); //clicks on the radio button of Butter
Assert.assertEquals(true, Butter.isSelected()); //Verifies that the radio 
button is selected after action
System.out.println(“Radio button is selected – Assert passed”);
}

Here, Butter refers to a radio button WebElement and it asserts if the element is selected. AssertEquals verifies for both expected and actual value to be equal. In this case, it asserts for a Boolean value (true)

Expected value: true
Actual value: Butter.isSelected () – returns true/false depending on whether the radio button is selected or not.

The same can be verified using AssertTrue(Butter.isSelected());
Here, it asserts for a true boolean condition, that is, Butter.isSelected() to be True.

Radio Button – UnSelected:

@Test
public void verifyElementSelection()
{
WebDriver driver = new FirefoxDriver();
driver.get("C:\\Users\\UserName\\Desktop\\sample.html");
WebElement Milk = driver.findElement(By.Id(“Milk”));

//Verifies that the radio button is not selected 
Assert.assertEquals(false, Milk.isSelected()); 

System.out.println(“Radio button is not selected – Assert passed”);
}

Assert.assertEquals(false, Milk.isSelected()); In contrast, to assert if the radio button is not selected, we can use assert for a False return value.

The same can be verified using Assert.assertEquals(false, Milk.isSelected());
Here, it asserts for a false boolean condition, that is, Milk.isSelected() to be false.

The same can be used to verify checkboxes also.

Checkbox – Selected:

Checkbox - Selected

@Test
public void verifyElementSelection()
{
WebDriver driver = new FirefoxDriver();
driver.get("C:\\Users\\UserName\\Desktop\\sample.html");
WebElement Juice = driver.findElement(By.Id(“Juice”));

Juice.click(); //clicks on the radio button of Juice
Assert.assertEquals(true, Juice.isSelected()); //Verifies that the checkbox is 
selected after action
System.out.println(“Checkbox is selected – Assert passed”);

}

Here, Juice to the checkbox WebElement and it asserts if the element is selected.
AssertEquals verifies for both expected and actual value to be equal. In this case, it asserts for a Boolean value (true).

Expected value: true
Actual value: Juice.isSelected()– returns true / false depending on whether the radio button is selected or not.

The same can be verified using Assert.assertEquals(true, Juice.isSelected())
Here, it asserts for a true boolean condition, that is, Juice.isSelected()to be True.

CheckBox – UnSelected:

@Test
public void verifyElementSelection()
{
WebDriver driver = new FirefoxDriver();
driver.get("C:\\Users\\UserName\\Desktop\\sample.html");
WebElement Water = driver.findElement(By.Id(“Water”));

Water.click(); //clicks on the radio button of Juice
Assert.assertEquals(true, Water.isSelected()); //Verifies that the checkbox is 
selected after action
System.out.println(“Checkbox is selected – Assert passed”);

}

Assert.assertEquals(false, Water.isSelected()); In contrast, to assert if the checkbox is not selected, we can use assert for a False return value.

The same can be verified using Assert.assertEquals(false, Water.isSelected());
Here, it asserts for a false boolean condition, that is, Water.isSelected() to be false.

#4) To verify if the object is editable

This is used to verify if an element is available to interact with. In order to verify, we can use isEnabled() method. This method can be used for any WebElement like textbox, radio button, button, dropdown, etc.

Sample Page: (login.html)

<head>
<body>
   <div class="container">
        <label for="uname"><b>Username</b></label>
        <input type="text" disabled="true" placeholder="Enter Username" name="uname" required>
        <label for="passwordtext"><b>Password</b></label>
        <input type="password" placeholder="Enter Password" name="passwordtext" required>
        <button type="submit">Login</button>
        <label>
             <input type="checkbox" checked="checked" name="rememberMe"> Remember me
        </label>
    </div>
    <div class="container">
         <button type="button" class="cancelbtn">Cancel</button>
         <span class="passwordtext">Forgot <a href="#">password?</a></span>
     </div>
</body>
</head>

To verify if object is editable

Verify if Element is Enabled :

@Test
public void verifyElementEditable()
{
WebDriver driver = new FirefoxDriver();
driver.get("C:\\Users\\UserName\\Desktop\\login.html");
WebElement Password = driver.findElement(By.Name(“passwordtext”));

Assert.assertEquals(true, Username.isEnabled()); //Verifies that the textbox 
elementis enabled
System.out.println(“Element is enabled – Assert passed”);

}

Here, the Password is the web element of a textbox and it asserts if that element is not disabled.
AssertEquals verifies for both expected and actual value to be equal. In this case, it is the Boolean value (true)

Expected value: true
Actual value: Username.isEnabled()– returns T/F depending on whether the textbox is available for editing/typing in.

The same can be verified using Assert.assertEquals(true, Username.isEnabled())
Here, it asserts for a true boolean condition, that is Username.isEnabled()to be True.

Verify if Element is Disabled :

@Test
public void verifyElementEditable()
{
WebDriver driver = new FirefoxDriver();
driver.get("C:\\Users\\UserName\\Desktop\\login.html");
WebElement RememberMe = driver.findElement(By.Name(“rememberMe”));

Assert.assertEquals(false, RememberMe.isEnabled()); //Verifies that element is
 disabled
System.out.println(“Element is Disbaled – Assert passed”);

}

Here, RememberMe is the web element of a textbox and it asserts if that element is not disabled.
AssertEquals verifies for both expected and actual value to be equal. In this case, it is the Boolean value (false).

Expected value: false
Actual value: RememberMe.isEnabled()– returns T/F depending on whether the element is available for interaction/enabled.

The same can be verified using Assert.assertEquals(false, RememberMe.isEnabled()).
Here, it asserts for a false boolean condition, that is RememberMe.isEnabled()to be False.

Verify In Scripts

Verify is a class, used for comparing expected and actual test results as assert class does, but when it fails it will not stop the test execution, it continues to run the test cases. In Selenium Web Driver it is achieved using Soft Assertion.

Verify Elements In Selenium Web Driver

1. Few Verify commands available in Selenium IDE and in Selenium RC are

  • verifyTextPresent / verifyTextNotPresent
  • verifyElementPresent / verifyElementNotPresent

2. So in Selenium Web Driver, if we want to verify the results without script getting terminated use Soft Assertion. Or

3. We can use Selenium Web Driver commands. Refer below code which will check

  • Weather text is present or not
  • Weather element is present or not
package com.wordpress.pages;

import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Web Driver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class LearnAssertion {

static Web Driver driver;

@Test
public void Test(){
System.setProperty("Web Driver.chrome.driver", "D:\\New folder\\exe\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.ksrtc.in/oprs-web/");

//Verify Title of the Site
String Expected_Title ="KSRTC Official Website for Online Bus Ticket Booking - KSRTC.in";
String Actual_Title = driver.getTitle();
Assert.assertEquals(Expected_Title, Actual_Title);
//Verify Text or the element "Home" "PNR Enquiry" "Cancel Tickets" "KSTDC Package Tours" "About Us" 
//"Contact US" is present of the Site

List<WebElement> Tab = driver.findElements(By.xpath("//div[@class='menu']//a"));

for(int i=1;i<=Tab.size();i++){

WebElementTab_Name=driver.findElement(By.xpath("(//div[@class='menu']//a)["+i+"]"));

Tab_Name.isDisplayed();
String Tab_Value = Tab_Name.getText();
System.out.println(Tab_Value);

if(Tab_Value.equalsIgnoreCase("HOME")){
System.out.println("HOME tab is present");
break;
}
else
{System.out.println("HOME tab is not present"); 
}
} 
}}

 Selenium Web Driver commands

When To Use Asset Command / Verify Commands

1. Use Assert commands to make sure your test case is going in correct direction means right user configuration is loaded or right page is loaded or right environment is loaded and so on.

2. Use Verify commands to compare test results with the output values like logged in to the wrong page and whatever verification you do it will fail

3. Logically group your test commands and start each group with “assert” and “verify” the contents

Conclusion

As discussed above, we have seen various practical applications of Assertion methods which can be used for the test case validation. Thus, Assertions help us with a test case verification and ensure whether the test has a Pass or Fail status.

=> Check Here To See A-Z Of Selenium Training Tutorials Here.