0% found this document useful (0 votes)
141 views

Common Function To Compare Two Double Values in Selenium WebDriver

This document provides an example of how to compare double values in Selenium WebDriver tests. It describes converting a string to a double, creating a common function to compare doubles that returns true or false, and calling that function within a test to either execute pass blocks or fail blocks depending on the comparison result. The function uses a soft assertion to fail the test at the end rather than interrupting execution.

Uploaded by

sudheer reddy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
141 views

Common Function To Compare Two Double Values in Selenium WebDriver

This document provides an example of how to compare double values in Selenium WebDriver tests. It describes converting a string to a double, creating a common function to compare doubles that returns true or false, and calling that function within a test to either execute pass blocks or fail blocks depending on the comparison result. The function uses a soft assertion to fail the test at the end rather than interrupting execution.

Uploaded by

sudheer reddy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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-body4292417847084983089']/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
import
import
import
import
import

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

public class Common_Functions_Test extends CommonFunctions{


WebDriver driver;
@BeforeTest
public void StartBrowser_NavURL() {
driver = new FirefoxDriver();
driver.get("http://only-testing-blog.blogspot.in/2014/01/textbox.html");
}
@AfterTest
public void CloseBrowser() {
driver.quit();
}
@Test
public void testToCompareDoubles(){
String actualValString = driver.findElement(By.xpath("//*[@id='postbody-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.

You might also like