Common Function To Compare Two Double Values In Selenium WebDriver

Comparison of double values Is not much more different than comparison of Integer values as described In my previous post but let me give you example of same so that you can get It better. Let me tell you one thing that comparison of different data types (compare two strings, compare two Integers, etc..) Is pure java concept and It has not any relation with selenium webdriver. My main Intention Is to show you that how to handle rest of test execution based on execution result and how to mark your test fail In webdriver test report without any Interruption In execution.

Let me try to describe you how to compare two double values with example.

As explained In my previous post, If you are retrieving value from web page then It will be always In string format because webdriver's .getText() function Is returning It as a string. So my first task Is to convert It from string to double using bellow given syntax.

String actualValString = driver.findElement(By.xpath("//*[@id='post-body-4292417847084983089']/div[1]/table/tbody/tr[2]/td[4]")).getText();
//To convert actual value string to Double value.
double actualVal = Double.parseDouble(actualValString);

Second task Is to create general function In CommonFunctions class to compare two double values. Latter on I will use this function whenever required In my test. Main work of this function Is to accept two double values then compare them and return true or false based on comparison result. See bellow given example.

package Testng_Pack;

import org.testng.Assert;
import org.testng.asserts.SoftAssert;

public class CommonFunctions {
 
 SoftAssert Soft_Assert = new SoftAssert(); 
 
 public boolean compareDoubleVals(double actualDobVal, double expectedDobVal){
  try{
   //If this assertion will fail, It will throw exception and catch block will be executed.
   Assert.assertEquals(actualDobVal, expectedDobVal);
   }catch(Throwable t){
    //This will throw soft assertion to keep continue your execution even assertion failure.
    //Un-comment bellow given hard assertion line and commnet soft assertion line If you wants to stop test execution on assertion failure. 
    //Assert.fail("Actual Value '"+actualDobVal+"' And Expected Value '"+expectedDobVal+"' Do Not Match.");
    Soft_Assert.fail("Actual Value '"+actualDobVal+"' And Expected Value '"+expectedDobVal+"' Do Not Match.");
    //If double values will not match, return false.
    return false;
   }
  //If  double values match, return true.
  return true;
 }
}

Now I can use compareDoubleVals function In my test whenever required as described In bellow given example.

package Testng_Pack;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;



public class Common_Functions_Test extends CommonFunctions{
WebDriver driver;
 
 @BeforeTest
 public void StartBrowser_NavURL() {
  System.setProperty("webdriver.gecko.driver", "D:\\Selenium Files\\geckodriver.exe");
  driver = new FirefoxDriver();
  driver.get("http://only-testing-blog.blogspot.com/2014/01/textbox.html");
 }
 
 @AfterTest
 public void CloseBrowser() {  
  driver.quit();  
 }
 
 @Test
 public void testToCompareDoubles(){  
  String actualValString = driver.findElement(By.xpath("//*[@id='post-body-4292417847084983089']/div[1]/table/tbody/tr[2]/td[4]")).getText();
  //To convert actual value string to Double value.
  double actualVal = Double.parseDouble(actualValString);
  //Call compareDoubleVals method Inside If condition to check Double values match or not.
  if(compareDoubleVals(actualVal, 20.63)){
   //If values match, This block will be executed.
   System.out.println("Write Action taking lines In this block when Values match.");
  }else{
   //If values not match, This block will be executed.
   System.out.println("Write Action taking lines In this block when Values not match.");
  }   
  //To mark test fail In report at the end of execution If values not match.
  Soft_Assert.assertAll();
 } 
}

Above test will call compareDoubleVals function and It will return false because actual and expected values will not match. So test will be marked as fail. You can use hard assertion too Inside compareDoubleVals function to stop your test on assertion failure.

2 comments:

  1. I'm getting an error Assert.assertEquals(actualDobVal, expectedDobVal); it accept three argument when using double
    so i have solved it Assert.assertEquals(actualDobVal, expectedDobVal, 0);

    ReplyDelete