Overview:
In this article, I would like to show you a simple keyword-driven framework using Java8. Please do note that this article will not cover all the possible keywords. My aim here is just to give you an idea how we could use Java – lambdas.
Java 8:
Java-8 has introduced a lot of cool features. Lambdas and streams etc which makes the code look readable and easily maintainable.
For ex: Assume, we have a requirement to calculate the square for the list of numbers which are greater than 5.
Function<Integer, Integer> square = (i) -> i*i;
Arrays.asList(1,2,3,4,5,6,7,8,9)
.stream() // fetches the element one by one
.filter(i -> i > 5) // filters the number
.map(square) // transforms the current element to i*i
.forEach(System.out::println); // prints the transformed element
The above code looks much better than the below traditional code.
int[] arr = new int[]{1,2,3,4,5,6,7,8,9};
for(int i = 0; i < arr.length; i++){
if(i > 5){
System.out.println(i*i);
}
}
square is a lambda function in the above example. The advantage of using lambda is – it can be passed as an argument to a method. It will be very useful in replacing traditional if-else / switch conditions. I would be using lambda to define the operation for each keywords in the framework.
Demo Application:
I am going to use below Mercury tours application for the demo. I will consider only the registration flow. As I had mentioned, aim of the article is not to create a framework for you, instead, give you an idea.
I need 3 keywords to complete the registration process.
- SET_VALUE – to fill the form with data
- SELECT – to select the drop down
- CLICK – to click on the submit button.
Keyword Library:
I create a separate class which contains the list of keywords and the corresponding operations in a map. We get specific operation by using the Key.
public class Actions {
// to enter given value for an element
private static final BiConsumer<WebElement, String> SET_VALUE = (ele, param) -> {
ele.sendKeys(param);
};
// to perform click operation
private static final BiConsumer<WebElement, String> CLICK = (ele, param) -> {
ele.click();
};
// to select drop down values
private static final BiConsumer<WebElement, String> SELECT = (ele, param) -> {
Select select = new Select(ele);
select.selectByVisibleText(param);
};
// this map will hold all the lamdas / keyword operations
private static final Map<String, BiConsumer<WebElement, String>> map = new HashMap<String, BiConsumer<WebElement, String>>();;
static {
map.put("SetValue", SET_VALUE);
map.put("Click", CLICK);
map.put("Select", SELECT);
}
// return the specific operation based on the key
public static BiConsumer<WebElement, String> get(String action){
return map.get(action);
}
}
Demo Application – Registration Process:
The registration page has the following steps.The steps are written in a spreadsheet as shown below. I use ‘name’ selector. But I would suggest you to go for CSS Selectors when you design the framework.
We would be reading the steps & convert to a list of TestStep objects which will look like this.
public class TestStep {
private final By selector;
private final WebDriver driver;
private final BiConsumer < WebElement, String > consumer;
private final String param;
// pass the driver, selector, action keyword, any paramater to be used
public TestStep(WebDriver driver, String selector, String action, String param) {
this.selector = By.name(selector);
this.driver = driver;
this.consumer = Actions.get(action);
this.param = param;
}
// find the element & perform the action as per the keyword
public void perform() {
WebElement ele = this.driver.findElement(selector);
this.consumer.accept(ele, param);
}
}
Reading Spreadsheet:
Reading spreadsheet using Java is very easy. I had recommended Fillo library as used in the article.
// To store the list of TestSteps from the spreadsheet
List < TestStep > steps = new ArrayList < > ();
while (recordset.next()) {
String nameSelector = recordset.getField("Selector");
String action = recordset.getField("Action");
String param = recordset.getField("Param");
//create new TestStep
TestStep step = new TestStep(driver, nameSelector, action, param);
steps.add(step);
}
Create an instance of the WebDriver as shown here,
WebDriver driver = new ChromeDriver();
driver.get("http://newtours.demoaut.com/mercuryregister.php");
Then last step would be to connect everything together. The getSteps will return the above list of TestSteps. Then, we basically iterate all the TestStep objects, call the perform method to invoke the corresponding operation.
XLSReader suite = new XLSReader("tests.xls");
suite.getSteps(driver, "select * from Step where Active = 'Y'")
.stream()
.forEach(TestStep::perform);
Demo:
Summary:
Using Java 8 lambdas, we could create a simple keyword driven framework very easily. By storing Lambdas in a map, we avoid the traditional if-else / switch statements. We will explore more Java 8 features in the upcoming articles.
Happy Testing & Subscribe 🙂
how do i read any value available in the in a Test Data excel sheet in selenium
The above approach has hard coded value. if you need to refer any variable, or any other sheet, you can do by
${somevariable}
. If something starts with ${..}, then we need to process accordingly.
if i have two sheets and i want to run the TestData on the second sheet how would do it using this framework , Can you give me a example code
In this article – locate the below statement.
String param = recordset.getField("Param");
Here, you need to add your logic to fetch the data from some other sheet – something like this.
String param = ExcelUtil.readFromOtherSheet(recordset.getField("Param"));