Method To Compare Two Integer Values In Selenium WebDriver Test

We bring learnt how to practise mutual business office to compare 2 strings In my previous post. Same way, Many times you lot take away to compare 2 Integer values. And based on comparison result, You tin perform your adjacent stride of examine execution. You tin likewise halt or choke along your examine execution on assertion failure yesteryear using soft or difficult assertions of TestNG . In this post, nosotros volition encounter how to compare 2 Integer values using mutual function.

Main Intention of creating this sort of mutual functions Is to minimize your code size. You bring to practise It solely in 1 trial together with and hence you lot tin telephone telephone It over again together with over again whenever required In your test. And for that, You bring to practise mutual flat which should hold out accessible from all the examine classes.

If you lot are retrieving text(which Is Integer value) from spider web page using webdriver's .getText() business office together with hence It volition hold out string together with you lot bring to convert It In Integer value using bellow given syntax earlier comparision.

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

Bellow given instance volition explicate you lot how to practise mutual business office to compare 2 Integers In your CommonFunctions class.

package Testng_Pack;  import org.testng.Assert; import org.testng.asserts.SoftAssert;  world flat CommonFunctions {    SoftAssert Soft_Assert = novel SoftAssert();     world boolean compareIntegerVals(int actualIntegerVal, int expectedIntegerVal){   try{    //If this assertion volition fail, It volition throw exception together with grab block volition hold out executed.    Assert.assertEquals(actualIntegerVal, expectedIntegerVal);    }catch(Throwable t){     //This volition throw soft assertion to maintain choke along your execution fifty-fifty assertion failure.     //Un-comment bellow given difficult assertion trouble together with commnet soft assertion trouble If you lot wants to halt examine 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 volition non match, supply false.     supply false;    }   //If  Integer values match, supply true.   supply true;  } }

Now you lot tin call compareIntegerVals() business office In your examine to compare 2 Integer values every bit shown 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;    world flat Common_Functions_Test extends CommonFunctions{ WebDriver driver;    @BeforeTest  world void StartBrowser_NavURL() {   driver = novel FirefoxDriver();   driver.get(" ");  }    @AfterTest  world void CloseBrowser() {     driver.quit();    }    @Test  world void testToCompareIntegers(){     String actualValString = driver.findElement(By.xpath("//*[@id='post-body-8228718889842861683']/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 status to banking venture check Integer values fit or not.   if(compareIntegerVals(actualVal, 5)){    //If values match, This block volition hold out executed.    System.out.println("Write Action taking lines In this block when Values match.");   }else{    //If values non match, This block volition hold out executed.    System.out.println("Write Action taking lines In this block when Values non match.");   }      //To score examine neglect In study at the destination of execution If values non match.   Soft_Assert.assertAll();  }  }

You tin VIEW MORE TUTORIAL ON SELENIUM WEBDRIVER which volition genuinely helps you lot In selenium webdriver learning In free.


More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post