Delivered by FeedBurner

General Function To Comparing Two Strings In Selenium WebDriver TestCase

When you will use selenium webdriver to automate real world applications, Many times you have to compare two strings like page title, product name or any other string. And based on comparison result you have to take specific action. Simple example of such scenario Is, You are navigating on any view product page to add product In to basket. But before adding product In basket, You wants to verify product's name to check It Is correct product or not.

Many time you have to compare strings In automating any single application. So What you will do? You will write same code multiple time In your different scripts? General way(Which I am using) Is to create common method In base class (Which Is accessible from all test classes) to compare two strings. Then we can call that method by passing actual and expected string to compare strings whenever required In out tests.

Let me explain you how to do It.

Step 1 : Create CommonFunctions Class In your package which can be accessible by any other class.
Step 2 : Create method compareStrings which can accept two string values. Also Create object of TestNG SoftAssert class to assert assertion softly as shown In 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 compareStrings(String actualStr, String expectedStr){
   //If this assertion will fail, It will throw exception and catch block will be executed.
   Assert.assertEquals(actualStr, expectedStr);
   }catch(Throwable t){
    //This will throw soft assertion to keep continue your execution even assertion failure.
    //Use here hard assertion ""Failure Message String")" If you wants to stop your test on assertion failure."Actual String '"+actualStr+"' And Expected String '"+expectedStr +"' Do Not Match.");
    //If Strings will not match, return false.
    return false;
  //If Strings match, return true.
  return true;

Step 3 : Now we can call compareStrings method In our test class whenever required as shown In bellow given example. We have Inherited CommonFunctions class to use Its function In Common_Functions_Test. Also call  Soft_Assert.assertAll() method at last of your test to mark your test fail If both strings not match. Read More about 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
public class Common_Functions_Test extends CommonFunctions{
WebDriver driver; 
 public void StartBrowser_NavURL() {
  driver = new FirefoxDriver();
 public void CloseBrowser() {  
 public void testToCompareStrings(){  
  String actualTitle = driver.getTitle();
  //Call compareStrings method Inside If condition to check string match or not.
  if(compareStrings(actualTitle, "Only Testing: LogIn")){
   //If strings match, This block will be executed.
   System.out.println("Write Action taking lines In this block when title match.");
   //If strings not match, This block will be executed.
   System.out.println("Write Action taking lines In this block when title not match.");
  //To mark test fail In report at the end of execution If strings not match.

Above given example will compare actual and expected title strings and mark test failure at the end of test execution If both strings not match without any Interruption In execution. Now you can call compareStrings method any time to compare strings. You just need to provide expected and actual string values.

Note : You can use hard assertion In your common function catch block at place of soft assertion If you wants to stop test on failure. See bellow. If you will use bellow given syntax In catch block then It will stop test execution on failure."Actual String '"+actualStr+"' And Expected String '"+expectedStr+"' Do Not Match.");

No comments:

Post a Comment