Sunday, 23 February 2014

Keyword driven automation framework in Selenium Webdriver in Java

Keyword driven Automation Framework is very popular framework used in Selenium Webdriver with Java.
In this article I will explain you all details about how we can design and use keyword driven automation framework in Selenium Webdriver with Java along with example.

Keyword driven automation framework in Selenium Webdriver with Java - Introduction

In keyword driven automation framework we create the methods in Java that are mapped to the functionality of the application.

For example -
Suppose you have a bus ticket booking application which provides many features like

  1. Login to the bus booking website 
  2. Search Buses with given source and destination and time
  3. Select the bus tickets
  4. Book bus tickets
  5. Cancel bus tickets
  6. View the booking history 
  7. Make the payment.

To automate the test cases for such web applications, We usually write the methods that perform specific task. For example we may write the searchBus method in Java which will search the buses for given source city, Destination city and Date.

Similarly we will create the methods for each functionality. The advantage of creating methods is that we can re-use these methods in multiple test cases with different input test data.
This speeds up the automation process with increased productivity.

The Components of keyword driven automation framework in Selenium Webdriver with Java

Each keyword driven automation framework has some common components as mentioned below.

  1. Java Class Library with functionality specific methods.
  2. Test Data Sheet (generally in excel format)
  3. Selenium Webdriver with Java.
  4. Reports in HTML format)
  5. Main Java driver script

1. Java Class Library with functionality specific methods.


As explained earlier, we can create methods for each functionality in the application like bookTicket, makePayment etc.

Sample Java method is shown below to login to the web application.

public static boolean Login()
{
   
driver.navigate().to("http://www.abc.com");
WebElement e1 = driver.findElement(By.id("UserName"));
e1.sendKeys("userid");
WebElement e2 = driver.findElement(By.id("Password"));
e2.sendKeys("password");
WebElement e3 = driver.findElement(By.name("submit"));
e3.click();

Boolean isPresent = driver.findElements(By.className("logoutlink")).size()>0;

if (isPresent == true)
{
//System.out.println("Login was successful");
       return true;

}
else
{
       //System.out.println("Login was not successful");
       return false;
}

}

//Please note that you can write the methods to perform more complex operations in similar //fashion


2. Test Data Sheet in Selenium Webdriver framework

As displayed in below figure, the data sheet contains below columns.
  1. ID -Manual test case ID
  2. Test_Case_Name - Name of the test case
  3. Exec_Flag - Execution flag to tell if you want to execute this test case. Y means that test will be executed
  4. Test_Step_Id - Step Id of the Test case
  5. Test_Case_Steps - Step name of the test case
  6. Keyword - The name of method in function library you want to call.
  7. objectTypes - type of the web elements like webedit, weblist, webcheckbox etc
  8. objectNames - Web element identification method and expression like xpath://td
  9. objectValues - actual test data you want to enter in the webElement 
  10. parameter1 - extra parameter to drive the method control
  11. parameter2 - one more parameter to drive the method control

Sample test Datasheet in selenium webdriver in Java


3. Selenium Webdriver in Java
This is the driver instance you will create to execute the test cases.
Sample code to create a web driver instance is given below.

System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
WebDriver drivernew ChromeDriver();


4. Creating html Reports in Selenium Webdriver automation framework
You can create the html reports using file handling mechanism in Java.

Below code will delete the existing report file
if ((new File(c:\\reportfile.html)).exists() )
   (new File(c:\\reportfile.html)).delete();

Below code will append the data to the existing report file stored at given filePath

public static void appendToFile(String filePath,String data) {
       //This function will be used to append text data to filepath
       try{
       File temp = new File(filePath);
       FileWriter fw = new FileWriter(temp,true);
       fw.append(data);
       fw.close();
       }catch(Exception e){}
}

Please note that you will need to import the classes in the package  java.io to work with files.

5. Main driver script in Selenium webdriver automation framework

This is the main method and starting point for the framework code. The main responsibilities of this method are given below.
  1. Read the test case steps from the datasheet one row at a time
  2. Execute the method corresponding to the current step in the test case
  3. Log the verification points in the html report file
  4. Report the overall execution status like total failed/passed test cases, execution time
  5. Send an email to all stakeholders regarding the execution status.


If you want to download the full source code, data sheets, sample scripts and examples, Please buy an e-book on selenium webdriver in Java at the url - Selenium Webdriver Book in Java

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

5 comments:

  1. Hi ,
    your post is nice , it's helping for knowing the basics of Keyword driven, Can you please provide me the sample Code for it.

    Thanks in advance

    ReplyDelete
  2. Please share the code.

    Thanks,
    Vandana

    ReplyDelete
  3. Thanks..Request you please post the compleet code...

    Regards,
    Kuladip

    ReplyDelete
  4. Thanks for sharing detailed information of unified functional testing automation tool. QTP Course in Chennai | QTP training

    ReplyDelete
  5. Very helpful - Can you please share the code for one keyword and the driver script to invoke .. Thank you

    ReplyDelete

Buy Best Selenium Books

About Me

My photo

Self-motivated software professional