Common Function To Compare Two Double Values In Selenium WebDriver

Comparison of double values Is non much to a greater extent than dissimilar than comparison of Integer values equally described In my previous post service but permit me laissez passer you lot instance of same hence that you lot tin leave of absence It better. Let me nation you lot 1 affair that comparison of dissimilar information types (compare 2 strings, compare 2 Integers, etc..) Is pure coffee concept together with It has non whatever relation amongst selenium webdriver. My principal Intention Is to demo you lot that how to grip remainder of essay out execution based on execution effect together with how to score your essay out neglect In webdriver essay out study without whatever Interruption In execution.

Let me attempt to depict you lot how to compare 2 double values amongst example.

As explained In my previous post, If you lot are retrieving value from spider web page together with hence It volition survive ever In string format because webdriver's .getText() operate Is returning It equally a string. So my get-go trace 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 trace Is to exercise full general operate In CommonFunctions course of education to compare 2 double values. Latter on I volition utilisation this operate whenever required In my test. Main piece of occupation of this operate Is to convey 2 double values together with hence compare them together with supply truthful or imitation based on comparing result. See bellow given example.

package Testng_Pack;  import org.testng.Assert; import org.testng.asserts.SoftAssert;  populace course of education CommonFunctions {    SoftAssert Soft_Assert = novel SoftAssert();     populace boolean compareDoubleVals(double actualDobVal, double expectedDobVal){   try{    //If this assertion volition fail, It volition throw exception together with grab block volition survive executed.    Assert.assertEquals(actualDobVal, expectedDobVal);    }catch(Throwable t){     //This volition throw soft assertion to maintain leave of absence on your execution fifty-fifty assertion failure.     //Un-comment bellow given difficult assertion trace together with commnet soft assertion trace If you lot wants to halt essay out 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 volition non match, supply false.     supply false;    }   //If  double values match, supply true.   supply true;  } }

Now I tin utilisation compareDoubleVals operate In my essay out whenever required equally 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;    populace course of education Common_Functions_Test extends CommonFunctions{ WebDriver driver;    @BeforeTest  populace void StartBrowser_NavURL() {   driver = novel FirefoxDriver();   driver.get(" ");  }    @AfterTest  populace void CloseBrowser() {     driver.quit();    }    @Test  populace 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 status to cheque Double values fit or not.   if(compareDoubleVals(actualVal, 20.63)){    //If values match, This block volition survive executed.    System.out.println("Write Action taking lines In this block when Values match.");   }else{    //If values non match, This block volition survive executed.    System.out.println("Write Action taking lines In this block when Values non match.");   }      //To score essay out neglect In study at the terminate of execution If values non match.   Soft_Assert.assertAll();  }  }

Above essay out volition call compareDoubleVals operate together with It volition supply imitation because actual together with expected values volition non match. So essay out volition survive marked equally fail. You tin utilisation difficult assertion likewise Inside compareDoubleVals operate to halt your essay out on assertion failure.

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