Here I’m back with part 2 of the series. Here I would be explaining the test class. It’s basically the hybrid framework in detail.
The Algorithm:
Let’s start with the basic algorithm. Here for each new incoming data row, according to the property a conditional block is executed. Now the question might be why take such a complicated approach. The answer is this way, for any change there is minimalistic change/no change in the code which makes the code easily maintainable and reusable. Below is the algorithm.
While resultset has a new row if property = pagetitle Do some page title related operations if property = link Do some link related operation if property = textbox Do some Text Box related operation if property = image Do some image related operation if property = checkbox Do some check box related operation if property = dropdown Do some select related operation if property = radio Do some radio button related operation if property = text Do some text check related operation if property = url Do some url related operation if property = value Do some field value check operation if property = object Do some object finding operation if property = pop up window Do some pop window related operation end While
Remember each row contains a property field, a locator field, a locatortype field, a value field, an expectedResult field, a permission field and a name field. Let’s see what’s the purpose of each of these field.
- Property field: This field is a mandatory field. The value of this field determines which condition in the loop will be executed.
- Locator field: This field is optional and whenever not required can be left blank. This is used in combination with locatorType field to locate any object in a page. This is used by inner level of classes which will be explained a little later.
- LocatorType field: This field is mandatory when there is a corresponding locator value is available. This helps in locating objects in a page.
- Value: This field is optional and whenever not required can be left blank. This is used when there is a need of giving input to any field.
- ExpectedResult field: This field is again optional and whenever not required can be left blank. This is used whenever there is checkpoint or in laymans term a data equality check is needed.
- Permission field: This field is optional and whenever not required can be left blank. This field is used to control the object level operation to be performed. This will be explained in a later state.
- Name field: This field is optional and used for identification purpose in the result.
Now let’s go into one of the condition and see how it works. Let’s assume we are in condition “if property = dropdown”. The implementation for each condition is written in a different class. Lets consider the algorithm for DropDown class for this particular condition.
Assumption: We have 7 variables declared in the class to hold valued for each of the columns of the row under execution.
Function testDropDown() if permission = wait for some time if locatorType = xpath if isElementPresent(locator) select(By.xpath(value)) sleep for 5 seconds else setActualResult(“Element Not Found”) if locatorType = cssSelector if isElementPresent(locator) select(By.cssSelector(value)) sleep for 5 seconds else setActualResult(“Element Not Found”) if permission = “” or permission = null if locatorType = xpath if isElementPresent(locator) select(By.xpath(value)) else setActualResult(“Element Not Found”) if locatorType = cssSelector if isElementPresent(locator) select(By.cssSelector(value)) else setActualResult(“Element Not Found”) End Function
This is just the algorithm. An example of a concrete implementation is there in later stage. The algorithm can be modified to suite your need.
Example of Conditional Test Element class:
Below is the code for the DropDown class for the aforementioned algorithm of the same.
import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.Select; import java.awt.List; @SuppressWarnings("unused") public class DropDown { private WebDriver localDriver; String localProperty; String localLocator; String localLocatorType; String localValue; String localExpectedResult; String localPermission; String localName; String actualResult; String cause; public DropDown(WebDriver driver, String property, String locator, String locatorType, String value, String expectedResult, String permission, String name){ localDriver = driver; localProperty = property; localLocator = locator; localLocatorType = locatorType; localValue = value; localExpectedResult = expectedResult; localPermission = permission; localName = name; cause = ""; } public boolean testDropDown() throws Exception{ if(isElementPresent(By.xpath(localLocator)) || isElementPresent(By.xpath(localLocator+"']")) || isElementPresent(By.cssSelector(localLocator)) || isElementPresent(By.name(localLocator)) || isElementPresent(By.id(localLocator))){ try{ if(localLocatorType.equals("xpath")){ Select select = new Select(localDriver.findElement(By.xpath(localLocator))); if(localPermission.equals("wait for some time")){ select.selectByVisibleText(localValue); Thread.sleep(5000); } else if(localPermission.equals("count")){ int noOfElements=select.getOptions().toArray().length; System.out.println("There are "+noOfElements+" options in "+localName+" dropdown"); } else if(localPermission.equals("all options")){ List allOptions=(List) select.getOptions(); System.out.println("All the options in the dropdown are as below:"); for(int count=0; count<allOptions.getItemCount(); count++){ System.out.println(allOptions.toString()); } } else{ select.selectByIndex(1); if(!localValue.equals("")) select.selectByVisibleText(localValue); } } else if(localLocatorType.equals("cssSelector")){ Select select = new Select(localDriver.findElement(By.cssSelector(localLocator))); if(localPermission.equals("wait for some time")){ select.selectByVisibleText(localValue); Thread.sleep(5000); } else if(localPermission.equals("count")){ int noOfElements=select.getOptions().toArray().length; System.out.println("There are "+noOfElements+" options in "+localName+" dropdown"); } else if(localPermission.equals("dynamic")){ select.selectByIndex(1); } else if(localPermission.equals("all options")){ List allOptions=(List) select.getOptions(); System.out.println("All the options in the dropdown are as below:"); for(int count=0; count<allOptions.getItemCount(); count++){ System.out.println(allOptions.toString()); } } else{ select.selectByIndex(1); if(!localValue.equals("")) select.selectByVisibleText(localValue); } } } } catch (Exception e){ setActualResult("Exception Occurred"); setCause(e.getMessage()); return false; } setActualResult("Element Present"); return true; } else{ setActualResult("Element Not Found"); setCause("Element Not Found"); return false; } } public void setActualResult(String actualResult){ this.actualResult = actualResult; } public String getActualResult(){ return actualResult; } public void setCause(String cause){ this.cause = cause; } public String getCause(){ return cause; } private boolean isElementPresent(By by) { try { localDriver.findElement(by); return true; } catch (NoSuchElementException e) { return false; } } }
I hope, this post is helpful to somebody. 🙂
Hi Thanks a lot for the explanation, this is the one i was looking for.
but where do I get jar for the classes com.pilanisoftlabs.TestElements?
Thank you
also, you have used import org.openqa.selenium.support.ui.Select for dropdown but can you tell me how do i go about with link,textbox etc,.
Those ate custom classes present in that package. There’s no jar for the same. You need to write the classes in the package yourself. For the same, I have given the reference of the example class.
Hi, really great explanation!!!
I am not using Sql Database. just using excel sheet for making Tables. plz let me know what should i do for the same.