Best way to handle error message in Selenium WebDriver

Today we are going to discuss the scenario to handle error message in Selenium. Defect, Bug, Error– All these are synonyms to each other and refer to the same meaning, that is, the un-expected working of the system. Software testing is all about finding a defect (bug) and reporting to the respective developer to make it fixed based on its priority and severity level.

A defect (bug) only occurs when there is a mismatch between the Actual result and the Expected result. In case of manual testing or Functional testing, we follow comparison technique to mark any bug a defect. Hence, we will follow the same principle of comparison between the expected and the actual test result to align the scenario with a defect or not a defect.

The technique will be the same everywhere; we only have to write the manual technique in the programming language. So in Selenium with Java, we use any of the comparison operators or methods or statements to identify any mismatches between actual and expected test results. Therefore, based on the observation we can print pass or fail in test report as well as we can send log which will be printed in the console.

What are the available logics to handle error message in Selenium?

We have three techniques that we can use to compare the expected test result and actual test result. It depends on the conscience of the Automation engineer to use the technique. So here are those three techniques:

  • Comparison using Ternary operator
  • Comparison using if-else statement
  • Comparison using Java Assertion or Selenium Assertion

You can use which among the above looks perfect for your business scenario. I will explain each one of them separately so that you don’t have any difficulty in making your choice. Hence, let’s start with the ternary operator.

Handle error message in Selenium

 

Comparison using Ternary operator

Ternary operator has three statements, like-

(conditions) ? “if the condition is true then execute this code” : “if the condition is false then execute this” ;

This is the sample statement of Ternary operator. At the place of conditions, you can mention the comparison between actual and expected. Following sample code is the best example of uses of the ternary operator. Here we are using getTitle() method to store the title of the web page, further, we placed hardcoded expected title in the string. We are comparing both the strings and putting them in Ternary operator. See the code snippet below:

package SeleniumTester;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class ErrorMessageDemo {
  @Test
  public void errorMessageDemo() {
	  
	  System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
	  
	  WebDriver driver = new ChromeDriver();
	 
	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	  
	  driver.get("http://www.inviul.com");
	  
	  driver.manage().window().maximize();
	  
          //Expected Title
	  String expectedTitle = "Free Selenium Tutorials by Avinash Mishrda";
	  //Actual Title
	  String actualTitle = driver.getTitle();

	  //Conditions
	  boolean t = (actualTitle.equals(expectedTitle));

	  //Ternary Operator Definition
	  String result = (t==true) ? "Title Matched" :  "Title didn't match";
	  
	  System.out.println("Result is- "+result);
	  
	  driver.close();
	  
	  driver.quit();
  }
}

 

Comparison using if-else statement

If-Else statement is the further expansion of the Ternary operator. If you are not comfortable with ternary operator then you can simply use if-else statement. So within ‘If’ statement the conditional logic for comparison will be written. If this logic is true then statements written inside if statement will be executed, otherwise, else statement will be executed. This is how you can do a comparison between actual and expected results. See code snippet below:

package SeleniumTester;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class ErrorMessageDemo {
  @Test
  public void errorMessageDemo() {
	  
	  System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
	  
	  WebDriver driver = new ChromeDriver();
	 
	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	  
	  driver.get("http://www.inviul.com");
	  
	  driver.manage().window().maximize();
	  
	  //Expected Title
	  String expectedTitle = "Free Selenium Tutorials by Avinash Mishrda";
	  
	  //Actual Title
	  String actualTitle = driver.getTitle();
	  
	  //Conditions
	  if(actualTitle.equals(expectedTitle)){
		  
		  System.out.println("Title Matched");
		  
	  }else {
		  
		  System.out.println("Title didn't match");
	  }
	  
	  driver.close();
	  
	  driver.quit();
  }
}

 

Comparison using Java Assertion or Selenium Assertion

This is an advanced yet effective technique to identify pass or fail criteria of any test case in Selenium WebDriver. It also has the good impact to handle error message in Selenium. I will have a separate tutorial on Assertions in Selenium, but for this time being, let me tell you the use of assertTrue() method here.

Assert.assertTrue(conditions, “Message on failure”);

Here we have to define the conditions to check the failure of the particular scenario if the scenario fails then assertTrue will send a pre-defined message to the console. See the code snippet below with screenshot from the console:

package SeleniumTester;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class ErrorMessageDemo {
  @Test
  public void errorMessageDemo() {
	  
	  System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
	  
	  WebDriver driver = new ChromeDriver();
	 
	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	  
	  driver.get("http://www.inviul.com");
	  
	  driver.manage().window().maximize();
	  
	  //Expected Title 
          String expectedTitle = "Free Selenium Tutorials by Avinash Mishrda"; 

          //Actual Title 
          String actualTitle = driver.getTitle(); 

          //Conditions 
          boolean t = (actualTitle.equals(expectedTitle));
	 
	  //Assertions
	   Assert.assertTrue(t, "Title didn't match");
	  
	  driver.close();
	  
	  driver.quit();
  }
}

Handle error message in Selenium

These three techniques are the best techniques to handle error message in Selenium WebDriver. We took title comparison as a referential example to guide you. Suppose a scenario where you are getting some error message then in such situation select the entire error message and define a dynamic XPath for it, then use getText() method to capture the error message. Further, you can do comparisons in Boolean conditions. You can use above techniques further to handle the error message.

Hope this tutorial guide you properly to define the pass-fail criteria of any test cases in Selenium. See you in the next tutorial.

Happy Learning. 🙂

 

2 Comments

Leave a Reply