Selenium WebDriver – Creating A Simple Keyword-Driven Framework Using Java Lambdas


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;

       .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){

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 BiConsumer<WebElement, String> SET_VALUE = (ele, param) -> {

    // to perform click operation
    private static BiConsumer<WebElement, String> CLICK = (ele, param) -> {;

    // to select drop down values
    private static BiConsumer<WebElement, String> SELECT = (ele, param) -> {
        Select select = new Select(ele);

    // this map will hold all the lamdas / keyword operations
    private static Map<String, BiConsumer<WebElement, String>> map = null;
    static {
        map = new HashMap<String, BiConsumer<WebElement, String>>();
        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 =;
        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 ( {

    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);


Create an instance of the WebDriver as shown here,

WebDriver driver = new ChromeDriver();

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'")



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 🙂


Share This:

Categories: Articles, Framework, Selenium

Leave a Reply

Your email address will not be published. Required fields are marked *