General Function To Comparing Two Strings In Selenium WebDriver TestCase

When you lot volition purpose selenium webdriver to automate existent footing applications, Many times you lot cause got to compare ii strings similar page title, production get upwards or whatever other string. And based on comparison upshot you cause got to convey specific action. Simple instance of such scenario Is, You are navigating on whatever persuasion product page to add together production In to basket. But earlier adding production In basket, You wants to verify product's name to depository fiscal establishment check It Is right production or not.

Many fourth dimension you lot cause got to compare strings In automating whatever unmarried application. So What you lot volition do? You volition write same code multiple fourth dimension In your unlike scripts? General way(Which I am using) Is to practise mutual method In base of operations course of study (Which Is accessible from all bear witness classes) to compare ii strings. Then nosotros tin telephone telephone that method past times passing actual as well as expected string to compare strings whenever required In out tests.

Let me explicate you lot how to practise It.


Step 1 : Create CommonFunctions Class In your parcel which tin survive accessible past times whatever other class.
Step 2 : Create method compareStrings which tin cause got ii string values. Also Create object of TestNG SoftAssert course of study to assert assertion softly every bit shown In bellow given example.

package Testng_Pack;  import org.testng.Assert; import org.testng.asserts.SoftAssert;  populace course of study CommonFunctions {    SoftAssert Soft_Assert = novel SoftAssert();    populace boolean compareStrings(String actualStr, String expectedStr){   try{    //If this assertion volition fail, It volition throw exception as well as select handgrip of block volition survive executed.    Assert.assertEquals(actualStr, expectedStr);    }catch(Throwable t){     //This volition throw soft assertion to hold proceed your execution fifty-fifty assertion failure.     //Use hither difficult assertion "Assert.fail("Failure Message String")" If you lot wants to halt your bear witness on assertion failure.     Soft_Assert.fail("Actual String '"+actualStr+"' And Expected String '"+expectedStr +"' Do Not Match.");     //If Strings volition non match, render false.     render false;    }   //If Strings match, render true.   render true;  }  }

Step 3 : Now nosotros tin call compareStrings method In our bear witness course of study whenever required every bit shown In bellow given example. We cause got Inherited CommonFunctions course of study to purpose Its component subdivision In Common_Functions_Test. Also telephone telephone  Soft_Assert.assertAll() method at terminal of your bear witness to grade your bear witness neglect If both strings non match. Read More close usage of TestNG Soft Assertion In webdriver.

package Testng_Pack;  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;  //Used Inheritance populace course of study 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 testToCompareStrings(){     String actualTitle = driver.getTitle();   //Call compareStrings method Inside If status to depository fiscal establishment check string jibe or not.   if(compareStrings(actualTitle, "Only Testing: LogIn")){    //If strings match, This block volition survive executed.    System.out.println("Write Action taking lines In this block when championship match.");   }else{    //If strings non match, This block volition survive executed.    System.out.println("Write Action taking lines In this block when championship non match.");   }      //To grade bear witness neglect In study at the halt of execution If strings non match.   Soft_Assert.assertAll();  }  }

Above given instance volition compare actual as well as expected championship strings as well as grade bear witness failure at the halt of bear witness execution If both strings non jibe without whatever Interruption In execution. Now you lot tin telephone telephone compareStrings method whatever fourth dimension to compare strings. You merely demand to render expected as well as actual string values.

Note : You tin purpose difficult assertion In your mutual component subdivision select handgrip of block at house of soft assertion If you lot wants to halt bear witness on failure. See bellow. If you lot volition purpose bellow given syntax In select handgrip of block thus It volition halt bear witness execution on failure.

Assert.fail("Actual String '"+actualStr+"' And Expected String '"+expectedStr+"' Do Not Match.");


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