You are on page 1of 3

Compare Two Integer Values In Selenium WebDriver Test

We have learnt how to create common function to compare two strings In my


PREVIOUS POST. Same way, Many times you need to compare two Integer
values. And based on comparison result, You can perform your next step of
test execution. You can also stop or continue your test execution on assertion
failure by using soft or hard TestNG assertions. In this post, we will see how
to compare two Integer values using common function.
Main Intention of creating this kind of common functions Is to minimize your
code size. You have to create It only once and then you can call It again and
again whenever required In your test. And for that, You have to create
common class which should be accessible from all the test classes.
If you are retrieving text(which Is Integer value) from web page using
webdriver's .getText() function then It will be string and you have to convert
It In Integer value using bellow given syntax before comparision.
String actualValString = driver.findElement(By.xpath("//*[@id='post-body8228718889842861683']/div[1]/table/tbody/tr[1]/td[2]")).getText();
//To convert actual value string to Integer value.
int actualVal = Integer.parseInt(actualValString);
Bellow given example will explain you how to create common function to
compare two Integers In your CommonFunctions class.
package Testng_Pack;
import org.testng.Assert;
import org.testng.asserts.SoftAssert;
public class CommonFunctions {
SoftAssert Soft_Assert = new SoftAssert();
public boolean compareIntegerVals(int actualIntegerVal, int
expectedIntegerVal){
try{
//If this assertion will fail, It will throw exception and catch block will be
executed.
Assert.assertEquals(actualIntegerVal, expectedIntegerVal);
}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 '"+actualIntegerVal+"' And Expected Value
'"+expectedIntegerVal+"' Do Not Match.");
Soft_Assert.fail("Actual Value '"+actualIntegerVal+"' And Expected Value
'"+expectedIntegerVal+"' Do Not Match.");
//If Integer values will not match, return false.
return false;
}
//If Integer values match, return true.
return true;
}
}
Now you can call compareIntegerVals() function In your test to compare two
Integer values as shown 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/05/form.html");
}
@AfterTest
public void CloseBrowser() {
driver.quit();
}
@Test

public void testToCompareIntegers(){


String actualValString = driver.findElement(By.xpath("//*[@id='post-body8228718889842861683']/div[1]/table/tbody/tr[1]/td[2]")).getText();
//To convert actual value string to Integer value.
int actualVal = Integer.parseInt(actualValString);
//Call compareIntegerVals method Inside If condition to check Integer values
match or not.
if(compareIntegerVals(actualVal, 5)){
//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();
}
}