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