Best way to handle error message in Selenium WebDriver

Category: Selenium WebDriver 45 2

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-

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:

 

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:

 

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.

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:

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. 🙂

 

Related Articles

2 thoughts on “Best way to handle error message in Selenium WebDriver

Add Comment