Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

Learning Selenium - Validating existence of object based on text of object




In this post, We will discuss on an example to verify if an element exist in the Page using Selenium. Together with Selenium concept of finding list of elements using findElements, we will discuss on following concepts useful on using Selenium with Java.



1. In this article, we will know how to create a generic function to verify elements of a specific object type based on text of object. Since this is a genric approach, we can use similar concept on working with multiple object. 


2. Explaining how to split a string based on delimiter into a string array.


3. Explaining how to use try catch statements to handle error in the Page. In case of no element found in the page matching the identifier, Exception needs to be handled using try catch block,


4. We will explain how to use switch statement in Java useful from a tester's perspective.  Switch is a useful concept to work with different inputs to create a generic function.


5.How to loop through list of elements is explained in the below example. Once we find a list of web Elements. how to extract each element in the list.


6. Creating a function with return value as boolean is explained.





Please find the code below: 





public boolean boolElementExists(String strObjType, String strText)
{
 boolean boolret = false;
//Suppose there are multiple object text provided of an object type provided in the strText, we will provide text separated by > .
//for e.g : login>HELP as link object in the Page
 String[] sre = strText.split(">");
// Loop through each of the text provided 
 for ( int j = 0;j<sre.length;j++)
 {
  List<WebElement> elemlst = null;
  // We will convert the strObjType to Upper Case and trimming the text around.
  strObjType = strObjType.toUpperCase().trim();
  //we will use try catch to return false mostly to handle scenario of object not found expection , in this case we will return false from the function
  try
  {
   //we will use a switch to perform action on an object.A case is used in this case to store different value in WebElement List Object. Using switch provides good option to create generic functions 
   switch (strObjType)
   {
   // in case of link object storing all the elements of type link in elemlst
   case "LINK":
// driver is already discussed in a previous article . Please refer to below article to understand how a driver object is created. 
Understanding Selenium Web Driver: Launching Selenium on different browsers

     elemlst = driver.findElements(By.tagName("a")); 
    break;
    // in case of link object storing all the elements of type link in label
   case "LABEL":
    elemlst = driver.findElements(By.tagName("label")); 
    break;
    // This is just a example , we can define other objects type also
   }
   
   // Now we will loop through all the objects of link , and whereever the text matches , we will loop out of the function and true will be returned.
   for (int i=0;i<elemlst.size();i++)
   {
    if (elemlst.get(i).getText().contentEquals(strText.trim()))
      {
       boolret = true;
       i= elemlst.size()-1;
      }
   }
   // In case no match is found , it will return false.
   if (boolret = false)
   {
    return false;
   }
   }
  catch(Exception e)
  {
   return false;
  }
 }
 return boolret;
}

No comments:

Post a Comment